A for loop can be used to calculate and display the factorial of numbers from 1 to 7. The factorial of a number is the product of all positive integers from 1 up to that number. In this program, the outer for loop selects each number from 1 to 7, while the inner for loop calculates its factorial. The result is displayed for every number.
How It Works?
i, j, and fact.for loop to generate numbers from 1 to 7.fact = 1.for loop to multiply fact by every number from 1 to the current value of i.fact.#include <stdio.h> #include <conio.h> main() { int i, j; long int fact; clrscr(); printf("Number\tFactorial\n\n"); for(i = 1; i <= 7; i = i + 1) { fact = 1; for(j = 1; j <= i; j = j + 1) { fact = fact * j; } printf("%d\t%ld\n", i, fact); } getch(); }
for loop to display the factorial of numbers from 1 to 5.for loops to calculate and display the factorial of multiple numbers.for loop from 1 to 5. Before each calculation, initialize fact = 1. Use an inner for loop to multiply the numbers from 1 to the current value of i.#include <stdio.h> #include <conio.h> main() { int i, j; long int fact; clrscr(); printf("Number\tFactorial\n\n"); for(i = 1; i <= 5; i = i + 1) { fact = 1; for(j = 1; j <= i; j = j + 1) { fact = fact * j; } printf("%d\t%ld\n", i, fact); } getch(); }
for loop to generate numbers from 1 to 5. For each number, the variable fact is initialized to 1. An inner for loop multiplies all numbers from 1 to the current value of i. After the factorial is calculated, the program displays the number along with its factorial.