EZ Notes Logo

EZNotes

🎯 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

  1. Open VS Code or any C compiler.
  2. Create a new file named sum_1_to_100.c.
  3. Write the following code:
/* Practical 42: Program to add numbers 1-100 using while loop */ #include <stdio.h> int main() { int i = 1; // Counter variable int sum = 0; // Sum accumulator /* ============================================ Using while loop to add numbers from 1 to 100 ============================================ */ while (i <= 100) { sum = sum + i; // Add current number to sum i++; // Increment the counter } // Display the result 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

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int i = 1; — Declares and initializes the counter variable to 1.
  3. int sum = 0; — Declares and initializes the sum variable to 0.
  4. while (i <= 100) — The while loop runs as long as i is less than or equal to 100.
  5. sum = sum + i; — Adds the current value of i to the sum.
  6. i++; — Increments the counter by 1 after each iteration.
  7. printf(...) — Displays the final sum after the loop ends.

Step 3: Save, Compile, and Run

  1. Save the file (Ctrl + S).
  2. Open the terminal and compile using the command:
gcc sum_1_to_100.c -o sum_1_to_100

Run the program:

./sum_1_to_100

Sample Output:

========== SUM RESULT ========== Sum of numbers from 1 to 100 = 5050 ================================

Step 4: Modify the Program

  1. Change the range: Change i <= 100 to i <= 50 to find the sum of 1 to 50.
  2. Take user input: Ask the user for the limit and calculate the sum.
  3. Print each step: Print the sum after each addition to see how it accumulates.
/* Modified: Sum of numbers up to user-defined limit */ 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);
/* Modified: Print sum after each addition */ while (i <= 10) // Up to 10 for demonstration { 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
11✅ True01
22✅ True13
33✅ True36
44✅ True610
55✅ True1015
...............
9999✅ True48514950
100100✅ True49505050
101101❌ 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

/* Alternative 1: Using for loop */ int sum = 0; for (int i = 1; i <= 100; i++) { sum = sum + i; } printf("Sum = %d", sum);
/* Alternative 2: Using do-while loop */ int i = 1, sum = 0; do { sum = sum + i; i++; } while (i <= 100); printf("Sum = %d", sum);
/* Alternative 3: Using mathematical formula (without loop) */ int n = 100; int sum = n * (n + 1) / 2; printf("Sum = %d", sum); // 5050

📊 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 ہے۔

📝 اقدامات

پہلا قدم: پروگرام لکھیں

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. sum_1_to_100.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 42: Program to add numbers 1-100 using while loop */ #include <stdio.h> int main() { int i = 1; // Counter variable int sum = 0; // Sum accumulator /* ============================================ Using while loop to add numbers from 1 to 100 ============================================ */ while (i <= 100) { sum = sum + i; // Add current number to sum i++; // Increment the counter } // Display the result printf("========== SUM RESULT ==========\n"); printf("Sum of numbers from 1 to 100 = %d\n", sum); printf("================================\n"); return 0; }

دوسرا قدم: کوڈ کی وضاحت

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. int i = 1; — کاؤنٹر متغیر کا اعلان اور 1 سے شروع کرتا ہے۔
  3. int sum = 0; — sum متغیر کا اعلان اور 0 سے شروع کرتا ہے۔
  4. while (i <= 100) — while لوپ اس وقت تک چلتا ہے جب تک i 100 سے کم یا برابر ہے۔
  5. sum = sum + i; — i کی موجودہ قیمت کو sum میں شامل کرتا ہے۔
  6. i++; — ہر تکرار کے بعد کاؤنٹر کو 1 بڑھاتا ہے۔
  7. printf(...) — لوپ ختم ہونے کے بعد آخری جمع ظاہر کرتا ہے۔

تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں

  1. فائل کو محفوظ کریں (Ctrl + S)۔
  2. ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
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 جمع (پہلے) جمع (بعد میں)
11✅ درست01
22✅ درست13
33✅ درست36
44✅ درست610
55✅ درست1015
...............
9999✅ درست48514950
100100✅ درست49505050
101101❌ غلط-(لوپ ختم)

📊 مشاہدات

  • while لوپ کامیابی سے 1 سے 100 تک کے اعداد کو جمع کرتا ہے۔
  • sum متغیر 0 سے شروع ہوتا ہے اور کل جمع کو جمع کرتا ہے۔
  • کاؤنٹر i 1 سے شروع ہوتا ہے اور 100 تک جاتا ہے۔
  • لوپ ختم ہونے کے بعد، جمع 5050 ہے۔
  • نتیجہ ریاضیاتی فارمولا n(n+1)/2 سے مماثل ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو while لوپ کا استعمال کرتے ہوئے 1 سے 100 تک کے تمام اعداد کا جمع حساب کرتا ہے اور آخر میں جمع پرنٹ کرتا ہے۔ آپ نے بار بار حسابات کرنے اور نتائج جمع کرنے کے لیے لوپ کا استعمال سیکھ لیا ہے۔

📌 اہم: اقدار کو شامل کرنے سے پہلے ہمیشہ sum متغیر کو 0 سے شروع کریں۔ اگر آپ کاؤنٹر کو بڑھانا بھول گئے تو لوپ ہمیشہ کے لیے چلتا رہے گا (لامحدود لوپ)۔
💡 مشورہ: آپ ابتدائی قیمت اور while لوپ میں شرط کو تبدیل کرکے اعداد کی کسی بھی رینج کا جمع حساب کرنے کے لیے اسی منطق کا استعمال کر سکتے ہیں۔
⬅ Back to Practical Notebooks