Write a c program to print factorial of a number

Ram Pothuraju

 #include <stdio.h>


int main()

{

    int n, i;

    unsigned long long factorial = 1;


    printf("Enter a positive integer: ");

    scanf("%d", &n);


    // Computing the factorial of n

    for (i = 1; i <= n; ++i)

    {

        factorial *= i;

    }


    printf("Factorial of %d = %llu", n, factorial);


    return 0;

}




In this program, we first take a positive integer as input from the user. Then, we initialize a variable factorial to 1 and loop through all the numbers from 1 to n, multiplying each number by the previous value of factorial. Finally, we print the computed value of factorial.

Note that we use an unsigned long long data type for factorial because factorials of larger numbers can quickly exceed the range of standard integer data types.
Tags

Post a Comment

0Comments

Post a Comment (0)