Problem 17

author
By Supreme2023-03-15
Problem Statement :
Write a C program to find factorial of a given number.
Code:

#include <stdio.h>

int main(){

int i,n,fact;

printf("\t\tFactorial");
printf("\n================================");


printf("\n\nEnter The Number For Factorial : ");
scanf("%d", &n);

printf("\n_______________________________________________________");
for(i=1;i<=n;i++){
fact *=i;
}

printf("\n\nFactorial Of %d is : %d ",n,fact);
printf("\n_______________________________________________________\n\n");

return 0;
}

Description :
  • This is a C program that calculates the factorial of a given number. The program prompts the user to input an integer number and then uses a for loop to calculate its factorial. The variable 'i' is initialized to 1, and the variable 'fact' is initialized to 1. The for loop continues until 'i' is less than or equal to 'n', and for each iteration, 'fact' is multiplied by 'i'. At the end of the loop, the program prints the result of the factorial calculation to the console along with a message. The program does not return a value, so it should end with return 0; to indicate successful execution.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.