Factorial Using Loops In C
C Programming Language Loops

Factorial Using Loops In C

In this article, we are going to discuss how to calculate the factorial using loops in C. We recently published an article C program to print bills using structures.  So, let’s start creating the program.

Factorial Using Loops In C:

In this section, we are going to discuss the logic of the program. Simply, take the number from the user and declare another variable f(factorial) and initialize it to 1 because we know that a factorial of 1 and 0 is equal to 1.

Next, we use a for loop which iterates from entered number to 1 because we know that factorial is a product of the integer and all the numbers below it. So, we simply multiply f and the loop variable i. This will calculate the factorial of any number you entered.

Program To Calculate Factorial Of A Number Using Loops:

#include<stdio.h>
int main(){
int num, f=1;
printf("Enter Any Number : ");
scanf("%d",&num);
for(int i=num; i>=1; i--){
f=f*i;
}
printf("Factorial of %d = %d",num,f);
}

 

Output:

Factorial Using Loops In C
Factorial Using Loops In C

 

 

Factorial Using While Loop In C:

#include<stdio.h>
int main(){
int num;
int f=1;
printf("Enter Any Number : ");
scanf("%d",&num);
int i =num;
while(i>=1){
f=f*i;
i--;
}
printf("Factorial Of %d is %d ",num,f);
}

Output:

 

From the above program and discussion, we conclude how to calculate factorial using the loops in c. We hope that you will understand the program easily.

Leave a Reply

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