🎯 Aim
To write a C/C++ program that takes marks as input from the user and assigns grades based on the marks using nested if-else structure.
🛠️ What You Need
- Visual Studio Code or any C/C++ compiler
- Knowledge of nested if-else statements and comparison operators
📖 Introduction
In this practical, you will use a nested if-else structure to assign grades to students based on their marks. A nested if-else is an if-else statement inside another if-else statement. This is useful when you need to check multiple conditions in a hierarchical manner.
The grade criteria is:
- A+: 90 - 100
- A: 80 - 89
- B: 70 - 79
- C: 60 - 69
- D: 50 - 59
- F: 0 - 49
- Invalid: Less than 0 or greater than 100
You will use nested if-else to check the marks in a step-by-step manner:
💡 Tip: Nested if-else is useful when you want to check conditions in a specific order. It is more readable for hierarchical conditions.
📝 Steps
Step 1: Write the Program
- Open VS Code or any C compiler.
- Create a new file named grade_nested.c.
- Write the following code:
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks (out of 100): ");
scanf("%d", &marks);
printf("\n========== GRADE ASSIGNMENT ==========\n");
printf("Marks Obtained: %d\n", marks);
if (marks >= 0 && marks <= 100)
{
if (marks >= 90)
{
printf("Grade: A+\n");
printf("🎉 Excellent Performance!\n");
}
else
{
if (marks >= 80)
{
printf("Grade: A\n");
printf("🌟 Very Good Performance!\n");
}
else
{
if (marks >= 70)
{
printf("Grade: B\n");
printf("👍 Good Performance!\n");
}
else
{
if (marks >= 60)
{
printf("Grade: C\n");
printf("📖 Satisfactory Performance!\n");
}
else
{
if (marks >= 50)
{
printf("Grade: D\n");
printf("📚 Need Improvement!\n");
}
else
{
printf("Grade: F\n");
printf("❌ Fail! Better luck next time.\n");
}
}
}
}
}
}
else
{
printf("⚠️ Invalid marks! Please enter marks between 0 and 100.\n");
}
printf("==========================================\n");
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- int marks; — Declares an integer variable to store marks.
- scanf("%d", &marks); — Reads marks from the user.
- if (marks >= 0 && marks <= 100) — Checks if marks are valid.
- Nested if-else — Checks each grade range in a hierarchical manner.
- if (marks >= 90) — Checks for A+ grade.
- else { if (marks >= 80) } — Nested structure for subsequent grades.
Step 3: Save, Compile, and Run
- Save the file (Ctrl + S).
- Open the terminal and compile using the command:
gcc grade_nested.c -o grade_nested
./grade_nested
Sample Input & Output:
Enter marks (out of 100): 95
========== GRADE ASSIGNMENT ==========
Marks Obtained: 95
Grade: A+
🎉 Excellent Performance!
==========================================
Enter marks (out of 100): 82
========== GRADE ASSIGNMENT ==========
Marks Obtained: 82
Grade: A
🌟 Very Good Performance!
==========================================
Enter marks (out of 100): 65
========== GRADE ASSIGNMENT ==========
Marks Obtained: 65
Grade: C
📖 Satisfactory Performance!
==========================================
Enter marks (out of 100): 35
========== GRADE ASSIGNMENT ==========
Marks Obtained: 35
Grade: F
❌ Fail! Better luck next time.
==========================================
Step 4: Test with Different Inputs
- Test 1: 95 → A+
- Test 2: 85 → A
- Test 3: 75 → B
- Test 4: 65 → C
- Test 5: 55 → D
- Test 6: 35 → F
- Test 7: 105 → Invalid
- Test 8: -5 → Invalid
- Test 9: 100 → A+
- Test 10: 50 → D
📊 Grade Criteria
| Marks Range |
Grade |
Description |
| 90 - 100 |
A+ |
Excellent Performance |
| 80 - 89 |
A |
Very Good Performance |
| 70 - 79 |
B |
Good Performance |
| 60 - 69 |
C |
Satisfactory Performance |
| 50 - 59 |
D |
Need Improvement |
| 0 - 49 |
F |
Fail |
| < 0 or > 100 |
Invalid |
Invalid Input |
📊 Alternative Method (Using else-if ladder)
if (marks >= 90 && marks <= 100)
printf("Grade: A+");
else if (marks >= 80)
printf("Grade: A");
else if (marks >= 70)
printf("Grade: B");
else if (marks >= 60)
printf("Grade: C");
else if (marks >= 50)
printf("Grade: D");
else if (marks >= 0)
printf("Grade: F");
else
printf("Invalid");
📊 Observations
- The program successfully takes marks as input from the user.
- It uses nested if-else to check the marks and assign grades.
- The nested structure checks conditions in a hierarchical manner.
- It handles invalid marks appropriately.
- Each grade has a descriptive message for the student.
✅ Result
You have successfully written a C program that uses nested if-else to assign grades based on marks entered by the user. You have learned how to use nested conditions to implement a hierarchical decision-making process.
📌 Important: In nested if-else, each condition is checked one by one. When a condition is true, the program executes that block and skips the rest. This is why the order of conditions matters.
💡 Tip: Nested if-else is best used when you have a hierarchical set of conditions. For simple linear conditions, an else-if ladder is often more readable.
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو صارف سے نمبرز ان پٹ کے طور پر لے اور nested if-else ڈھانچے کا استعمال کرتے ہوئے نمبرز کی بنیاد پر گریڈز تفویض کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- nested if-else بیانات اور موازنہ آپریٹرز کی معلومات
📖 تعارف
اس عملی میں، آپ nested if-else ڈھانچے کا استعمال کریں گے تاکہ طلباء کو ان کے نمبرز کی بنیاد پر گریڈز تفویض کیے جا سکیں۔ Nested if-else ایک ایسا ڈھانچہ ہے جس میں ایک if-else بیان دوسرے if-else بیان کے اندر ہوتا ہے۔ یہ تب مفید ہے جب آپ کو درجہ بندی کی ترتیب میں متعدد شرائط کو چیک کرنا ہو۔
گریڈ کا معیار یہ ہے:
- A+: 90 - 100
- A: 80 - 89
- B: 70 - 79
- C: 60 - 69
- D: 50 - 59
- F: 0 - 49
- غلط: 0 سے کم یا 100 سے زیادہ
💡 مشورہ: Nested if-else اس وقت مفید ہے جب آپ شرائط کو ایک مخصوص ترتیب میں چیک کرنا چاہتے ہیں۔ یہ درجہ بندی کی شرائط کے لیے زیادہ پڑھنے کے قابل ہے۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- grade_nested.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks (out of 100): ");
scanf("%d", &marks);
printf("\n========== GRADE ASSIGNMENT ==========\n");
printf("Marks Obtained: %d\n", marks);
if (marks >= 0 && marks <= 100)
{
if (marks >= 90)
{
printf("Grade: A+\n");
printf("🎉 Excellent Performance!\n");
}
else
{
if (marks >= 80)
{
printf("Grade: A\n");
printf("🌟 Very Good Performance!\n");
}
else
{
if (marks >= 70)
{
printf("Grade: B\n");
printf("👍 Good Performance!\n");
}
else
{
if (marks >= 60)
{
printf("Grade: C\n");
printf("📖 Satisfactory Performance!\n");
}
else
{
if (marks >= 50)
{
printf("Grade: D\n");
printf("📚 Need Improvement!\n");
}
else
{
printf("Grade: F\n");
printf("❌ Fail! Better luck next time.\n");
}
}
}
}
}
}
else
{
printf("⚠️ Invalid marks! Please enter marks between 0 and 100.\n");
}
printf("==========================================\n");
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- int marks; — نمبرز کو ذخیرہ کرنے کے لیے ایک انٹیجر متغیر کا اعلان کرتا ہے۔
- scanf("%d", &marks); — صارف سے نمبرز پڑھتا ہے۔
- if (marks >= 0 && marks <= 100) — چیک کرتا ہے کہ آیا نمبرز درست ہیں۔
- Nested if-else — ہر گریڈ کی حد کو درجہ بندی کی ترتیب میں چیک کرتا ہے۔
- if (marks >= 90) — A+ گریڈ کے لیے چیک کرتا ہے۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc grade_nested.c -o grade_nested
./grade_nested
نمونہ ان پٹ اور آؤٹ پٹ:
Enter marks (out of 100): 95
========== GRADE ASSIGNMENT ==========
Marks Obtained: 95
Grade: A+
🎉 Excellent Performance!
==========================================
📊 گریڈ کا معیار
| نمبرز کی حد |
گریڈ |
وضاحت |
| 90 - 100 |
A+ |
شاندار کارکردگی |
| 80 - 89 |
A |
بہت اچھی کارکردگی |
| 70 - 79 |
B |
اچھی کارکردگی |
| 60 - 69 |
C |
تسلی بخش کارکردگی |
| 50 - 59 |
D |
بہتری کی ضرورت |
| 0 - 49 |
F |
ناکام |
| < 0 یا > 100 |
غلط |
غلط ان پٹ |
📊 مشاہدات
- پروگرام کامیابی سے صارف سے نمبرز ان پٹ کے طور پر لیتا ہے۔
- یہ nested if-else کا استعمال کرتے ہوئے نمبرز کو چیک کرتا ہے اور گریڈز تفویض کرتا ہے۔
- nested ڈھانچہ شرائط کو درجہ بندی کی ترتیب میں چیک کرتا ہے۔
- یہ غلط نمبرز کو مناسب طریقے سے ہینڈل کرتا ہے۔
- ہر گریڈ کے لیے طالب علم کے لیے ایک وضاحتی پیغام ہے۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو nested if-else کا استعمال کرتے ہوئے صارف کے درج کردہ نمبرز کی بنیاد پر گریڈز تفویض کرتا ہے۔ آپ نے درجہ بندی کے فیصلہ سازی کے عمل کو نافذ کرنے کے لیے nested شرائط کا استعمال کرنا سیکھ لیا ہے۔
📌 اہم: nested if-else میں، ہر شرط کو یکے بعد دیگرے چیک کیا جاتا ہے۔ جب کوئی شرط درست ہوتی ہے تو پروگرام اس بلاک کو انجام دیتا ہے اور باقی کو چھوڑ دیتا ہے۔ یہی وجہ ہے کہ شرائط کی ترتیب اہم ہے۔
💡 مشورہ: Nested if-else اس وقت بہترین استعمال ہوتا ہے جب آپ کے پاس درجہ بندی کی شرائط ہوں۔ سادہ لکیری شرائط کے لیے، else-if ladder اکثر زیادہ پڑھنے کے قابل ہوتا ہے۔