Coding Practice

Write a C program to calculate factorial of a number

Calculate factorial of a number
Sample Output
Calculate factorial of a number.

Number: 5

Factorial of 5 = 120
Source Code
#include<stdio.h>

int main()
{
    int n, num, fact = 1;

    printf("Calculate factorial of a number.\n\nNumber: ");
    scanf("%d", &num);

    for(n = 1; n <= num; n++)
    {
        fact = fact * n;
    }
    printf("\nFactorial of %d = %d\n", num, fact);

    return 0;
}
Sample Output
Calculate factorial of a number.

Number: 4

Factorial of 4 = 24

No comments:

Post a Comment

Change Theme
X