Factorial program in c using while loop
In this post, you will learn how to find the factorial of a number in the C programming language using a while loop.
The factorial is normally used in different areas of mathematics, including algebra, mathematical analysis, and combinations and permutations. The factorial of a number n is denoted by n!. This is the product of all positive numbers less than or equal to n. It is calculated as-
n! = n X (n-1) X (n-3) X ……… X 3 X 2 X 1
An example of factorial
Suppose we want to get a factorial of 7, then the factorial will be-
7! = 7 X 6 X 5 X 4 X 3 X 2 X 1
5040
Factorial program in C using while loop
In the given C program, we ask the user to enter a number and store it in a variable num. The while loop continues till the value of num greater than or equal to the value of i. Then within the loop, the value of 'i' is multiplied with the value of 'fact'. Finally, we get the factorial value of the given number.
#include <stdio.h>
int main()
{
int i,fact=1,num;
fact=i=1;
printf("Enter a number: ");
scanf("%d",&num);
while(i<=num)
{
fact*=i;
i++;
}
printf("The Factorial of %d : %d",num,fact);
return 0;
}
Output of the above code:
Enter a number: 7
The Factorial of 7 : 5040
Enter a number: 11
The Factorial of 11 : 39916800
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative