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
Leave a Comment