C Program to Check Whether a Character is a Vowel or Consonant
Code:
#include <stdio.h> int main() { char c; int low, upp; printf("Enter any alphabet: "); scanf("%c", &c); low = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); upp = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); if (low || upp) { printf("Vowel\n"); } else { printf("Consonant\n"); } return 0; }
Output:
Enter any alphabet: A Vowel
C Program to Find the Largest Number Among Three Numbers
Code:
#include <stdio.h> int main() { int a, b, c; printf("Enter first no: "); scanf("%d", &a); printf("Enter second no: "); scanf("%d", &b); printf("Enter third no: "); scanf("%d", &c); if (a >= b && a >= c) printf("Largest number: %d\n", a); if (b >= a && b >= c) printf("Largest number: %d\n", b); if (c >= a && c >= b) printf("Largest number is %d\n", c); return 0; }
Output:
Enter first no: 1 Enter second no: 6 Enter third no: 4 Largest number: 6
C Program to Find the Roots of a Quadratic Equation
Code:
#include <math.h> #include <stdio.h> int main() { double a, b, c, disc, rt1, rt2, real, imaginary; printf("Enter coefficients a: "); scanf("%lf", &a); printf("Enter coefficients b: "); scanf("%lf", &b); printf("Enter coefficients c: "); scanf("%lf", &c); disc = (b * b) - (4 * a * c); if (disc > 0) { rt1 = (-b + sqrt(disc)) / (2 * a); rt2 = (-b - sqrt(disc)) / (2 * a); printf("rt1 = %.2lf and rt2 = %.2lf", rt1, rt2); } else if (disc == 0) { rt1 = rt2 = -b / (2 * a); printf("First root = Second root = %.2lf;", rt1); } else { real = -b / (2 * a); imaginary = sqrt(-disc) / (2 * a); printf("First root = %.2lf+%.2lfi and Second root = %.2f-%.2fi", real, imaginary, real, imaginary); } return 0; }
Output:
Enter coefficients a: 4 Enter coefficients b: 5 Enter coefficients c: 2 First root = -0.62+0.33i and Second root = -0.62-0.33i
C Program to Check Leap Year
Code:
#include <stdio.h> int main() { int n; printf("Year: "); scanf("%d", &n); if (n % 400 == 0) { printf("Leap Year", n); } else if (n % 100 == 0) { printf("Not a Leap Year", n); } else if (n % 4 == 0) { printf("Leap Year", n); } else { printf("Not a Leap Year", n); } return 0; }
Output:
Year: 2020 Leap Year
C Program to Check Whether a Number is Positive or Negative
Code:
#include <stdio.h> int main() { int n; printf("Input a number: "); scanf("%d", &n); if (n <= 0.0) { if (n == 0.0) printf("Entered number is 0\n"); else printf("Negative number\n"); } else printf("Positive number\n"); return 0; }
Output:
Input a number: 5 Positive number
C Program to Check Whether a Character is an Alphabet or not
Code:
#include <stdio.h> int main() { char val; printf("Enter a character value: "); scanf("%c", &val); if ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z')) printf("Alphabet\n", val); else printf("Not an alphabet\n", val); return 0; }
Output:
Enter a character value: b Alphabet
C Program to Calculate the Sum of Natural Numbers
Code:
#include <stdio.h> int main() { int n, i, add = 0; printf("Enter a number: "); scanf("%d", &n); for (i = 1; i <= n; ++i) { add = add+i; } printf("Sum of natural numbers from 1 to %d is %d\n", n, add); return 0; }
Output:
Enter a number: 8 Sum of natural numbers from 1 to 8 is 36
C Program to Find Factorial of a Number
Code:
#include <stdio.h> int main() { int num, i; long long unsigned int fact = 1; printf("Enter a number: "); scanf("%d", &num); if (num < 0) printf("Write a positive number."); else { for (i = 1; i <= num; ++i) { fact *= i; } printf("Factorial is %llu\n", fact); } return 0; }
Output:
Enter a number: 5 Factorial is 120
C Program to Generate Multiplication Table
Code:
#include <stdio.h> int main() { int a, i; printf("Enter an integer value: "); scanf("%d", &a); for (i = 1; i <= 10; ++i) { printf("%d * %d = %d \n", a, i, a * i); } return 0; }
Output:
Enter an integer value: 9 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 9 * 10 = 90
C Program to Display Fibonacci Sequence
Code:
#include <stdio.h> int main() { int i, n, term1 = 0, term2 = 1, next; printf("Number of terms to be calculated: "); scanf("%d", &n); printf("This is the Fibonacci Series: "); for (i = 1; i <= n; ++i) { printf("%d, ", term1); next = term1 + term2; term1 = term2; term2 = next; } printf("\n"); return 0; }
Output:
Number of terms to be calculated: 8 This is the Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13,
C Program to Find GCD of two Numbers
Code:
#include <stdio.h> int main() { int number1, number2, i, GCD; printf("Enter first integer: "); scanf("%d", &number1); printf("Enter two integer: "); scanf("%d", &number2); for (i = 1; i <= number1 && i <= number2; ++i) { if (number1 % i == 0 && number2 % i == 0) GCD = i; } printf("G.C.D: %d\n", GCD); return 0; }
Output:
Enter first integer: 5 Enter two integer: 9 G.C.D: 1
C Program to Find LCM of two Numbers
Code:
#include <stdio.h> int main() { int number1, number2, maximum; printf("Enter first integer: "); scanf("%d", &number1); printf("Enter second integer: "); scanf("%d", &number2); maximum = (number1 > number2) ? number1 : number2; while (1) { if (maximum % number1 == 0 && maximum % number2 == 0) { printf("LCM: %d\n", maximum); break; } ++maximum; } return 0; }
Output:
Enter first integer: 10 Enter second integer: 20 LCM: 20
C Program to Display Characters from A to Z Using Loop
Code:
#include <stdio.h> int main() { char ch; for (ch = 'A'; ch <= 'Z'; ++ch) { printf("%c ", ch); } printf("\n"); return 0; }
Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
C Program to Count Number of Digits in an Integer
Code:
#include <stdio.h> int main() { long long a; int count = 0; printf("Enter an integer value: "); scanf("%lld", &a); while (a != 0) { a /= 10; ++count; } printf("%d digits in integer\n", count); }
Output:
Enter an integer value: 145 3 digits in integer
C Program to Reverse a Number
Code:
#include <stdio.h> int main() { int n, reverse = 0, rem; printf("Enter an integer value: "); scanf("%d", &n); while (n != 0) { rem = n % 10; reverse = reverse * 10 + rem; n /= 10; } printf("Reverse of the number is %d\n", reverse); return 0; }
Output:
Enter an integer value: 1984 Reverse of the number is 4891
C Program to Calculate the Power of a Number
Code:
#include <stdio.h> int main() { int a, n; long long sum = 1; printf("Base number: "); scanf("%d", &a); printf("Exponent: "); scanf("%d", &n); while (n != 0) { sum *= a; --n; } printf("Value evaluated is %lld\n", sum); return 0; }
Output:
Base number: 8 Exponent: 3 Value evaluated is 512
C Program to Check Whether a Number is Palindrome or Not
Code:
#include <stdio.h> int main() { int n, reverse = 0, rem, origin; printf("Enter an integer value : "); scanf("%d", &n); origin = n; while (n != 0) { rem = n % 10; reverse = reverse * 10 + rem; n /= 10; } if (origin == reverse) printf("Palindrome\n", origin); else printf("Not a Palindrome\n", origin); return 0; }
Output:
Enter an integer value : 5 Palindrome
C Program to Check Whether a Number is Prime or Not
Code:
#include <stdio.h> int main() { int num, i, k = 0; printf("Enter a number: "); scanf("%d", &num); for (i = 2; i <= num / 2; ++i) { if (num % i == 0) { k = 1; break; } } if (k == 0) printf("Prime number\n", num); else printf("Not a Prime number\n", num); return 0; }
Output:
Enter a number: 49 Not a Prime number
C Program to Display Prime Numbers Between Two Intervals
Code:
#include <stdio.h> int main() { int open, close, i, k; printf("Enter opening intervals: "); scanf("%d", &open); printf("Enter closing intervals: "); scanf("%d",&close); printf("Prime number are ", open, close); while (open < close) { k = 0; if (open <= 1) { ++open; continue; } for (i = 2; i <= open / 2; ++i) { if (open % i == 0) { k = 1; break; } } if (k == 0) printf("%d ", open); ++open; } printf("\n"); return 0; }
Output:
Enter opening intervals: 1 Enter closing intervals: 20 Prime number are 2 3 5 7 11 13 17 19
C Program to Check Armstrong Number
Code:
#include <math.h> #include <stdio.h> int main() { int number, original, rem, n = 0; float result = 0.0; printf("Enter an number: "); scanf("%d", &number); original = number; for (original = number; original != 0; ++n) { original /= 10; } for (original = number; original != 0; original /= 10) { rem = original % 10; result += pow(rem, n); } if ((int)result == number) printf("Armstrong number\n", number); else printf("Armstrong number\n", number); return 0; }
Output:
Enter an number: 9 Armstrong number
C Program to Display Armstrong Number Between Two Intervals
Code:
#include <math.h> #include <stdio.h> int main() { int lowest, highest, number, original, remainder, reps = 0; double result = 0.0; printf("Enter opening interval: "); scanf("%d", &lowest); printf("Enter closing interval: "); scanf("%d", &highest); printf("Armstrong numbers are: ", lowest, highest); for (number = lowest + 1; number < highest; ++number) { original = number; while (original != 0) { original /= 10; ++reps; } original = number; while (original != 0) { remainder = original % 10; result += pow(remainder, reps); original /= 10; } if ((int)result == number) { printf("%d ", number); } reps = 0; result = 0; } printf("\n"); return 0; }
Output:
Enter opening interval: 5 Enter closing interval: 20 Armstrong numbers are: 6 7 8 9
C Program to Display Factors of a Number
Code:
#include <stdio.h> int main() { int number, i; printf("Enter an integer: "); scanf("%d", &number); printf("Factors: ", number); for (i = 1; i <= number; ++i) { if (number % i == 0) { printf("%d ", i); } } printf("\n"); return 0; }
Output:
Enter an integer: 5 Factors: 1 5
C Program to Make a Simple Calculator Using switch…case
Code:
#include <stdio.h> int main() { char oprtr; double n1, n2; printf("Enter an operation to be done (+, -, *,): "); scanf("%c", &oprtr); printf("Enter first number: "); scanf("%lf", &n1); printf("Enter second number: "); scanf("%lf", &n2); switch (oprtr) { case '+': printf("%.1lf + %.1lf = %.1lf", n1, n2, n1 + n2); break; case '-': printf("%.1lf - %.1lf = %.1lf", n1, n2, n1 - n2); break; case '*': printf("%.1lf * %.1lf = %.1lf", n1, n2, n1 * n2); break; case '/': printf("%.1lf / %.1lf = %.1lf", n1, n2, n1 / n2); break; default: printf(" oprtr is not correct"); } printf("\n"); return 0; }
Output:
Enter an operation to be done (+, -, *,): + Enter first number: 4 Enter second number: 6 4.0 + 6.0 = 10.0