Calculating Factorials: Harnessing Loops in C Programming
#include <stdio.h>
int main()
{
int n;
// Prompt user to enter a number
printf("Enter a number: ");
scanf("%d", &n);
int factorial = 1; // Initialize factorial to 1
// Loop to calculate the factorial of the entered number
for (int i = 1; n >= i; i++)
{
factorial = factorial * i; // Calculate the factorial incrementally
}
// Display the calculated factorial
printf("Factorial of %d is %d", n, factorial);
return 0;
}
// Blog Title: "Calculating Factorials: Harnessing Loops in C Programming"
// Keywords: C programming, loops, factorial calculation, iterative computation, mathematical operations
Comments
Post a Comment