EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that prints natural numbers from 1 to 10 in descending order (i.e., 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) using the while loop.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of while loops and decrement operator

📖 Introduction

In this practical, you will use the while loop with the decrement operator (--) to print numbers in descending order (from 10 down to 1). This is the opposite of the previous practical where you printed from 1 to 10.

The key concept is:

  • Initialize the counter to the highest number (10)
  • Condition checks if the counter is >= 1
  • Decrement the counter by 1 each time
💡 Tip: The decrement operator -- decreases the value of a variable by 1. i-- is the same as i = i - 1.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named descending_while.c.
  3. Write the following code:
/* Practical 41: Program to print numbers 1-10 in descending order using while loop */ #include <stdio.h> int main() { int i; /* ============================================ Using while loop with decrement operator (--) to print numbers from 10 to 1 ============================================ */ printf("========== DESCENDING ORDER ==========\n"); i = 10; // Initialize to highest number while (i >= 1) { printf("%d\n", i); i--; // Decrement the counter } printf("=======================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int i; — Declares an integer variable i as the loop counter.
  3. i = 10; — Initializes the counter to 10 (starting from the highest number).
  4. while (i >= 1) — The while loop runs as long as i is greater than or equal to 1.
  5. printf("%d\n", i); — Displays the current value of i.
  6. i--; — Decrements the counter by 1 after each iteration.

Step 3: Save, Compile, and Run

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

Run the program:

./descending_while

Sample Output:

========== DESCENDING ORDER ========== 10 9 8 7 6 5 4 3 2 1 =======================================

Step 4: Modify the Program

  1. Change the starting value: Change i = 10 to i = 20 to print from 20 down to 1.
  2. Change the condition: Change i >= 1 to i >= 5 to print from 10 down to 5.
  3. Change the decrement: Change i-- to i -= 2 to print even numbers in descending order (10, 8, 6, 4, 2).
