5 PROGRAM TO FIND FACTORIAL
1
#include <stdio.h>
#include <conio.h>
void main()
{
int n,fact=1,i;
printf("Enter a number");
scanf("%d",&n);
if(n==0||n==1)
{
fact=1;
printf("The factorial of %d is %d",n,fact);
}
else
{
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The factorial of %d is %d",n,fact);
}
getch();
}
Output:
Enter a number5
The factorial of 5 is 120
52 PROGRAM TO GENERATE N FIBONACCI SERIES
#include <stdio.h>
#include <conio.h>
void main()
{
int first=-1,second=1,n,third;
printf("Enter the no. of digits to be printed\n");
scanf("%d",&n);
printf("\nThe fibonnaci series is : ");
for(int i=0;i<n;i++)
{
third=first+second;
printf("%d ",third);
first=second;
second=third;
}
}
OUTPUT:
Enter the no. of digits to be printed
4
The fibonnaci series is : 0 1 1 2
5 PROGRAM TO GENERATE N PRIME NUMBER
3
#include <stdio.h>
main()
{
int n,i,count=0,num;
printf("Enter the n value");
scanf("%d",&n);
printf("The prime number in between the range 1 to %d:\n",n);
for(num=1;num<=n;num++)
{
count=0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count==0 && num!=1)
{
printf("%d\t",num);
}
}
}
OUTPUT:
Enter the n value5
The prime number in between the range 1 to 5:
2 3 5
5 PROGRAM TO CHECK PRIME OR
4 COMPOSITE
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,count=0;
printf("Enter a number:");
scanf("%d",&num);
if(num==0 || num==1)
{
printf("\n%d is neither prime nor composite",num);
}
else if(num==2)
{
printf("\n%d is even prime number ",num);
}
else
{
for(i=1;i<=num;i++)
{
if(num%i==0)
{
count++;
}
}
if(count==2)
{
printf("\n%d is a prime number",num);
}
else
{
printf("\n%d is not a prime number",num);
}
}
}
OUTPUT:
Enter a number:5
5 is a prime number
Program to check a number is Armstrong
55 number or not
#include <stdio.h>
#include <conio.h>
void main()
{
int num,rem,n,sum=0,x,i;
printf("Enter the number to check if it is armstrong number or not:");
scanf("%d",&num);
for(i=num;i!=0;n++)
{
i/=10;
}
for(i=num;i!=0;i/=10)
{
rem=i%10;
sum+=pow(rem,n);
}
if(sum==num)
{
printf("\nThe number %d is a armstrong number",num);
}
else{
printf("\nThe number %d is not a armstrong number",num);
}
}
OUTPUT:
Enter the number to check if it is armstrong number or not:153
The number 153 is a armstrong number
Program check whether a given number is
56
perfect number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,rem,x,i;
printf("Enter a number:");
scanf("%d",&a);
for(i=1;i<a;i++)
{
rem=a%i;
if(rem==0)
{
x=x+i;
}
}
if(a==x)
{
printf("\nThe entered number is a perfect number");
}
else{
printf("\nThe entered number is not a perfect number");
}
}
OUTPUT:
Enter a number:6
The entered number is a perfect number
57 Program to accept a 5 digit number from user and
perform Sum of 5 digits
#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum,rem;
printf("Enter a number:");
scanf("%d",&num);
while(num!=0)
{
rem=num%10;
num=num/10;
sum=sum+rem;
}
printf("%d is the sum of the digits",sum);
}
OUTPUT:
Enter a number:11111
5 is the sum of the digits
58 Program to Reverse a Number
#include<stdio.h>
#include<conio.h>
void main()
{
int a,rem,rev,dummy;
printf("Enter a number :");
scanf("%d",&a);
dummy=a;
while(a!=0)
{
rem=a%10;
rev=rev*10+rem;
printf("rem:%d\n",rev);
a=a/10;
}
if(dummy==rev)
{
printf("\nThe entered number is a palindrome");
}
else{
printf("\nThe entered number is not a palindrome");
}
}
OUTPUT
Enter a number :5225
rem:5
rem:52
rem:522
rem:5225
The entered number is a palindrome
HCF and LCM of two numbers
59
61 Count the no.of digits in a number
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,count;
printf("Enter the number:");
scanf("%d",&n);
while(n>0)
{
n=n/10;
count++;
}
printf("The no.of digits is %d",count);
}