Factorial
C Program To Find Factorial of a Given number
Factorial of n is the product of all positive descending integers.
Factorial of n is denoted by n!.
For example:
5! = 5*4*3*2*1 = 120
3! = 3*2*1 = 6
Here, 5! is pronounced as "5 factorial", it is also called "5 bang" or "5 shriek".
The factorial is normally used in Combinations and Permutations (mathematics).
Source Code
// C Program To Find Factorial of a Given number ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num , i , fact = 1;
printf("Enter a Number : ");
scanf("%d",&num);
for( i=1 ; i<=num ; i++ )
{
fact = fact * i ;
}
printf("Factorial of a Given Number %d is %d ",num,fact);
getch();
}
Sample Input :
Enter a Number : 5
Sample Output:
Factorial of a Given Number 5 is : 120
Sample Input :
Enter a Number: 6
Sample Output:
Factorial of a Given Number 6 is : 720
Perfect Number
C Program To Check Perfect Number or Not
In mathematics, a perfect number is a positive integer that is
equal to the sum of its positive divisors, excluding the number itself.
For Example,
28 is a positive number that is completely divisible by 1, 2, 4, 7 and 14.
We know that the number is also divisible by itself but we will exclude it in the addition of divisors.
When we add these divisors (1 + 2 + 4 + 7 + 14 = 28), it produces 28, which is equal to the number that we have considered.
So, we can say that 28 is a perfect number.
Source Code
// C Program To Check Perfect Number
#include<stdio.h>
#include<conio.h>
void main()
{
int num , i , sum = 0 ;
printf("Enter a Number To Check : ");
scanf("%d",&num);
for( i=1; i<=num/2; i++ )
{
if(num%i==0)
{
printf("%d ",i);
sum = sum + i ;
}
}
printf("\nSum of Factors of a Given number : %d \n",sum);
if(sum==num)
printf("It is a Perfect Number . ");
else
printf("It is Not a Perfect Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 28
Sample Output:
1 2 4 7 14
Sum of Factors of a Given Number : 28
It is a Perfect Number
Sample Input :
Enter a Number To Check : 10
Sample Output:
1 2 5
Sum of Factors of a Given Number : 8
It is Not a Perfect Number
Prime Number
C Program To Check Prime Number or Not
A prime number is a whole number greater than 1 whose only factors are 1 and itself.
A factor is a whole number that can be divided evenly into another number.
The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.
Numbers that have more than two factors are called composite numbers.
Note: The number 1 is neither prime nor composite.
Example :
Given Number : 11
It is a Prime Number
Since :
11 whose only factors are 1 and itself (11).
Given Number : 10
It is Not a Prime Number
Since :
10 Have more than two factors are called composite numbers
Source Code
// C Program To Check Prime Number - Part One
#include<stdio.h>
#include<conio.h>
void main()
{
int num , i , count = 0 ;
printf("Enter a Number To Check : ");
scanf("%d",&num);
for( i=1; i<=num; i++ )
{
if( num%i==0 )
{
printf("%d ",i);
count++;
}
}
if(count==2)
printf("\nIt is a Prime Number . ");
else
printf("\nIt is Not a Prime Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 11
Sample Output:
1 11
It is a Prime Number
Sample Input :
Enter a Number To Check : 10
Sample Output:
1 2 5 10
It is Not a Prime Number
Source Code
// C Program To Check Prime Number - Part Two
#include<stdio.h>
#include<conio.h>
void main()
{
int num , i ;
int check=0;
printf("Enter a Number To Check : ");
scanf("%d",&num);
for( i=2; i<=num/2; i++ )
{
if( num%i==0 )
{
check=1;
break;
}
}
if(check==0)
printf("It is a Prime Number . ");
else
printf("It is Not a Prime Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 11
Sample Output:
It is a Prime Number
Sample Input :
Enter a Number To Check : 10
Sample Output:
It is Not a Prime Number
Twin Prime Number
C Program To Check Twin Prime Numbers Or Not
A twin prime is a prime number that is Either 2 less or 2 more than another prime number
A twin prime is a prime that has a prime gap of Two
Example:
Given Numbers : 11 and 13
11 and 13 are Twin Prime Number Because
11 and 13 Both are Prime Numbers and Gap Between Them are 2 .
Given Numbers : 11 and 19
11 and 19 are Not Twin Prime Number Because
11 and 19 Both are Prime Numbers But Gap Between Them are Not 2 .
//C Program To Check Twin Prime Number
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
int i,count1=0,count2=0;
printf("Enter First Number : ");
scanf("%d",&num1);
printf("Enter Second Number : ");
scanf("%d",&num2);
// Count The Factors of num1
for( i=1;i<=num1;i++ )
{
if(num1%i==0)
count1++;
}
// Count The Factors of num2
for( i=1;i<=num2;i++ )
{
if(num2%i==0)
count2++;
}
if( (count1==2 && count2==2)&& ((num1-num2)==2 || (num1-num2)==-2 ))
printf("%d and %d are Twin Prime Numbers ",num1,num2);
else
printf("%d and %d are Not Twin Prime Numbers ",num1,num2);
getch();
}
Sample Input :
Enter First Number : 11
Enter Second Number : 13
Sample Output:
11 and 13 are Twin Prime Numbers
Sample Input :
Enter First Number : 17
Enter Second Number : 23
Sample Output:
17 and 23 are Not Twin Prime Numbers
HCF and LCM
C++ Program To Find HCF and LCM of Two Numbers
HCF or Highest Common Factor is the greatest number which divides
each of the two or more numbers. HCF is also called the Greatest Common Measure (GCM)
and Greatest Common Divisor(GCD).
HCM and LCM are two different methods, where LCM or Least Common Multiple is used to
find the smallest common multiple of any two or more numbers.
LCM = (Product of Two Or More Numbers ) / HCF
Example :
Given Numbers : 10 and 20
HCF = 10
LCM = 20
Given Numbers : 8 and 12
HCF = 4
LCM = 24
//C Program To Find HCF and LCM of Two Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int num1 , num2 , Min ;
int i , HCF , LCM ;
printf("Enter First Number : ");
scanf("%d",&num1);
printf("Enter Second Number : ");
scanf("%d",&num2);
if (num1<=num2)
Min = num1 ;
else
Min = num2 ;
for( i=1;i<=Min;i++ )
{
if( num1%i==0 && num2%i==0 )
HCF = i;
}
LCM = (num1*num2)/HCF;
printf("HCF of %d and %d is : %d \n",num1,num2,HCF);
printf("HCF of %d and %d is : %d \n",num1,num2,LCM);
getch();
}
Sample Input :
Enter First Number : 10
Enter Second Number: 20
Sample Output:
HCF of 10 and 20 is : 10
LCM of 10 and 20 is : 20
Sample Input :
Enter First Number : 8
Enter Second Number: 12
Sample Output:
HCF of 8 and 12 is : 4
LCM of 8 and 12 is : 24
Count Number of Digits
C Program To Count Number of digits of a Given Number
Program To Count Number of Digits of a Given Number
Example :
Given Number : 125
Number of Digits of a Given Number : 3
Given Number : 1256
Number of Digits of a Given Number : 4
Source Code
// C Program To Count Number of digits of a Given Number
#include<stdio.h>
#include<conio.h>
void main()
{
int num , count = 0;
printf("Enter a Number : ");
scanf("%d",&num);
while( num!=0 )
{
num = num / 10;
count++;
}
printf("No. of Digits : %d ",count);
getch();
}
Sample Input :
Enter a Number: 125
Sample Output:
No. of Digits : 3
Sample Input :
Enter a Number To Check : 1024
Sample Output:
No. of Digits :4
Duck Number
C Program To Check Duck Numbers
A Duck number is a positive number which has zeroes present in it,Example: 120, 1024, 102030 are all Duck numbers.
Note : A Numbers with only leading 0s is not considered as Duck Number.
Example:
Numbers Like 007 or 001 are Not Considered as Duck Numbers.
//C Program To Check Duck Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int num , Rd , count=0;
printf("Enter a Number To Check : ");
scanf("%d",&num);
while( num!=0 )
{
Rd = num % 10 ;
if( Rd==0 )
count++;
num = num / 10 ;
}
if( count>=1 )
printf("It is a Duck Number . ");
else
printf("It is Not a Duck Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 1024
Sample Output:
It is a Duck Number
Sample Input :
Enter a Number To Check : 123
Sample Output:
It is Not a Duck Number
Find Sum of Digits
C Program To Find Sum of Digits of a Given Numbers
Program To Find Sum of Digits of a Given Number
Example :
Given Number : 125
Sum of Digits of a Given Number : 1+2+5 = 8
Given Number : 1254
Sum of Digits of a Given Number : 1+2+5+4 = 12
Source Code
// C Program To Find Sum of Digits of a Given Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int num , Rd , sum = 0 ;
printf("Enter a number To Check : ");
scanf("%d",&num);
while( num!=0 )
{
Rd = num % 10 ;
sum = sum + Rd ;
num = num / 10;
}
printf("Sum of Digits of a Given numbre : %d ",sum);
getch();
}
Sample Input :
Enter a Number To Check : 125
Sample Output:
Sum of Digits of a Given Number : 8
Sample Input :
Enter a Number To Check : 123
Sample Output:
Sum of Digits of a Given Number : 6
Niven Or Harshad Number
C Program To Check Niven or Harshad Number or Not
Niven Number or Harshad Number
In mathematics, A Harshad Number (or Niven number) in a given number
base is an integer that is divisible by the sum of its digits
when written in that base. Harshad numbers in base n are also known as
n-harshad (or n-Niven) numbers.
Example :
Given Number : 36
Sum of its Digits : 3 + 6 = 9
36 is a Niven or Harshad Number
Since ,
36 is Divisible By The Sum of Its Digits
//C Program To Check Niven or Harshad Number or Not
#include<stdio.h>
#include<conio.h>
void main()
{
int num , Rd , sum = 0 , ncopy ;
printf("Enter a Number To Check : ");
scanf("%d",&num);
ncopy = num ;
while( num!=0 )
{
Rd = num % 10 ;
sum = sum + Rd ;
num = num / 10 ;
}
if ( ncopy%sum==0 )
printf("It is a Niven Number ");
else
printf("It is Not a Niven Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 12
Sample Output:
It is a Niven Number
Sample Input :
Enter a Number To Check : 15
Sample Output:
It is Not a Niven Number
Spy Number
C Program To Check Spy Number or Not
Spy Number
A number is said to be a Spy number if the
sum of all the digits is equal to the product of all digits.
Example :
Given Number : 1412
Sum of all the Digits = 1+4+1+2 = 8
Product of all the Digit = 1*4*1*2 = 8
1412 is a Spy Number
Since ,
Sum of all the digits is equal to the product of all digits.
//C Program To Check Spy Number or Not
#include<stdio.h>
#include<conio.h>
void main()
{
int num,Rd,sum=0,pro=1;
printf("Enter a Number To Check : ");
scanf("%d",&num);
while( num!=0 )
{
Rd = num % 10 ;
sum = sum + Rd ;
pro = pro * Rd ;
num = num / 10 ;
}
if( sum==pro )
printf("It is a Spy Number . ");
else
printf("It is Not a Spy Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 123
Sample Output:
It is a Spy Number
Sample Input :
Enter a Number To Check : 1421
Sample Output:
It is a Spy Number
Sample Input :
Enter a Number To Check : 125
Sample Output:
It is a Not Spy Number
Neon Number
C Program To Check Neon Number or Not
A neon number is a number where the sum of digits of square of
the number is equal to the number.
Example :
Given Number : 9
It is a Neon Number
Square of a Given Number : 9*9 = 81
Sum of Digit of a Square of a Given Number : 8+1 = 9
Since :
Sum of Digit of a Square of a Given Number is Equal To The Number (Given Number)
Given Number : 5
It is Not a Neon Number
Square of a Given Number : 5*5 = 25
Sum of Digit of a Square of a Given Number : 2+5 = 7
Since :
Sum of Digit of a Square of a Given Number is Not Equal To The Number (Given Number)
Source Code
// C Program To Check Neon Number or Not
#include<stdio.h>
#include<conio.h>
void main()
{
int num , Square , Rd , sum = 0 ;
printf("Enter a Number To Check : ");
scanf("%d",&num);
Square = num * num ;
while ( Square!=0 )
{
Rd = Square % 10 ;
sum = sum + Rd ;
Square = Square / 10 ;
}
if( sum==num )
printf("It is a Neon Number . ");
else
printf("It is Not a Neon Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 9
Sample Output:
It is a Neon Number
Sample Input :
Enter a Number To Check : 5
Sample Output:
It is Not a Neon Number
Armstrong Number
C Program To Check Armstrong number or Not
Armstrong number(after Michael F. Armstrong) or a plus perfect number)
Armstrong is a number that is the sum of its own digits each raised to the power of
the number of digits.
Example :
Given Number : 153 (It is a Three Digit Number)
=> (1*1*1) + (5*5*5) + (3*3*3) -> Sum of Cubes of its digits
=> 1 + 125 + 27
=> 153
153 is an Armstrong Number
Source Code
// C Program To Check Armstrong number or Not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num , Rd , sum = 0 , ncopy;
printf("Enter a Number To Check : ");
scanf("%d",&num);
ncopy = num ;
while ( num!=0 )
{
Rd = num % 10 ;
sum = sum + Rd * Rd * Rd ;
num = num / 10 ;
}
if ( sum==ncopy )
printf("It is an Armstrong Number . ");
else
printf("It is not an Armstrong Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 153
Sample Output:
It is an Armstrong Number
Sample Input :
Enter a Number To Check : 125
Sample Output:
It is Not an Armstrong Number
To Check Any Digit Number Whether it is an Armstrong number or Not ?
// To Check Any Digit Number Whether it is Armstrong number or Not ?
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num , count=0;
int Rd , sum = 0 , ncopy;
printf("Enter a Number To Check : ");
scanf("%d",&num);
ncopy = num ;
// To Count Number of Digit of a Given Number
while( num!=0 )
{
num = num / 10;
count++;
}
num=ncopy;
// To Find Sum of Its own digits each raised to the power of
// The number of digits.
while ( num!=0 )
{
Rd = num % 10 ;
sum = sum + pow(Rd,count);
num = num / 10 ;
}
if ( sum==ncopy )
printf("It is an Armstrong Number . ");
else
printf("It is not an Armstrong Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 1634
Sample Output:
It is an Armstrong Number
Sample Input :
Enter a Number To Check : 123
Sample Output:
It is Not an Armstrong Number
Palindrome Number
C Program To Check Palindrome number or Not
A Palindromic number (Also Known As a Numeral Palindrome or a Numeric palindrome)
is a number (such as 12321) that remains the same when its digits are reversed
Example :
Given Number : 121
It is a Palindrome Number
Given Number : 123
It is Not a Palindrome Number
Source Code
// C Program To Check Palindrome number or Not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num , Rd , Rev = 0 , ncopy ;
printf("Enter a number To Check : ");
scanf("%d",&num);
ncopy=num;
while( num!=0 )
{
Rd = num % 10 ;
Rev = Rev * 10 + Rd ;
num = num / 10 ;
}
if( Rev==ncopy )
printf("It is a Palindrome Number . ");
else
printf("It is Not a Palindrome Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 121
Sample Output:
It is a Palindrome Number
Sample Input :
Enter a Number To Check : 124
Sample Output:
It is Not a Palindrome Number
Strong Number
C Program To Check Strong Number or Not
Strong number is a special number whose sum of the factorial of digits
is equal to the original number.
Example:
Given Number : 145
It is a Strong Nuumber.
Since: 1! + 4! + 5! = 145
1! = 1*1 = 1
4! = 1*2*3*4 = 24
5! = 1*2*3*4*5 = 120
Given Number : 125
It is Not a Strong Nuumber.
Since:
1! + 2! + 5! = 123
1! = 1*1 = 1
2! = 1*2 = 2
5! = 1*2*3*4*5 = 120
Source Code
// C Program To Check Strong Number or Not
#include<stdio.h>
#include<conio.h>
void main()
{
int num , Rd , i , fact , sum = 0 , ncopy ;
printf("Enter a Number To Check : ");
scanf("%d",&num);
ncopy=num;
while( num!=0 )
{
Rd = num % 10 ;
fact = 1;
for( i=1; i<=Rd; i++ )
{
fact = fact * i ;
}
sum = sum + fact ;
num = num / 10;
}
if ( sum==ncopy )
printf("It is a Strong Number . ");
else
printf("It is Not a Strong Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 145
Sample Output:
It is a Strong Number
Sample Input :
Enter a Number To Check : 123
Sample Output:
It is Not a Strong Number
Automorphic Number
C Program To Check Automorphic Number or Not
A neon number is a number where the sum of digits of square of
the number is equal to the number.
Example :
Given Number : 9
It is a Neon Number
Square of a Given Number : 9*9 = 81
Sum of Digit of a Square of a Given Number : 8+1 = 9
Since :
Sum of Digit of a Square of a Given Number is Equal To The Number (Given Number)
Given Number : 5
It is Not a Neon Number
Square of a Given Number : 5*5 = 25
Sum of Digit of a Square of a Given Number : 2+5 = 7
Since :
Sum of Digit of a Square of a Given Number is Not Equal To The Number (Given Number)
Source Code
// C Program To Check Automorphic Number Or Not ?
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num , Square , count =0, ncopy ;
printf("Enter a Number To Check : ");
scanf("%d",&num);
ncopy = num ;
Square = num * num ;
while( num!=0 )
{
num = num / 10 ;
count++;
}
if ( Square%(int)pow(10,count)==ncopy)
printf("It is an Automorphic Number . ");
else
printf("It is Not an Automorphic Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 25
Sample Output:
It is an Automorphic Number
Sample Input :
Enter a Number To Check : 6
Sample Output:
It is an Automorphic Number
Sample Input :
Enter a Number To Check : 7
Sample Output:
It is Not an Automorphic Number
Happy Number
C Program to Check Happy Number Or Not
In Number Theory, A Happy Number is a Number Which
Eventually reaches 1 when replaced by the sum of the square of each digit.
Example :
Given Number : 13
13 is a Happy Number Because
13 = ( 1*1 )+( 3*3 ) = 1 + 9 = 10
10 = ( 1*1 )+( 0*0 ) = 1 + 0 = 1
Given Number : 28
28 is a Happy Number Because
28 = ( 2*2 ) +( 8*8 ) = 4 + 64 = 68
68 = ( 6*6 ) +( 8*8 ) = 36+ 64 = 100
100= ( 1*1)+( 0*0 )+ ( 0*0 ) = 1 + 0 + 0 = 1
//C Program to Check Happy Number Or Not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,Rd,sum=0;
printf("Enter a Number To Check : ");
scanf("%d",&num);
while( num!=0 )
{
Rd = num % 10 ;
sum = sum + Rd * Rd ;
num = num / 10 ;
if( num==0 && sum>=10)
{
num=sum;
sum=0;
}
}
if( sum==1 )
printf("It is a Happy Number . ");
else
printf("It is Not a Happy Number . ");
getch();
}
Sample Input :
Enter a Number To Check : 28
Sample Output:
It is a Happy Number
Sample Input :
Enter a Number To Check : 12
Sample Output:
It is Not a Happy Number