Q1 Write a function to calculate the factorial value of any integer entered through the keyboard.

Write a function to calculate the factorial value of any integer entered through the keyboard.

Program: 122

Write a function in c language to calculate the factorial value of any integer entered through the keyboard.

#include<stdio.h>
#include<conio.h>
int fact(int num);  //function prototype declaration
int main()
{
    int n, fac;
    printf("Enter an integer: ");
    scanf("%d", &n);

    fac = fact(n); //function call

    printf("The factorial of %d is %d", n, fac);

}

//factorial function
int fact(int num)   //function definition
{
    int res;

    res = num;
    while(num>1)
    {
        res = res*(num-1);
        num = num-1;
    }
    return res;
}

 

Output:

Enter an integer: 5
The factorial of 5 is 120

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.
Related Post
Leave a Comment

This website uses cookies.