This C program calculates the factorial of a given number entered by the user. The main function is returning an integer value of 0, which indicates that the program has executed successfully.
#include<stdio.h>
int main(){
int n, fact = 1;
printf("Enter the number : ");
scanf("%d", &n);
for(int i = 1; i <= n; i++){
fact *= i;
}
printf("Factorial : %d", fact);
return 0;
}
#include<stdio.h>
Includes the standard libarary for using the input/output inbuild operation.
int main(){
Entry point of the program, whcih called in c program main() function.
int n, fact = 1;
Declared n as integer and set fact = 1 as integer.
printf("Enter the Number : ");
Prompt to the entter the number using the printf() function.
scanf("%d", &n);
Reads n from the user using the scanf() function which include in stdio.h library.
for(int i = 1; i <= n; i++){
fact *= i;
}
Factorial of any number is the multiplication from to n which called the factorial. Here we performs the same logic using the for loop, which iterates from 1 to n (which given from the user) andd in the loop body fact = i (fact = fact * i) multiple from 1 to n and store the result in fact variable.
printf("Factorial : %d", fact);
After calculate the factorial print the factorial on the console/termiinal.
return 0;
}
At the end return 0, which means that program is successfully executed.
Comments
Oops!