A Program to display factorial of specified number using for loop in C

A program to find factorial for integer data-type using for loop in C - language

Algorithm for Factorial

Step 1: Start
Step 2: Read a number n Step 2: Initialize variables: i = 1, result = 1
Step 3: if i <= n go to step 4 otherwise go to step 7
Step 4: Calculate result = result * i
Step 5: Increment the i by 1 (i = i + 1) and go to step 3
Step 6: Display result Step 7: Stop


Working example

/**
 * Factorial For N Using For Loop
 */
#include <stdio.h>
int main()
{
    int i, n, result = 1;
    printf(" Enter number: ");
    scanf("%d", &number);

    for (i = 1; i <= number; i += 1)
    {
        // printf("\n I : %d", i);
        // printf("\n Result B4 : %d", result);
        result *= i;
        // printf("\n Result After : %d", result);
    }
    printf("\n Factorial of %d : %d", number, result);
    printf("\n");
    return 0;
}

Output for number 5

Enter number: 5

Factorial of 5 : 120

Output for number 6

Enter number: 6

Factorial of 5 : 720
Avatar

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳

Next
Previous

Related