C Program to Calculate Factorial of a Given Number दिए गए Number का Factorial Calculate करने का C Program

The factorial of a positive integer is the product of all positive integers from 1 to that number. It is represented by the symbol !. For example:

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

किसी positive integer का factorial, 1 से लेकर उस number तक सभी positive integers के product के बराबर होता है। इसे ! symbol से दर्शाया जाता है।

C Program

#include <stdio.h>
int main(void)
{
int n, i;
unsigned long long fact = 1;
printf("Enter a non-negative integer number\n");
if (scanf("%d", &n) != 1)
{
printf("Invalid input! Please enter an integer.\n");
return 1;
}
if (n < 0)
{
printf("Wrong input! Enter a non-negative integer.\n");
return 1;
}
if (n > 20)
{
printf("Number is too large to calculate factorial.\n");
return 1;
}
for (i = 1; i <= n; i++)
{
fact = fact * i;
}
printf("Factorial of %d = %llu\n", n, fact);
return 0;
}

Output

Example 1:

Enter a non-negative integer number
5
Factorial of 5 = 120

Example 2:

Enter a non-negative integer number
0
Factorial of 0 = 1

Example 3:

Enter a non-negative integer number
-4
Wrong input! Enter a non-negative integer.

Explanation

  • fact = 1 initializes the factorial variable.

  • scanf() accepts the integer from the user.

  • n < 0 checks for a negative number.

  • The for loop multiplies fact by each integer from 1 to n.

  • 0! is equal to 1.

  • unsigned long long can store factorial values up to 20! on typical modern C implementations.

fact = 1 factorial variable को initialize करता है। for loop 1 से लेकर n तक प्रत्येक integer को fact से multiply करता है। Mathematics के अनुसार 0! का value भी 1 होता है।

Modern C Compatibility Note

This program uses standard C and is compatible with modern compilers such as GCC, Clang, and Visual Studio C. It does not require conio.h, clrscr(), or getch().

This program demonstrates how to calculate the factorial of a number using a for loop in C, along with basic input validation.

यह program C भाषा में for loop का उपयोग करके किसी number का factorial calculate करना और basic input validation करना सिखाता है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025