/* Modified: Print even numbers from 10 to 2 in descending order */ i = 10; while (i >= 2) { printf("%d\n", i); i -= 2; // Decrement by 2 }

📊 While Loop Execution (Step-by-Step)

Iteration i Value Condition i >= 1 Output i--
1 10 ✅ True 10 9
2 9 ✅ True 9 8
3 8 ✅ True 8 7
4 7 ✅ True 7 6
5 6 ✅ True 6 5
6 5 ✅ True 5 4
7 4 ✅ True 4 3
8 3 ✅ True 3 2
9 2 ✅ True 2 1
10 1 ✅ True 1 0
11 0 ❌ False (Loop ends) -

📊 Ascending vs Descending Comparison

Direction Starting Value Condition Update Output
Ascending 1 i <= 10 i++ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Descending 10 i >= 1 i-- 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

📊 Alternative Methods

/* Alternative 1: Using for loop */ for (int i = 10; i >= 1; i--) { printf("%d\n", i); }
/* Alternative 2: Using do-while loop */ int i = 10; do { printf("%d\n", i); i--; } while (i >= 1);
/* Alternative 3: Print in ascending order using while loop */ int i = 1; while (i <= 10) { printf("%d\n", i); i++; }

📊 Observations

  • The while loop with decrement operator prints numbers in descending order.
  • The counter starts at 10 and goes down to 1.
  • The condition i >= 1 ensures the loop runs until 1 is printed.
  • The decrement operator i-- reduces the value by 1 each time.
  • You can change the starting value, condition, and decrement to customize the output.

✅ Result

You have successfully written a C program that prints natural numbers from 1 to 10 in descending order using the while loop. You have learned how to use the decrement operator to count backwards in a loop.

📌 Important: The decrement operator -- is used to decrease the counter. The condition i >= 1 checks if the counter is still greater than or equal to 1. When i becomes 0, the loop stops.
💡 Tip: You can use the while loop to count backwards with any step size by changing the decrement expression (e.g., i -= 2 for even numbers, i -= 3 for every third number).

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو 1 سے 10 تک کے قدرتی اعداد کو نزولی ترتیب (یعنی 10، 9، 8، 7، 6، 5، 4، 3، 2، 1) میں while لوپ کا استعمال کرتے ہوئے پرنٹ کرے۔

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

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

📖 تعارف

اس عملی میں، آپ while لوپ اور ڈیکریمنٹ آپریٹر (--) کا استعمال کرتے ہوئے اعداد کو نزولی ترتیب (10 سے 1 تک) میں پرنٹ کریں گے۔ یہ پچھلی عملی کے برعکس ہے جہاں آپ نے 1 سے 10 تک پرنٹ کیا تھا۔

اہم تصور یہ ہے:

  • کاؤنٹر کو سب سے بڑے عدد (10) سے شروع کریں
  • شرط چیک کرتی ہے کہ کیا کاؤنٹر >= 1 ہے
  • ہر بار کاؤنٹر کو 1 کم کریں
💡 مشورہ: ڈیکریمنٹ آپریٹر -- متغیر کی قیمت کو 1 کم کرتا ہے۔ i--، i = i - 1 کے برابر ہے۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. descending_while.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 41: Program to print numbers 1-10 in descending order using while loop */ #include <stdio.h> int main() { int i; /* ============================================ Using while loop with decrement operator (--) to print numbers from 10 to 1 ============================================ */ printf("========== DESCENDING ORDER ==========\n"); i = 10; // Initialize to highest number while (i >= 1) { printf("%d\n", i); i--; // Decrement the counter } printf("=======================================\n"); return 0; }

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

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. int i; — لوپ کاؤنٹر کے لیے ایک انٹیجر متغیر i کا اعلان کرتا ہے۔
  3. i = 10; — کاؤنٹر کو 10 سے شروع کرتا ہے (سب سے بڑے عدد سے)۔
  4. while (i >= 1) — while لوپ اس وقت تک چلتا ہے جب تک i 1 سے بڑا یا برابر ہے۔
  5. printf("%d\n", i); — i کی موجودہ قیمت ظاہر کرتا ہے۔
  6. i--; — ہر تکرار کے بعد کاؤنٹر کو 1 کم کرتا ہے۔

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

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

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

./descending_while

نمونہ آؤٹ پٹ:

========== DESCENDING ORDER ========== 10 9 8 7 6 5 4 3 2 1 =======================================

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

تکرار i کی قیمت شرط i >= 1 آؤٹ پٹ i--
1 10 ✅ درست 10 9
2 9 ✅ درست 9 8
3 8 ✅ درست 8 7
4 7 ✅ درست 7 6
5 6 ✅ درست 6 5
6 5 ✅ درست 5 4
7 4 ✅ درست 4 3
8 3 ✅ درست 3 2
9 2 ✅ درست 2 1
10 1 ✅ درست 1 0
11 0 ❌ غلط (لوپ ختم) -

📊 مشاہدات

  • while لوپ ڈیکریمنٹ آپریٹر کے ساتھ اعداد کو نزولی ترتیب میں پرنٹ کرتا ہے۔
  • کاؤنٹر 10 سے شروع ہوتا ہے اور 1 تک جاتا ہے۔
  • شرط i >= 1 اس بات کو یقینی بناتی ہے کہ لوپ 1 پرنٹ ہونے تک چلے۔
  • ڈیکریمنٹ آپریٹر i-- ہر بار قیمت کو 1 کم کرتا ہے۔
  • آپ آؤٹ پٹ کو حسب ضرورت بنانے کے لیے ابتدائی قیمت، شرط اور ڈیکریمنٹ کو تبدیل کر سکتے ہیں۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو while لوپ کا استعمال کرتے ہوئے 1 سے 10 تک کے قدرتی اعداد کو نزولی ترتیب میں پرنٹ کرتا ہے۔ آپ نے لوپ میں پیچھے کی طرف شمار کرنے کے لیے ڈیکریمنٹ آپریٹر کا استعمال سیکھ لیا ہے۔

📌 اہم: ڈیکریمنٹ آپریٹر -- کاؤنٹر کو کم کرنے کے لیے استعمال ہوتا ہے۔ شرط i >= 1 چیک کرتی ہے کہ آیا کاؤنٹر اب بھی 1 سے بڑا یا برابر ہے۔ جب i 0 ہو جاتا ہے تو لوپ رک جاتا ہے۔
💡 مشورہ: آپ ڈیکریمنٹ ایکسپریشن کو تبدیل کرکے (مثال: i -= 2 جفت اعداد کے لیے، i -= 3 ہر تیسرے عدد کے لیے) while لوپ کو کسی بھی قدم کے سائز کے ساتھ پیچھے کی طرف شمار کرنے کے لیے استعمال کر سکتے ہیں۔
⬅ Back to Practical Notebooks