Finding the factorial of a number is a common problem in computer programming. A factorial of a number is the product of all positive integers up to and including that number. For example, the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.
In C programming, we can write a program to find the factorial of a number using a loop. Here's an example program:
c#include <stdio.h>
int main() {
int num;
unsigned long long fact = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
for(int i=1; i<=num; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu\n", num, fact);
return 0;
}
In this program, we first declare the variables num
and fact
. We then prompt the user to enter a positive integer using the printf()
and scanf()
functions. Note that we use the unsigned long long
data type for the variable fact
because the factorial of a large number can become very large, and using unsigned long long
provides enough range to handle such cases.
We then use a for
loop to calculate the factorial of the number entered by the user. The loop starts from 1 and goes up to num
, multiplying each integer along the way to the fact
variable.
Finally, we print the factorial using the printf()
function.
Let's step through the program and understand how it works.
We include the
stdio.h
header file, which contains the declarations of standard input/output functions.We declare two variables
num
andfact
, wherenum
stores the number for which we need to find the factorial, andfact
stores the factorial value.We prompt the user to enter a positive integer using the
printf()
function, which displays the message "Enter a positive integer: " on the screen.We use the
scanf()
function to read the integer entered by the user and store it in thenum
variable.We use a
for
loop to calculate the factorial of the number entered by the user. The loop starts from 1 and goes up tonum
. In each iteration of the loop, we multiply the current value offact
with the loop counteri
. Since we startfact
with a value of 1, the first iteration multiplies 1 with 1, resulting infact
being equal to 1. The second iteration multiplies 1 with 2, resulting infact
being equal to 2. The third iteration multiplies 2 with 3, resulting infact
being equal to 6. This process continues until the loop counteri
reaches the value ofnum
.Finally, we print the factorial of the number entered by the user using the
printf()
function. The%d
format specifier is used to display the value ofnum
, and the%llu
format specifier is used to display the value offact
.
When we run this program and enter a positive integer, it calculates the factorial of that number and displays the result on the screen. If the user enters a negative integer, the program will still execute but will give an incorrect result, as the factorial of a negative number is not defined. If the user enters a very large number, the program may take a long time to execute or may run out of memory, as the factorial of a large number can become very large.
Comments
Post a Comment