Example: Factorial of a Number
#includeint main(){int n, i;unsigned long long factorial = 1;printf("Enter an integer: ");scanf("%d",&n);// show error if the user enters a negative integerif (n < 0)printf("Error! Factorial of a negative number doesn't exist.");else{for(i=1; i<=n; ++i){factorial *= i; // factorial = factorial*i;}printf("Factorial of %d = %llu", n, factorial);}return 0;}
Output Enter an integer 4
Factorial of input is 24
Example #1: Check if a Number is Positive or Negative Using if...else
#includeint main(){double number;printf("Enter a number: ");scanf("%lf", &number);if (number <= 0.0){if (number == 0.0)printf("You entered 0.");elseprintf("You entered a negative number.");}elseprintf("You entered a positive number.");return 0;}
You can also solve this problem using nested if else statement.
Example #2: Check if a Number is Positive or Negative Using Nested if...else
#includeint main(){double number;printf("Enter a number: ");scanf("%lf", &number);// true if number is less than 0if (number < 0.0)printf("You entered a negative number.");// true if number is greater than 0else if ( number > 0.0)printf("You entered a positive number.");// if both test expression is evaluated to falseelseprintf("You entered 0.");return 0;}
Output 1
Enter a number :35 you entered positive number
Output 2
Enter a number :-36
You entered negative number
Example: Program to Find Roots of a Quadratic Equation
#include#includeint main(){double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;printf("Enter coefficients a, b and c: ");scanf("%lf %lf %lf",&a, &b, &c);discriminant = b*b-4*a*c;// condition for real and different rootsif (discriminant > 0){// sqrt() function returns square rootroot1 = (-b+sqrt(discriminant))/(2*a);root2 = (-b-sqrt(discriminant))/(2*a);printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);}//condition for real and equal rootselse if (discriminant == 0){root1 = root2 = -b/(2*a);printf("root1 = root2 = %.2lf;", root1);}// if roots are not realelse{realPart = -b/(2*a);imaginaryPart = sqrt(-discriminant)/(2*a);printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imaginaryPart, realPart, imaginaryPart);}return 0;}
Output
Enter coefficients a, b and c: 2.3 4 5.6 Roots are: -0.87+1.30i and -0.87-1.30i
C Program to Find LCM of two Numbers

To understand this example, you should have the knowledge of following C programming topics:
The LCM of two integers n1 and n2is the smallest positive integer that is perfectly divisible by both n1 and n2(without a remainder). For example: the LCM of 72 and 120 is 360.
Example #1: LCM using while Loop and if Statement
#includeint main(){int n1, n2, minMultiple;printf("Enter two positive integers: ");scanf("%d %d", &n1, &n2);// maximum number between n1 and n2 is stored in minMultipleminMultiple = (n1>n2) ? n1 : n2;// Always truewhile(1){if( minMultiple%n1==0 && minMultiple%n2==0 ){printf("The LCM of %d and %d is %d.", n1, n2,minMultiple);break;}++minMultiple;}return 0;}
Output
Enter two positive integers: 72 120 The LCM of 72 and 120 is 360.
In this program, the integers entered by the user are stored in variable n1and n2 respectively.
The largest number among n1 and n2 is stored in minMultiple. The LCM of two numbers cannot be less than minMultiple.
The test expression of while loop is always true (1). In each iteration, whether minMultiple is perfectly divisible by n1 and n2 is checked. If this test condition is not true, minMultiple is incremented by 1 and the iteration continues until the test expression of if statement is true.
The LCM of two numbers can also be found using the formula:
LCM = (num1*num2)/GCD
Learn more on, how to find the GCD of two numbers in C programming.
Example #2: LCM Calculation by Finding GCD
#includeint main(){int n1, n2, i, gcd, lcm;printf("Enter two positive integers: ");scanf("%d %d",&n1,&n2);for(i=1; i <= n1 && i <= n2; ++i){// Checks if i is factor of both integersif(n1%i==0 && n2%i==0)gcd = i;}lcm = (n1*n2)/gcd;printf("The LCM of two numbers %d and %d is %d.", n1, n2, lcm);return 0; }C Program to Check Leap Year
This program checks whether an year (integer) entered by the user is a leap year or not.To understand this example, you should have the knowledge of following C programming topics:
A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.
Example: Program to Check Leap Year
#includeint main(){int year;printf("Enter a year: ");scanf("%d",&year);if(year%4 == 0){if( year%100 == 0){// year is divisible by 400, hence the year is a leap yearif ( year%400 == 0)printf("%d is a leap year.", year);elseprintf("%d is not a leap year.", year);}elseprintf("%d is a leap year.", year );}elseprintf("%d is not a leap year.", year);return 0;}Output 1
Enter a year: 1900 1900 is not a leap year.Output 2
Enter a year: 2012C Program to Check Leap Year
This program checks whether an year (integer) entered by the user is a leap year or not.To understand this example, you should have the knowledge of following C programming topics:
A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.
Example: Program to Check Leap Year
#includeint main(){int year;printf("Enter a year: ");scanf("%d",&year);if(year%4 == 0){if( year%100 == 0){// year is divisible by 400, hence the year is a leap yearif ( year%400 == 0)printf("%d is a leap year.", year);elseprintf("%d is not a leap year.", year);}elseprintf("%d is a leap year.", year );}elseprintf("%d is not a leap year.", year);return 0;}Output 1
Enter a year: 1900 1900 is not a leap year.Output 2
Enter a year: 2012 2012 is a leap year.Example: Program to Check Palindrome
#include <stdio.h>int main(){int n, reversedInteger = 0, remainder, originalInteger;printf("Enter an integer: ");scanf("%d", &n);originalInteger = n;// reversed integer is stored in variablewhile( n!=0 ){remainder = n%10;reversedInteger = reversedInteger*10 + remainder;n /= 10;}// palindrome if orignalInteger and reversedInteger are equalif (originalInteger == reversedInteger)printf("%d is a palindrome.", originalInteger);elseprintf("%d is not a palindrome.", originalInteger);return 0;}Output
Enter an integer: 1001 1001 is a palindrome


Fantastic content
ReplyDeletebest
ReplyDeleteAwesome
ReplyDeleteSir you are helping those students who are poor
ReplyDeleteJai hoo
ReplyDeleteLove you 💓💓💓
ReplyDelete