Showing posts with label factorial. Show all posts
Showing posts with label factorial. Show all posts

Friday, 30 December 2011

C program to check whether a number is a strong number



#include<stdio.h>
int fact(int);
main()
{
int n,t,r,s;
printf("Enter any number\n");
scanf("%d",&n);
t=n;
s=0;
while(n>0)
{
r=n%10;
s=s+fact(r);
n=n/10;
}
if(t==s)
printf("\n%d is a strong number",t);
else
printf("\n%d is not a strong number",t);
printf("\n");
}
int fact(int a)
{
int f,g;
f=1;
for(g=a;g>0;g--)
f=f*g;
return(f);
}



Wednesday, 28 December 2011

C program to find the factorial of a number using recursion



#include<stdio.h>
int fact(int);
main()
{
int n,temp;
printf("Enter a number\n");
scanf("%d",&n);
if(n>=0)
{
temp=fact(n);
printf("Factorial is %d",temp);
}
else
printf("Cannot find the factorial");
}

int fact(int c)
{
if(c==0)
return(1);
else
return(c*fact(c-1));
}