EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that prints the first five natural numbers (1, 2, 3, 4, 5) and their sum using the while loop.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of while loops and sum accumulation

📖 Introduction

In this practical, you will write a program that:

  • Prints the first five natural numbers: 1, 2, 3, 4, 5
  • Calculates their sum: 1 + 2 + 3 + 4 + 5 = 15
  • Uses a while loop for iteration

This is a simple but important program that demonstrates how to use loops to both display values and perform calculations simultaneously.

💡 Tip: The sum of the first n natural numbers is given by the formula n * (n + 1) / 2. For n = 5, the sum is 15.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named first_five_natural.c.
  3. Write the following code:
/* Practical 43: Program to print first five natural numbers and their sum */ #include <stdio.h> int main() { int i = 1; // Counter variable int sum = 0; // Sum accumulator /* ============================================ Using while loop to print first five natural numbers and calculate their sum ============================================ */ printf("========== FIRST FIVE NATURAL NUMBERS ==========\n"); while (i <= 5) { printf("%d\n", i); // Print the current number sum = sum + i; // Add the number to sum i++; // Increment the counter } // Display the sum printf("\nSum of first five natural numbers = %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 <= 5) — The while loop runs 5 times (from 1 to 5).
  5. printf("%d\n", i); — Prints the current number.
  6. sum = sum + i; — Adds the current number to the sum.
  7. i++; — Increments the counter by 1.
  8. 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 first_five_natural.c -o first_five_natural

Run the program:

./first_five_natural

Sample Output:

========== FIRST FIVE NATURAL NUMBERS ========== 1 2 3 4 5 Sum of first five natural numbers = 15 =================================================

Step 4: Modify the Program

  1. Print in one line: Change printf("%d\n", i); to printf("%d ", i);
  2. Print with formatting: Add a header or footer with dashes.
  3. Change the count: Change i <= 5 to i <= 10 to print the first ten natural numbers.
/* Modified: Print in one line with commas */ while (i <= 5) { if (i == 5) printf("%d", i); else printf("%d, ", i); sum = sum + i; i++; } printf("\nSum = %d\n", sum);

📊 While Loop Execution (Step-by-Step)

Iteration i Value Condition i <= 5 Print Sum Before Sum After
1 1 ✅ True 1 0 1
2 2 ✅ True 2 1 3
3 3 ✅ True 3 3 6
4 4 ✅ True 4 6 10
5 5 ✅ True 5 10 15
6 6 ❌ False (Loop ends) - -

📊 Natural Numbers and Their Sum

n (Count) First n Natural Numbers Sum
1 1 1
2 1, 2 3
3 1, 2, 3 6
4 1, 2, 3, 4 10
5 1, 2, 3, 4, 5 15
10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 55

📊 Alternative Methods

/* Alternative 1: Using for loop */ int sum = 0; for (int i = 1; i <= 5; i++) { printf("%d\n", i); sum = sum + i; } printf("Sum = %d", sum);
/* Alternative 2: Using do-while loop */ int i = 1, sum = 0; do { printf("%d\n", i); sum = sum + i; i++; } while (i <= 5); printf("Sum = %d", sum);
/* Alternative 3: Using mathematical formula */ int n = 5; int sum = n * (n + 1) / 2; printf("Sum of first %d natural numbers = %d", n, sum);

📊 Observations

  • The while loop successfully prints the first five natural numbers.
  • The sum variable accumulates the total: 1 + 2 + 3 + 4 + 5 = 15.
  • The loop runs exactly 5 times (from 1 to 5).
  • The program displays both the numbers and their sum.
  • The result matches the mathematical formula for the sum of first n natural numbers.

✅ Result

You have successfully written a C program that prints the first five natural numbers and their sum using the while loop. You have learned how to use a loop to both display values and perform calculations simultaneously.

