This is a C Language program where a number along with choice are entered by user.
According to the choice, the program either print
1. Factors of the number
2. Factorial value of the number
3. Whether the number is prime or not
4. Whether the entered number is perfect or not
#include< stdio.h>
void per(int n)
{
int i,s=0;
for(i=1;i< n;i++)
{
if(n%i==0)
s+=i;
}
if(s==n)
printf("%d is a perfect number.",n);
else
printf( "%d is not a perfect number.",n);
}
void pri(int n)
{
int i;
for(i=2;i< n;i++)
{
if(n%i==0)
break;
}
if(i==n)
printf("%d is a prime number.",n);
else
printf( "%d is not a prime number.",n);}
void fact(int n)
{
int i;
printf("\nFactors of %d are=",n);
for(i=1;i< n;i++)
{
if(n%i==0)
printf(" %d",i);
}
}
void facto(int n)
{
int i,f=1;
for(i=2;i<=n;i++)
{
f=f*i;
}
printf("\nFactorial value =%d",f);
}
void main()
{
int i,n;
printf("Enter the number:");
scanf("%d",&n);
printf("Enter the Choice ('1' for showing factors, '2' for factorial , '3' for prime checking and '4' for perfect number checking:");
scanf("%d",&i);
if(i==1)
fact(n);
else if(i==2)
facto(n);
else if(i==3)
pri(n);
else if(i==4)
per(n);
else
printf("\nWrong Choice..");
getch();
}
No comments:
Post a Comment