1.Write a program that prints Hello World! on screen.
1 2 3 4 5 6 |
#include <stdio.h> int main() { printf("Hello World!"); return 0; } |
2. Sum of two integers
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> int main() { int num1, num2, sum; printf("Enter first number :"); scanf("%d", &num1); printf("Enter second number :"); scanf("%d", &num2); sum = num1 + num2; printf("sum of two numbers is %d", sum); return 0; } |
3. Celsius to Farenheit conversion
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> int main() { float celsius, fahrenheit; printf("Enter the temperature in celcius :"); scanf("%f", &celsius); /*Converting celcius to fahrenheit */ fahrenheit = 9.0 / 5 * celsius + 32; printf("Temperature in fahrenheit : %0.2f", fahrenheit); return 0; } |
4. Simple interest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { float principle, rate, time, simple_interest; printf("Enter the principle :"); scanf("%f", &principle); printf("Enter the rate :"); scanf("%f", &rate); printf("Enter the time :"); scanf("%f", &time); simple_interest = principle * rate * time / 100; printf("Simple interest is %0.2f", simple_interest); return 0; } |
5. Print ASCII value
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> int main() { char letter; printf("Enter a character :"); scanf("%c", &letter); printf("ASCII value of %c is %d.", letter, letter); return 0; } |
6. Swapping of two numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int num1, num2, temp; printf("Enter first number :"); scanf("%d", &num1); printf("Enter second number :"); scanf("%d", &num2); temp = num1; num1 = num2; num2 = temp; printf("After swapping, first is %d and second is %d.", num1, num2); return 0; } |
7. Area and circumference of Circle
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> int main() { float radius, area, circumference; printf("Enter the radius of the circle :"); scanf("%f", &radius); area = 3.14 * radius * radius; circumference = 2 * 3.14 * radius; printf("\nThe area of the circle is %0.2f", area); printf("\nThe circumference of the circle is %0.2f", circumference); return 0; |
8.Area and circumference of Rectangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int length, width, area, circumference; printf("Enter the length of the rectangle :"); scanf("%d", &length); printf("Enter the width of the rectangle :"); scanf("%d", &width); area = length * width; circumference = 2 *(length + width); printf("\nThe area of the rectangle is %d.", area); printf("\nThe circumference of the rectangle is %d.", circumference); return 0; } |
9.
Area of Triangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> #include <math.h> int main() { float a, b, c, s, area; printf("Enter the length of side 1 :"); scanf("%f", &a); printf("Enter the length of side 2 :"); scanf("%f", &b); printf("Enter the length of side 3 :"); scanf("%f", &c); s = (a + b + c) / 2; area = sqrt(s*(s - a)*(s - b)*(s - c)); printf("\nThe area of the triangle is %0.2f", area); return 0; } |
10. Compound Interest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> #include <math.h> int main() { float p, r, t, ci; printf("Enter the principle :"); scanf("%f", &p); printf("Enter the rate :"); scanf("%f", &r); printf("Enter the time :"); scanf("%f", &t); ci = p * pow((1 + r / 100), t) - p; printf("\nThe compound interest is %0.2f", ci); return 0; |
10 .Write a program that prompts the user to input a number and display if the number is even or odd.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int main() { int number; printf("Enter a number :"); scanf("%d", &number); if (number % 2 == 0) { printf("The number is even."); } else { printf("The number is odd."); } return 0; } |
11.Write a program that prompts the user to input a year and determine whether the year is a leap year or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int year; printf("Enter a year :"); scanf("%d", &year); /* Determine whether the year is leap year */ if ((year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0))) { printf("A leap year"); } else { printf("Not a leap year"); } return 0; } |
12. Write a program that prompts the user to input a character and determine the character is vowel or consonant.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> int main() { char letter; printf("Enter a letter :"); scanf("%c", &letter); switch (letter) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': printf("The letter is a vowel."); break; default: printf("The letter is a consonent."); } return 0; } |
13.Write a program to print numbers from 1 to 10.
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> int main() { int i; for (i = 1; i <= 10; i++) { printf("%d ", i); } return 0; } |
14 .Write a program that prompts the user to input a number and prints its factorial.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number :"); scanf("%d", &n); for (i = 1; i <= n; i++) { fact *= i; } printf("The factorial of %d is %d.", n, fact); return 0; } |
15.Write a program that prompts the user to input a number and reverse its digits. For example, the reverse of 12345 is 54321; reverse of 5600 is 65.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int main() { int number, temp, remainder, reverse = 0; printf("Enter a positive integer :"); scanf("%d", &number); temp = number; while (temp > 0) { remainder = temp % 10; reverse = reverse * 10 + remainder; temp /= 10; } printf("The reverse of %d is %d.", number, reverse); return 0; } |
16 .A palindromic number is a number that remains the same when its digits are reversed. For example, 16461. Write a program that prompts the user to input a number and determine whether the number is palindrome or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> int main() { int number, temp, remainder, reverse = 0; printf("Enter a number :"); scanf("%d", &number); temp = number; while (temp > 0) { remainder = temp % 10; reverse = reverse * 10 + remainder; temp /= 10; } if (reverse == number) { printf("The number is palindrome."); } else { printf("The number is not palindrome."); } return 0; } |
17.Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number.
A prime number is a number that is evenly divisible only by itself and 1. For example, the number 5 is prime because it can be evenly divided only by 1 and 5. The number 6, however, is not prime because it can be divided evenly by I, 2, 3, and 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> int main() { int number, i; int flag = 1; printf("Enter any number : "); scanf("%d", &number); for (i = 2; i < number; i++) { if (number % i == 0) { flag = 0; break; } } if (flag == 1 && number != 1) { printf("Number is prime"); } else { printf("Number is not prime"); } return 0; } |
18.Write a program to enter the numbers till the user wants and at the end the program should display the largest and smallest numbers entered.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> int main() { int number, max = 0, min = 32767; char choice; do { printf("Enter a number :"); scanf("%d", &number); if (number > max) { max = number; } if (number < min) { min = number; } printf("Do you want to Continue(y/n)? "); scanf("%c", &choice); }while (choice == 'y' || choice == 'Y'); printf("\nMaximum number :%d\nMinimum Number :%d", max, min); return 0; } |
19.Write a program to find all Armstrong number in the range of 0 and 999
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 33 + 73 + 13 = 371.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> int main() { int number, i, digit, sum; for (i = 0; i < 1000; i++) { number = i; sum = 0; while (number > 0) { digit = number % 10; sum += digit * digit * digit; number /= 10; } if (sum == i) { printf("%d\t", i); } } return 0; } |