How to write a C program to find factorial of a given number

Factorial of a number: A factorial of a number n, denoted by n!, is the product of all positive integers less than or equal to n

example: factorial of 4 is : 4! = 4x3x2x1 = 24 . similarly factorial of 3 is: 3!=3x2x1=6 and factorial of 6 is: 6! = 6x5x4x3x2x1 = 720

Factorial of a number in c language can be calculated using for loops for and while
and also using recursion technique

1>Write a C program to find factorial of a given number using while loop

Write a program to print factorial of a given number using c programming

#include<stdio.h>
void main()
{
int num, c , factorial; // c is count variable
printf(“To find factorial of a given number \n”);
printf(“Enter the number :”);
scanf (“%d \n”, &num);
c =1;
factorial=1;
while ( num > 1)
{
c++ ;
factorial=factorial*c;
num= num-1;
}
printf(“The factorial of the entered number is:%d”, factorial);
}

OUTPUT:

To find factorial of a given number

Enter the number : 5

The factorial of the entered number is 120

2>WAP to calculate factorial of a given number using for loop using c programming

Write a program to find factorial of a given number using for loop

#include<stdio.h>
void main()
{
int c , factorial=1, i; // c is number
printf(“To calculate factorial of a given number \n”);
printf(“Enter the number :”);
scanf (“%d \n”, &c);
for(i=2;i<=c;i++)
{
factorrial=factorial*i;
}
printf(“The factorial of the number %d is:%d”, c , factorial);
}

OUTPUT:

To calculate factorial of a given number

Enter the number : 5

The factorial of number 5 is 120

3>Write a C program to calculate factorial of a given number using factorial function

WAP to find factorial of a given number using factorial function using c programming

#include<stdio.h>
void main()
{
int n;
printf(“To show factorial of a given number \n”);
printf(“Enter the number whose factorial is to be calculated:”);
scanf (“%d \n”, &n);
printf(“The factorial of the entered number is:”);
int findfactorial(n);
}

findfactorial(int c )
{
int factorial=1, i;
for(i=2;i<=c;i++)
{
factorial=factorial*i;
}
return(factorial);
}

OUTPUT:

To show factorial of a given number

Enter the number whose factorial is to be calculated: 5

The factorial of the entered number is 120

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>