🎯 Aim
To write a C/C++ program that takes a number as input from the user and calculates its factorial using the while loop.
🛠️ What You Need
- Visual Studio Code or any C/C++ compiler
- Knowledge of while loops and arithmetic operations
📖 Introduction
The factorial of a number (denoted by n!) is the product of all positive integers from 1 to n.
Formula: n! = n × (n-1) × (n-2) × ... × 1
For example:
- 5! = 5 × 4 × 3 × 2 × 1 = 120
- 4! = 4 × 3 × 2 × 1 = 24
- 3! = 3 × 2 × 1 = 6
- 0! = 1 (by definition)
You will use:
- scanf() — to take the number from the user
- A while loop — to multiply numbers from 1 to n
- A factorial variable — to store the result
- printf() — to display the result
💡 Tip: Factorial is used in many mathematical problems like permutations, combinations, and probability. The factorial of 0 is defined as 1.
📝 Steps
Step 1: Write the Program
- Open VS Code or any C compiler.
- Create a new file named factorial_while.c.
- Write the following code:
#include <stdio.h>
int main()
{
int num, i = 1;
long long factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0)
{
printf("❌ Factorial of a negative number does not exist.\n");
}
else
{
while (i <= num)
{
factorial = factorial * i;
i++;
}
printf("\n========== FACTORIAL RESULT ==========\n");
printf("Number: %d\n", num);
printf("Factorial: %d! = %lld\n", num, factorial);
printf("=======================================\n");
}
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- int num, i = 1; — Declares
num for user input and initializes i to 1.
- long long factorial = 1; — Declares
factorial as long long to handle large numbers, initialized to 1.
- scanf("%d", &num); — Reads the number from the user.
- if (num < 0) — Checks if the number is negative.
- while (i <= num) — The while loop multiplies numbers from 1 to num.
- factorial = factorial * i; — Multiplies the current value of factorial by i.
- i++; — Increments the counter.
Step 3: Save, Compile, and Run
- Save the file (Ctrl + S).
- Open the terminal and compile using the command:
gcc factorial_while.c -o factorial_while
./factorial_while
Sample Input & Output:
Enter a positive integer: 5
========== FACTORIAL RESULT ==========
Number: 5
Factorial: 5! = 120
=======================================
Enter a positive integer: 7
========== FACTORIAL RESULT ==========
Number: 7
Factorial: 7! = 5040
=======================================
Enter a positive integer: -3
❌ Factorial of a negative number does not exist.
Step 4: Modify the Program
- Using while loop to display multiplication steps: Print the multiplication process.
- Calculate factorial of numbers up to 20: Test with larger numbers.
- Use do-while loop: Replace while with do-while.
while (i <= num)
{
factorial = factorial * i;
printf("Step %d: %d! = %lld\n", i, i, factorial);
i++;
}
📊 While Loop Execution for Factorial
| Iteration |
i Value |
Condition i <= 5 |
Factorial Before |
Factorial After |
| 1 |
1 |
✅ True |
1 |
1 |
| 2 |
2 |
✅ True |
1 |
2 |
| 3 |
3 |
✅ True |
2 |
6 |
| 4 |
4 |
✅ True |
6 |
24 |
| 5 |
5 |
✅ True |
24 |
120 |
| 6 |
6 |
❌ False |
- |
(Loop ends) |
📊 Factorial Table
| n |
n! |
| 0 | 1 |
| 1 | 1 |
| 2 | 2 |
| 3 | 6 |
| 4 | 24 |
| 5 | 120 |
| 6 | 720 |
| 7 | 5040 |
| 8 | 40320 |
| 9 | 362880 |
| 10 | 3628800 |
| 11 | 39916800 |
| 12 | 479001600 |
| 13 | 6227020800 |
| 14 | 87178291200 |
| 15 | 1307674368000 |
| 16 | 20922789888000 |
| 17 | 355687428096000 |
| 18 | 6402373705728000 |
| 19 | 121645100408832000 |
| 20 | 2432902008176640000 |
📊 Alternative Methods
long long factorial = 1;
for (int i = 1; i <= num; i++)
{
factorial = factorial * i;
}
int i = 1;
long long factorial = 1;
do
{
factorial = factorial * i;
i++;
} while (i <= num);
long long factorial(int n)
{
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
📊 Observations
- The program successfully takes a number as input from the user.
- The
while loop iterates from 1 to num, multiplying each number.
- The factorial variable is initialized to 1 because multiplying by 1 doesn't change the result.
- The program handles negative numbers by displaying an error message.
- The factorial of 0 is 1 (handled by the loop not running).
- Using
long long allows the program to handle large factorial values (up to 20!).
✅ Result
You have successfully written a C program that calculates the factorial of a number entered by the user using the while loop. You have learned how to use loops to perform repetitive multiplication.
📌 Important: The factorial of a number grows very quickly. For numbers greater than 20, the factorial exceeds the range of long long data type. For very large numbers, you would need to use libraries that support big integers.
💡 Tip: You can also calculate factorial using recursion. Recursion is a technique where a function calls itself. The factorial function can be written as factorial(n) = n * factorial(n-1) with factorial(0) = 1 as the base case.
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو صارف سے ایک عدد ان پٹ کے طور پر لے اور while لوپ کا استعمال کرتے ہوئے اس کا فیکٹوریل حساب کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- while لوپ اور ریاضی کے عمل کی معلومات
📖 تعارف
کسی عدد کا فیکٹوریل (جسے n! سے ظاہر کیا جاتا ہے) 1 سے n تک کے تمام مثبت اعداد کی ضرب ہے۔
فارمولا: n! = n × (n-1) × (n-2) × ... × 1
مثال کے طور پر:
- 5! = 5 × 4 × 3 × 2 × 1 = 120
- 4! = 4 × 3 × 2 × 1 = 24
- 3! = 3 × 2 × 1 = 6
- 0! = 1 (تعریف کے مطابق)
💡 مشورہ: فیکٹوریل ریاضی کے بہت سے مسائل میں استعمال ہوتا ہے جیسے ترتیب، مجموعہ، اور احتمال۔ 0 کا فیکٹوریل 1 ہے۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- factorial_while.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
int num, i = 1;
long long factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0)
{
printf("❌ Factorial of a negative number does not exist.\n");
}
else
{
while (i <= num)
{
factorial = factorial * i;
i++;
}
printf("\n========== FACTORIAL RESULT ==========\n");
printf("Number: %d\n", num);
printf("Factorial: %d! = %lld\n", num, factorial);
printf("=======================================\n");
}
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- int num, i = 1; —
num صارف کے ان پٹ کے لیے اور i کو 1 سے شروع کرتا ہے۔
- long long factorial = 1; — بڑے اعداد کو ہینڈل کرنے کے لیے
factorial کو long long کے طور پر اعلان کرتا ہے، 1 سے شروع کرتا ہے۔
- scanf("%d", &num); — صارف سے عدد پڑھتا ہے۔
- if (num < 0) — چیک کرتا ہے کہ آیا عدد منفی ہے۔
- while (i <= num) — while لوپ 1 سے num تک اعداد کو ضرب کرتا ہے۔
- factorial = factorial * i; — factorial کی موجودہ قیمت کو i سے ضرب کرتا ہے۔
- i++; — کاؤنٹر کو بڑھاتا ہے۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc factorial_while.c -o factorial_while
./factorial_while
نمونہ ان پٹ اور آؤٹ پٹ:
Enter a positive integer: 5
========== FACTORIAL RESULT ==========
Number: 5
Factorial: 5! = 120
=======================================
📊 فیکٹوریل جدول
| n |
n! |
| 0 | 1 |
| 1 | 1 |
| 2 | 2 |
| 3 | 6 |
| 4 | 24 |
| 5 | 120 |
| 6 | 720 |
| 7 | 5040 |
| 8 | 40320 |
| 9 | 362880 |
| 10 | 3628800 |
📊 مشاہدات
- پروگرام کامیابی سے صارف سے ایک عدد ان پٹ کے طور پر لیتا ہے۔
while لوپ 1 سے num تک تکرار کرتا ہے، ہر عدد کو ضرب کرتا ہے۔
- factorial متغیر کو 1 سے شروع کیا گیا ہے کیونکہ 1 سے ضرب کرنے سے نتیجہ تبدیل نہیں ہوتا۔
- پروگرام منفی اعداد کو غلطی کا پیغام دکھا کر ہینڈل کرتا ہے۔
- 0 کا فیکٹوریل 1 ہے (لوپ کے نہ چلنے سے ہینڈل ہوتا ہے)۔
long long کا استعمال پروگرام کو بڑی فیکٹوریل ویلیوز (20 تک) کو ہینڈل کرنے کی اجازت دیتا ہے۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو صارف کے درج کردہ عدد کا while لوپ کا استعمال کرتے ہوئے فیکٹوریل حساب کرتا ہے۔ آپ نے بار بار ضرب کرنے کے لیے لوپ کا استعمال سیکھ لیا ہے۔
📌 اہم: کسی عدد کا فیکٹوریل بہت تیزی سے بڑھتا ہے۔ 20 سے بڑے اعداد کے لیے، فیکٹوریل long long ڈیٹا ٹائپ کی حد سے تجاوز کر جاتا ہے۔ بہت بڑے اعداد کے لیے، آپ کو بگ انٹیجرز کو سپورٹ کرنے والی لائبریریاں استعمال کرنی ہوں گی۔
💡 مشورہ: آپ ریکرسن کا استعمال کرتے ہوئے بھی فیکٹوریل حساب کر سکتے ہیں۔ ریکرسن ایک ایسی تکنیک ہے جہاں ایک فنکشن خود کو کال کرتا ہے۔ فیکٹوریل فنکشن کو factorial(n) = n * factorial(n-1) کے طور پر لکھا جا سکتا ہے جس میں factorial(0) = 1 بنیادی کیس ہے۔