6️⃣C Program to Find Factorial of a Number(With Program and Explanation)

 ๐Ÿ“Œ C Program to Find Factorial of a Number

(With Program and Explanation)

๐Ÿ”น What is Factorial?

The factorial of a number n is:

n! = n × (n−1) × (n−2) × ... × 1

Example:

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


๐Ÿ’ป C Program Code

#include <stdio.h> int main() { int num, i; long long factorial = 1; printf("Enter a number: "); scanf("%d", &num); // Check for negative number if (num < 0) { printf("Factorial of a negative number is not possible.\n"); } else { for (i = 1; i <= num; i++) { factorial = factorial * i; } printf("Factorial of %d is %lld\n", num, factorial); } return 0; }

๐Ÿง  How the Program Works

  1. User enters a number

  2. If the number is negative, factorial is not defined

  3. Loop multiplies numbers from 1 to n

  4. Result is stored in factorial


▶️ Sample Output

Enter a number: 6 Factorial of 6 is 720

✅ Conclusion

In this program, we learned how to calculate the factorial of a number using loops in C.


๐Ÿ“˜ C Programming Series – What’s Next?

๐Ÿ‘‰ In the next post of this series, we will learn how to find the sum of digits of a number in C. with complete explanation and output examples. Stay tuned! ๐Ÿš€

Comments

Popular posts from this blog

⓮C Program to calculate Simple Interest (With Program and Explanation)

⑫ C Program to Swap Two Numbers Without Using a Temporary Variable (With Program and Explanation)

9️⃣C Program to Find LCM of Two Numbers (With Program and Explanation)