C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print factorial of a number (loop)

Introduction

The factorial of a number n (written as n!) is the product of all positive integers less than or equal to n.

 C Programs

For example:

  • 5! = 5 × 4 × 3 × 2 × 1 = 120
  • 3! = 3 × 2 × 1 = 6

C Program: Print factorial of a number (loop)

NOTE: The Program is written using for, while and do..while loop.
Method 1 is for loop, Method 2 is while loop and Method 3 is do..while loop

Method 1: Using for loop


Factorial of a Number (Loop)


C

#include <stdio.h>

 

int main() {

    int n, i;

    long long fact = 1;

 

    // Input

    printf("Enter a number: ");

    scanf("%d", &n);

 

    // Check for negative number

    if (n < 0) {

        printf("Factorial is not defined for negative numbers.\n");

    } else {

        // Calculate factorial using for loop

        for (i = 1; i <= n; i++) {

            fact *= i;

        }

 

        // Display result

        printf("Factorial of %d = %lld\n", n, fact);

    }

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a number: 5
Factorial of 5 = 120

OUTPUT 2 :
Enter a number: 0
Factorial of 0 = 1

Explanation

  1. The user enters a number n.
  2. If n is negative, the program displays an error message.
  3. Otherwise, it initializes fact = 1 and multiplies it by all integers from 1 to n.
  4. The final product is the factorial of n.

 

C Program: Print factorial of a number (loop)

Method 2: Using while loop


C

#include <stdio.h>

 

int main() {

    int n, i = 1;

    long long fact = 1;

 

    // Input

    printf("Enter a number: ");

    scanf("%d", &n);

 

    // Check for negative numbers

    if (n < 0) {

        printf("Factorial is not defined for negative numbers.\n");

    } else {

        // While loop to calculate factorial

        while (i <= n) {

            fact *= i;

            i++;

        }

 

        // Display result

        printf("Factorial of %d = %lld\n", n, fact);

    }

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a number: 6
Factorial of 6 = 720

OUTPUT 2 :
Enter a number: 0
Factorial of 0 = 1

Explanation

  1. The user inputs an integer n.
  2. If n is negative → print an error message.
  3. Otherwise:
    • Start with i = 1 and fact = 1
    • While i <= n, multiply fact by i and increment i
    • When the loop finishes, print fact

 

C Program: Print factorial of a number (loop)

Method 3: Using do..while loop


C

#include <stdio.h>

 

int main() {

    int n, i = 1;

    long long fact = 1;

 

    // Input

    printf("Enter a number: ");

    scanf("%d", &n);

 

    // Check for negative number

    if (n < 0) {

        printf("Factorial is not defined for negative numbers.\n");

    } else {

        // Do-while loop to calculate factorial

        do {

            fact *= i;

            i++;

        } while (i <= n);

 

        // Display result

        printf("Factorial of %d = %lld\n", n, fact);

    }

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a number: 5
Factorial of 5 = 120

OUTPUT 2 :
Enter a number: 0
Factorial of 0 = 1

Explanation

  • The program initializes fact = 1 and i = 1.
  • The do-while loop multiplies fact by i and increments i in each iteration.
  • The loop executes at least once, even if n = 0.
  • For negative numbers, the program displays an error message.