Example: Factorial of a Number
#include
int main()
{
int n, i;
unsigned long long factorial = 1;
printf("Enter an integer: ");
scanf("%d",&n);
// show error if the user enters a negative integer
if (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
#include
int main()
{
double number;
printf("Enter a number: ");
scanf("%lf", &number);
if (number <= 0.0)
{
if (number == 0.0)
printf("You entered 0.");
else
printf("You entered a negative number.");
}
else
printf("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
#include
int main()
{
double number;
printf("Enter a number: ");
scanf("%lf", &number);
// true if number is less than 0
if (number < 0.0)
printf("You entered a negative number.");
// true if number is greater than 0
else if ( number > 0.0)
printf("You entered a positive number.");
// if both test expression is evaluated to false
else
printf("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
#include
int 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 roots
if (discriminant > 0)
{
// sqrt() function returns square root
root1 = (-b+sqrt(discriminant))/(2*a);
root2 = (-b-sqrt(discriminant))/(2*a);
printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);
}
//condition for real and equal roots
else if (discriminant == 0)
{
root1 = root2 = -b/(2*a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else
{
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
#include
int main()
{
int n1, n2, minMultiple;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
// maximum number between n1 and n2 is stored in minMultiple
minMultiple = (n1>n2) ? n1 : n2;
// Always true
while(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
#include
int 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 integers
if(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
#include
int 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 year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%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
#include
int 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 year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%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 variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%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