Code 1: Factorial program in c
using for loop
#include<stdio.h>
int main()
{
int i,fact=1,num;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
fact=fact*i;
printf("Factorial of the number is: %d",fact);
return 0;
}
Sample output:
Enter a number:4
Factorial of the number is: 24
____________________________________________________________
Code 2:Factorial program in c
using function
#include<stdio.h>
int factorial(int);
int main(){
int i,fact,num;
printf("Enter a number: ");
scanf("%d",&num);
fact = factorial(num);
printf("Factorial of the number is: %d"fact);
return 0;
}
int factorial(int num){
int i,f=1;
for(i=1;i<=num;i++)
f=f*i;
return f;
}
output:
Enter a number:4
Factorial of the number is: 24
Code 3:C program to find the
factorial of a given number
#include<stdio.h>
int main()
{
int i=1,fact=1,num;
printf("Enter a number: ");
scanf("%d",&num);
while(i<=num)
{
fact=fact*i;
i++;
}
printf("Factorial of the number is: %d",fact);
return 0;
}
Sample output:
Enter a number:4
Factorial of the number is: 24
Code 4:Factorial program in c
using pointers
#include<stdio.h>
void factorial(int,int *);
int main()
{
int i,fact,num;
printf("Enter a number: ");
scanf("%d",&num);
factorial(num,&fact);
printf("Factorial of the number is: %d",*fact);
return 0;
}
void factorial(int num,int *factorial)
{
int i;
*fact
=1;
for(i=1;i<=num;i++)
*fact=*fact*i;
}
output:
Enter a number:4
Factorial of the number is: 24
____________________________________________________________
Code 5: Program for Factorial series in c
#include<stdio.h>
int main()
{
long f=1;
int i,num,min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Factorial series in given range is: ");
for(num=min;num<=max;num++){
f=1;
for(i=1;i<=num;i++)
f=f*i;
printf("%ld
",f);
}
return 0;
}
output:
Enter the minimum range: 1
Enter the maximum range: 5
Factorial series in given range is: 1 2 6 24 120
No comments:
Post a Comment