🎯 Aim
To write a C/C++ program that calculates the sum of all positive odd numbers below 10 (i.e., 1, 3, 5, 7, 9) 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 odd numbers that are less than 10. The numbers are:
The sum is: 1 + 3 + 5 + 7 + 9 = 25
You will use:
- A for loop — to iterate from 1 to 9
- An if statement — to check if the number is odd (
i % 2 != 0)
- A sum variable — to accumulate the total
💡 Tip: An odd number is not divisible by 2. You can check this using i % 2 != 0 or i % 2 == 1.
📝 Steps
Step 1: Write the Program
- Open VS Code or any C compiler.
- Create a new file named sum_odd_below10.c.
- Write the following code:
#include <stdio.h>
int main()
{
int i, sum = 0;
printf("========== SUM OF ODD NUMBERS ==========\n");
printf("Positive odd 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 odd 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 odd.
- sum = sum + i; — Adds the odd number to the sum.
- printf(...) — Displays the odd 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_odd_below10.c -o sum_odd_below10
./sum_odd_below10
Sample Output:
========== SUM OF ODD NUMBERS ==========
Positive odd numbers below 10 are:
1 3 5 7 9
Sum of odd numbers below 10 = 25
=========================================
Step 4: Modify the Program
- Change the limit: Change
i < 10 to i < 20 to find the sum of odd numbers below 20.
- Change the condition: Change
i % 2 != 0 to i % 2 == 0 to find the sum of even 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 odd numbers below %d = %d\n", limit, sum);
📊 Step-by-Step Execution
| Iteration |
i |
Is i odd? |
Sum Before |
Sum After |
| 1 |
1 |
✅ Yes |
0 |
1 |
| 2 |
2 |
❌ No |
1 |
1 |
| 3 |
3 |
✅ Yes |
1 |
4 |
| 4 |
4 |
❌ No |
4 |
4 |
| 5 |
5 |
✅ Yes |
4 |
9 |
| 6 |
6 |
❌ No |
9 |
9 |
| 7 |
7 |
✅ Yes |
9 |
16 |
| 8 |
8 |
❌ No |
16 |
16 |
| 9 |
9 |
✅ Yes |
16 |
25 |
📊 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 = 1; i < 10; i += 2)
{
sum = sum + i;
}
printf("Sum = %d", sum);
📊 Observations
- The
for loop iterates from 1 to 9.
- The
if statement checks if the number is odd using i % 2 != 0.
- Only odd numbers are added to the sum.
- The sum variable accumulates the total: 1 + 3 + 5 + 7 + 9 = 25.
- The program displays both the odd numbers and the final sum.
✅ Result
You have successfully written a C program that calculates the sum of positive odd 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 odd. The sum variable accumulates the total. Always initialize sum to 0 before adding values.
💡 Tip: You can use i += 2 in the for loop to iterate only over odd numbers directly, which is more efficient than checking with if.
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو 10 سے کم تمام مثبت طاق اعداد (یعنی 1، 3، 5، 7، 9) کا جمع for لوپ اور مشروط بیان (if) کا استعمال کرتے ہوئے حساب کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- for لوپ، if بیانات، اور ریاضی کے عمل کی معلومات
📖 تعارف
اس عملی میں، آپ ان تمام مثبت طاق اعداد کا جمع حساب کریں گے جو 10 سے کم ہیں۔ اعداد یہ ہیں:
جمع ہے: 1 + 3 + 5 + 7 + 9 = 25
آپ استعمال کریں گے:
- for لوپ — 1 سے 9 تک تکرار کرنے کے لیے
- if بیان — یہ چیک کرنے کے لیے کہ آیا عدد طاق ہے (
i % 2 != 0)
- sum متغیر — کل جمع کو ذخیرہ کرنے کے لیے
💡 مشورہ: ایک طاق عدد 2 سے تقسیم نہیں ہوتا۔ آپ اسے i % 2 != 0 یا i % 2 == 1 کے ساتھ چیک کر سکتے ہیں۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- sum_odd_below10.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
int i, sum = 0;
printf("========== SUM OF ODD NUMBERS ==========\n");
printf("Positive odd 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 odd 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_odd_below10.c -o sum_odd_below10
./sum_odd_below10
نمونہ آؤٹ پٹ:
========== SUM OF ODD NUMBERS ==========
Positive odd numbers below 10 are:
1 3 5 7 9
Sum of odd numbers below 10 = 25
=========================================
📊 مرحلہ وار عمل
| تکرار |
i |
کیا i طاق ہے؟ |
جمع (پہلے) |
جمع (بعد میں) |
| 1 |
1 |
✅ ہاں |
0 |
1 |
| 2 |
2 |
❌ نہیں |
1 |
1 |
| 3 |
3 |
✅ ہاں |
1 |
4 |
| 4 |
4 |
❌ نہیں |
4 |
4 |
| 5 |
5 |
✅ ہاں |
4 |
9 |
| 6 |
6 |
❌ نہیں |
9 |
9 |
| 7 |
7 |
✅ ہاں |
9 |
16 |
| 8 |
8 |
❌ نہیں |
16 |
16 |
| 9 |
9 |
✅ ہاں |
16 |
25 |
📊 مشاہدات
for لوپ 1 سے 9 تک تکرار کرتا ہے۔
if بیان i % 2 != 0 کا استعمال کرتے ہوئے چیک کرتا ہے کہ آیا عدد طاق ہے۔
- صرف طاق اعداد کو جمع میں شامل کیا جاتا ہے۔
- sum متغیر کل جمع کو جمع کرتا ہے: 1 + 3 + 5 + 7 + 9 = 25۔
- پروگرام طاق اعداد اور آخری جمع دونوں کو ظاہر کرتا ہے۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو for لوپ اور مشروط بیان کا استعمال کرتے ہوئے 10 سے کم مثبت طاق اعداد کا جمع حساب کرتا ہے۔ آپ نے مسائل کو حل کرنے کے لیے لوپ اور شرائط کو ایک ساتھ استعمال کرنا سیکھ لیا ہے۔
📌 اہم: شرط i % 2 != 0 چیک کرتی ہے کہ آیا کوئی عدد طاق ہے۔ sum متغیر کل جمع کو جمع کرتا ہے۔ اقدار کو شامل کرنے سے پہلے ہمیشہ sum کو 0 سے شروع کریں۔
💡 مشورہ: آپ for لوپ میں i += 2 استعمال کر سکتے ہیں تاکہ صرف طاق اعداد پر تکرار کی جا سکے، جو if کے ساتھ چیک کرنے سے زیادہ موثر ہے۔