📌 Important: The sum variable must be initialized to 0 before the loop starts. The counter variable must be incremented inside the loop to avoid an infinite loop.
💡 Tip: You can extend this program to print the first n natural numbers and their sum by asking the user for the value of n.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو پہلے پانچ قدرتی اعداد (1، 2، 3، 4، 5) اور ان کا جمع while لوپ کا استعمال کرتے ہوئے پرنٹ کرے۔

🛠️ آپ کو کیا چاہیے

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • while لوپ اور جمع جمع کرنے کی معلومات

📖 تعارف

اس عملی میں، آپ ایک پروگرام لکھیں گے جو:

  • پہلے پانچ قدرتی اعداد پرنٹ کرتا ہے: 1، 2، 3، 4، 5
  • ان کا جمع حساب کرتا ہے: 1 + 2 + 3 + 4 + 5 = 15
  • تکرار کے لیے while لوپ استعمال کرتا ہے

یہ ایک سادہ مگر اہم پروگرام ہے جو ظاہر کرتا ہے کہ لوپ کا استعمال کرتے ہوئے اقدار کو ظاہر کرنے اور حسابات کرنے کا طریقہ۔

💡 مشورہ: پہلے n قدرتی اعداد کا جمع فارمولا n * (n + 1) / 2 سے دیا جاتا ہے۔ n = 5 کے لیے، جمع 15 ہے۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. first_five_natural.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 43: Program to print first five natural numbers and their sum */ #include <stdio.h> int main() { int i = 1; // Counter variable int sum = 0; // Sum accumulator /* ============================================ Using while loop to print first five natural numbers and calculate their sum ============================================ */ printf("========== FIRST FIVE NATURAL NUMBERS ==========\n"); while (i <= 5) { printf("%d\n", i); // Print the current number sum = sum + i; // Add the number to sum i++; // Increment the counter } // Display the sum printf("\nSum of first five natural numbers = %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 <= 5) — while لوپ 5 بار چلتا ہے (1 سے 5 تک)۔
  5. printf("%d\n", i); — موجودہ عدد پرنٹ کرتا ہے۔
  6. sum = sum + i; — موجودہ عدد کو sum میں شامل کرتا ہے۔
  7. i++; — کاؤنٹر کو 1 بڑھاتا ہے۔
  8. printf(...) — لوپ ختم ہونے کے بعد آخری جمع ظاہر کرتا ہے۔

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

  1. فائل کو محفوظ کریں (Ctrl + S)۔
  2. ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc first_five_natural.c -o first_five_natural

پروگرام چلائیں:

./first_five_natural

نمونہ آؤٹ پٹ:

========== FIRST FIVE NATURAL NUMBERS ========== 1 2 3 4 5 Sum of first five natural numbers = 15 =================================================

📊 while لوپ کا مرحلہ وار عمل

تکرار i کی قیمت شرط i <= 5 پرنٹ جمع (پہلے) جمع (بعد میں)
1 1 ✅ درست 1 0 1
2 2 ✅ درست 2 1 3
3 3 ✅ درست 3 3 6
4 4 ✅ درست 4 6 10
5 5 ✅ درست 5 10 15
6 6 ❌ غلط (لوپ ختم) - -

📊 مشاہدات

  • while لوپ کامیابی سے پہلے پانچ قدرتی اعداد پرنٹ کرتا ہے۔
  • sum متغیر کل جمع کو جمع کرتا ہے: 1 + 2 + 3 + 4 + 5 = 15۔
  • لوپ بالکل 5 بار چلتا ہے (1 سے 5 تک)۔
  • پروگرام اعداد اور ان کا جمع دونوں ظاہر کرتا ہے۔
  • نتیجہ پہلے n قدرتی اعداد کے جمع کے ریاضیاتی فارمولا سے مماثل ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو while لوپ کا استعمال کرتے ہوئے پہلے پانچ قدرتی اعداد اور ان کا جمع پرنٹ کرتا ہے۔ آپ نے لوپ کا استعمال کرتے ہوئے اقدار کو ظاہر کرنے اور حسابات کرنے کا طریقہ سیکھ لیا ہے۔

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