🎯 Aim
To write a C/C++ program that calculates the sum of all positive even numbers below 10 (i.e., 2, 4, 6, 8) using the for loop with a conditional statement (if).
🛠️ What You Need
- Visual Studio Code or any C/C++ compiler
- Knowledge of for loops, if statements, and arithmetic operations
📖 Introduction
In this practical, you will calculate the sum of all positive even numbers that are less than 10. The numbers are:
The sum is: 2 + 4 + 6 + 8 = 20
You will use:
- A for loop — to iterate from 1 to 9
- An if statement — to check if the number is even (
i % 2 == 0)
- A sum variable — to accumulate the total
💡 Tip: An even number is divisible by 2. You can check this using i % 2 == 0.
📝 Steps
Step 1: Write the Program
- Open VS Code or any C compiler.
- Create a new file named sum_even_below10.c.
- Write the following code:
#include <stdio.h>
int main()
{
int i, sum = 0;
printf("========== SUM OF EVEN NUMBERS ==========\n");
printf("Positive even numbers below 10 are:\n");
for (i = 1; i < 10; i++)
{
if (i % 2 == 0)
{
printf("%d ", i);
sum = sum + i;
}
}
printf("\n\nSum of even numbers below 10 = %d\n", sum);
printf("=========================================\n");
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- int i, sum = 0; — Declares
i as the loop counter and initializes sum to 0.
- for (i = 1; i < 10; i++) — The for loop runs from 1 to 9.
- if (i % 2 == 0) — Checks if the number is even.
- sum = sum + i; — Adds the even number to the sum.
- printf(...) — Displays the even numbers and the final sum.
Step 3: Save, Compile, and Run
- Save the file (Ctrl + S).
- Open the terminal and compile using the command:
gcc sum_even_below10.c -o sum_even_below10
./sum_even_below10
Sample Output:
========== SUM OF EVEN NUMBERS ==========
Positive even numbers below 10 are:
2 4 6 8
Sum of even numbers below 10 = 20
=========================================
Step 4: Modify the Program
- Change the limit: Change
i < 10 to i < 20 to find the sum of even numbers below 20.
- Change the condition: Change
i % 2 == 0 to i % 2 != 0 to find the sum of odd numbers.
- Take user input: Ask the user for the limit.
int limit, i, sum = 0;
printf("Enter the limit: ");
scanf("%d", &limit);
for (i = 1; i < limit; i++)
{
if (i % 2 == 0)
{
sum = sum + i;
}
}
printf("Sum of even numbers below %d = %d\n", limit, sum);
📊 Step-by-Step Execution
| Iteration |
i |
Is i even? |
Sum Before |
Sum After |
| 1 |
1 |
❌ No |
0 |
0 |
| 2 |
2 |
✅ Yes |
0 |
2 |
| 3 |
3 |
❌ No |
2 |
2 |
| 4 |
4 |
✅ Yes |
2 |
6 |
| 5 |
5 |
❌ No |
6 |
6 |
| 6 |
6 |
✅ Yes |
6 |
12 |
| 7 |
7 |
❌ No |
12 |
12 |
| 8 |
8 |
✅ Yes |
12 |
20 |
| 9 |
9 |
❌ No |
20 |
20 |
📊 Odd vs Even Sum Comparison
| Type |
Numbers |
Sum |
| Odd Numbers |
1, 3, 5, 7, 9 |
25 |
| Even Numbers |
2, 4, 6, 8 |
20 |
| All Numbers |
1, 2, 3, 4, 5, 6, 7, 8, 9 |
45 |
📊 Alternative Methods
int i = 1, sum = 0;
while (i < 10)
{
if (i % 2 == 0)
{
sum = sum + i;
}
i++;
}
int i = 1, sum = 0;
do
{
if (i % 2 == 0)
{
sum = sum + i;
}
i++;
} while (i < 10);
int i, sum = 0;
for (i = 2; i < 10; i += 2)
{
sum = sum + i;
}
printf("Sum = %d", sum);
int n = 10;
int count = (n - 1) / 2;
int sum = count * (count + 1);
printf("Sum = %d", sum);
📊 Observations
- The
for loop iterates from 1 to 9.
- The
if statement checks if the number is even using i % 2 == 0.
- Only even numbers (2, 4, 6, 8) are added to the sum.
- The sum variable accumulates the total: 2 + 4 + 6 + 8 = 20.
- The program displays both the even numbers and the final sum.
- Zero is not included because we start from 1 and go up to 9.
✅ Result
You have successfully written a C program that calculates the sum of positive even numbers below 10 using the for loop with a conditional statement. You have learned how to use loops and conditions together to solve problems.
📌 Important: The condition i % 2 == 0 checks if a number is even. The sum variable accumulates the total. Always initialize sum to 0 before adding values.
💡 Tip: You can use i += 2 with an initial value of 2 in the for loop to iterate only over even numbers directly, which is more efficient than checking with if.
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو 10 سے کم تمام مثبت جفت اعداد (یعنی 2، 4، 6، 8) کا جمع for لوپ اور مشروط بیان (if) کا استعمال کرتے ہوئے حساب کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- for لوپ، if بیانات، اور ریاضی کے عمل کی معلومات
📖 تعارف
اس عملی میں، آپ ان تمام مثبت جفت اعداد کا جمع حساب کریں گے جو 10 سے کم ہیں۔ اعداد یہ ہیں:
جمع ہے: 2 + 4 + 6 + 8 = 20
آپ استعمال کریں گے:
- for لوپ — 1 سے 9 تک تکرار کرنے کے لیے
- if بیان — یہ چیک کرنے کے لیے کہ آیا عدد جفت ہے (
i % 2 == 0)
- sum متغیر — کل جمع کو ذخیرہ کرنے کے لیے
💡 مشورہ: ایک جفت عدد 2 سے تقسیم ہوتا ہے۔ آپ اسے i % 2 == 0 کے ساتھ چیک کر سکتے ہیں۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- sum_even_below10.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
int i, sum = 0;
printf("========== SUM OF EVEN NUMBERS ==========\n");
printf("Positive even numbers below 10 are:\n");
for (i = 1; i < 10; i++)
{
if (i % 2 == 0)
{
printf("%d ", i);
sum = sum + i;
}
}
printf("\n\nSum of even numbers below 10 = %d\n", sum);
printf("=========================================\n");
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- int i, sum = 0; —
i کو لوپ کاؤنٹر کے طور پر اعلان کرتا ہے اور sum کو 0 سے شروع کرتا ہے۔
- for (i = 1; i < 10; i++) — for لوپ 1 سے 9 تک چلتا ہے۔
- if (i % 2 == 0) — چیک کرتا ہے کہ آیا عدد جفت ہے۔
- sum = sum + i; — جفت عدد کو جمع میں شامل کرتا ہے۔
- printf(...) — جفت اعداد اور آخری جمع کو ظاہر کرتا ہے۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc sum_even_below10.c -o sum_even_below10
./sum_even_below10
نمونہ آؤٹ پٹ:
========== SUM OF EVEN NUMBERS ==========
Positive even numbers below 10 are:
2 4 6 8
Sum of even numbers below 10 = 20
=========================================
📊 مرحلہ وار عمل
| تکرار |
i |
کیا i جفت ہے؟ |
جمع (پہلے) |
جمع (بعد میں) |
| 1 |
1 |
❌ نہیں |
0 |
0 |
| 2 |
2 |
✅ ہاں |
0 |
2 |
| 3 |
3 |
❌ نہیں |
2 |
2 |
| 4 |
4 |
✅ ہاں |
2 |
6 |
| 5 |
5 |
❌ نہیں |
6 |
6 |
| 6 |
6 |
✅ ہاں |
6 |
12 |
| 7 |
7 |
❌ نہیں |
12 |
12 |
| 8 |
8 |
✅ ہاں |
12 |
20 |
| 9 |
9 |
❌ نہیں |
20 |
20 |
📊 مشاہدات
for لوپ 1 سے 9 تک تکرار کرتا ہے۔
if بیان i % 2 == 0 کا استعمال کرتے ہوئے چیک کرتا ہے کہ آیا عدد جفت ہے۔
- صرف جفت اعداد (2، 4، 6، 8) کو جمع میں شامل کیا جاتا ہے۔
- sum متغیر کل جمع کو جمع کرتا ہے: 2 + 4 + 6 + 8 = 20۔
- پروگرام جفت اعداد اور آخری جمع دونوں کو ظاہر کرتا ہے۔
- صفر شامل نہیں ہے کیونکہ ہم 1 سے شروع کرتے ہیں اور 9 تک جاتے ہیں۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو for لوپ اور مشروط بیان کا استعمال کرتے ہوئے 10 سے کم مثبت جفت اعداد کا جمع حساب کرتا ہے۔ آپ نے مسائل کو حل کرنے کے لیے لوپ اور شرائط کو ایک ساتھ استعمال کرنا سیکھ لیا ہے۔
📌 اہم: شرط i % 2 == 0 چیک کرتی ہے کہ آیا کوئی عدد جفت ہے۔ sum متغیر کل جمع کو جمع کرتا ہے۔ اقدار کو شامل کرنے سے پہلے ہمیشہ sum کو 0 سے شروع کریں۔
💡 مشورہ: آپ for لوپ میں i = 2 سے شروع کر کے i += 2 استعمال کر سکتے ہیں تاکہ صرف جفت اعداد پر تکرار کی جا سکے، جو if کے ساتھ چیک کرنے سے زیادہ موثر ہے۔