🎯 Aim
To write a C/C++ program that adds all numbers from 1 to 100 using the while loop and prints the sum at the end.
🛠️ What You Need
- Visual Studio Code or any C/C++ compiler
- Knowledge of while loops and sum accumulation
📖 Introduction
In this practical, you will calculate the sum of all numbers from 1 to 100 using a while loop. The sum of numbers from 1 to 100 is a well-known mathematical problem. The result is 5050 (by the formula n(n+1)/2, where n = 100).
The logic is:
- Initialize a counter variable to 1
- Initialize a sum variable to 0
- While the counter is <= 100, add the counter to sum
- Increment the counter by 1 each iteration
- Print the final sum after the loop ends
💡 Tip: The sum of numbers from 1 to n is given by the formula n * (n + 1) / 2. For n = 100, the sum is 5050.
📝 Steps
Step 1: Write the Program
- Open VS Code or any C compiler.
- Create a new file named sum_1_to_100.c.
- Write the following code:
#include <stdio.h>
int main()
{
int i = 1;
int sum = 0;
while (i <= 100)
{
sum = sum + i;
i++;
}
printf("========== SUM RESULT ==========\n");
printf("Sum of numbers from 1 to 100 = %d\n", sum);
printf("================================\n");
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- int i = 1; — Declares and initializes the counter variable to 1.
- int sum = 0; — Declares and initializes the sum variable to 0.
- while (i <= 100) — The while loop runs as long as
i is less than or equal to 100.
- sum = sum + i; — Adds the current value of
i to the sum.
- i++; — Increments the counter by 1 after each iteration.
- printf(...) — Displays the final sum after the loop ends.
Step 3: Save, Compile, and Run
- Save the file (Ctrl + S).
- Open the terminal and compile using the command:
gcc sum_1_to_100.c -o sum_1_to_100
./sum_1_to_100
Sample Output:
========== SUM RESULT ==========
Sum of numbers from 1 to 100 = 5050
================================
Step 4: Modify the Program
- Change the range: Change
i <= 100 to i <= 50 to find the sum of 1 to 50.
- Take user input: Ask the user for the limit and calculate the sum.
- Print each step: Print the sum after each addition to see how it accumulates.
int limit, i = 1, sum = 0;
printf("Enter the limit: ");
scanf("%d", &limit);
while (i <= limit)
{
sum = sum + i;
i++;
}
printf("Sum of numbers from 1 to %d = %d\n", limit, sum);
while (i <= 10)
{
sum = sum + i;
printf("After adding %d, sum = %d\n", i, sum);
i++;
}
📊 While Loop Execution (Step-by-Step)
| Iteration |
i Value |
Condition i <= 100 |
Sum Before |
Sum After |
| 1 | 1 | ✅ True | 0 | 1 |
| 2 | 2 | ✅ True | 1 | 3 |
| 3 | 3 | ✅ True | 3 | 6 |
| 4 | 4 | ✅ True | 6 | 10 |
| 5 | 5 | ✅ True | 10 | 15 |
| ... | ... | ... | ... | ... |
| 99 | 99 | ✅ True | 4851 | 4950 |
| 100 | 100 | ✅ True | 4950 | 5050 |
| 101 | 101 | ❌ False | - | (Loop ends) |
📊 Formula Verification
| Method |
Formula |
Result |
| While Loop |
Sum of 1 to 100 |
5050 |
| Mathematical Formula |
n * (n + 1) / 2 |
100 × 101 / 2 = 5050 |
📊 Alternative Methods
int sum = 0;
for (int i = 1; i <= 100; i++)
{
sum = sum + i;
}
printf("Sum = %d", sum);
int i = 1, sum = 0;
do
{
sum = sum + i;
i++;
} while (i <= 100);
printf("Sum = %d", sum);
int n = 100;
int sum = n * (n + 1) / 2;
printf("Sum = %d", sum);
📊 Observations
- The
while loop successfully adds numbers from 1 to 100.
- The sum variable starts at 0 and accumulates the total.
- The counter
i starts at 1 and goes up to 100.
- After the loop ends, the sum is 5050.
- The result matches the mathematical formula
n(n+1)/2.
✅ Result
You have successfully written a C program that adds all numbers from 1 to 100 using the while loop and prints the sum at the end. You have learned how to use loops to perform repetitive calculations and accumulate results.
📌 Important: Always initialize the sum variable to 0 before adding values. If you forget to increment the counter, the loop will run forever (infinite loop).
💡 Tip: You can use this same logic to calculate the sum of any range of numbers by changing the starting value and the condition in the while loop.
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو 1 سے 100 تک کے تمام اعداد کا جمع while لوپ کا استعمال کرتے ہوئے حساب کرے اور آخر میں جمع پرنٹ کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- while لوپ اور جمع جمع کرنے کی معلومات
📖 تعارف
اس عملی میں، آپ while لوپ کا استعمال کرتے ہوئے 1 سے 100 تک کے تمام اعداد کا جمع حساب کریں گے۔ 1 سے 100 تک کے اعداد کا جمع ایک معروف ریاضیاتی مسئلہ ہے۔ نتیجہ 5050 ہے (فارمولا n(n+1)/2 کے مطابق، جہاں n = 100)۔
منطق یہ ہے:
- کاؤنٹر کو 1 سے شروع کریں
- sum کو 0 سے شروع کریں
- جب تک کاؤنٹر <= 100 ہے، کاؤنٹر کو sum میں شامل کریں
- ہر تکرار کے بعد کاؤنٹر کو 1 بڑھائیں
- لوپ ختم ہونے کے بعد آخری جمع پرنٹ کریں
💡 مشورہ: 1 سے n تک کے اعداد کا جمع فارمولا n * (n + 1) / 2 سے دیا جاتا ہے۔ n = 100 کے لیے، جمع 5050 ہے۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- sum_1_to_100.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
int i = 1;
int sum = 0;
while (i <= 100)
{
sum = sum + i;
i++;
}
printf("========== SUM RESULT ==========\n");
printf("Sum of numbers from 1 to 100 = %d\n", sum);
printf("================================\n");
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- int i = 1; — کاؤنٹر متغیر کا اعلان اور 1 سے شروع کرتا ہے۔
- int sum = 0; — sum متغیر کا اعلان اور 0 سے شروع کرتا ہے۔
- while (i <= 100) — while لوپ اس وقت تک چلتا ہے جب تک
i 100 سے کم یا برابر ہے۔
- sum = sum + i; —
i کی موجودہ قیمت کو sum میں شامل کرتا ہے۔
- i++; — ہر تکرار کے بعد کاؤنٹر کو 1 بڑھاتا ہے۔
- printf(...) — لوپ ختم ہونے کے بعد آخری جمع ظاہر کرتا ہے۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc sum_1_to_100.c -o sum_1_to_100
./sum_1_to_100
نمونہ آؤٹ پٹ:
========== SUM RESULT ==========
Sum of numbers from 1 to 100 = 5050
================================
📊 while لوپ کا مرحلہ وار عمل
| تکرار |
i کی قیمت |
شرط i <= 100 |
جمع (پہلے) |
جمع (بعد میں) |
| 1 | 1 | ✅ درست | 0 | 1 |
| 2 | 2 | ✅ درست | 1 | 3 |
| 3 | 3 | ✅ درست | 3 | 6 |
| 4 | 4 | ✅ درست | 6 | 10 |
| 5 | 5 | ✅ درست | 10 | 15 |
| ... | ... | ... | ... | ... |
| 99 | 99 | ✅ درست | 4851 | 4950 |
| 100 | 100 | ✅ درست | 4950 | 5050 |
| 101 | 101 | ❌ غلط | - | (لوپ ختم) |
📊 مشاہدات
while لوپ کامیابی سے 1 سے 100 تک کے اعداد کو جمع کرتا ہے۔
- sum متغیر 0 سے شروع ہوتا ہے اور کل جمع کو جمع کرتا ہے۔
- کاؤنٹر
i 1 سے شروع ہوتا ہے اور 100 تک جاتا ہے۔
- لوپ ختم ہونے کے بعد، جمع 5050 ہے۔
- نتیجہ ریاضیاتی فارمولا
n(n+1)/2 سے مماثل ہے۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو while لوپ کا استعمال کرتے ہوئے 1 سے 100 تک کے تمام اعداد کا جمع حساب کرتا ہے اور آخر میں جمع پرنٹ کرتا ہے۔ آپ نے بار بار حسابات کرنے اور نتائج جمع کرنے کے لیے لوپ کا استعمال سیکھ لیا ہے۔
📌 اہم: اقدار کو شامل کرنے سے پہلے ہمیشہ sum متغیر کو 0 سے شروع کریں۔ اگر آپ کاؤنٹر کو بڑھانا بھول گئے تو لوپ ہمیشہ کے لیے چلتا رہے گا (لامحدود لوپ)۔
💡 مشورہ: آپ ابتدائی قیمت اور while لوپ میں شرط کو تبدیل کرکے اعداد کی کسی بھی رینج کا جمع حساب کرنے کے لیے اسی منطق کا استعمال کر سکتے ہیں۔