EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that uses the switch statement to display the salary range based on employee grade (A, B, C, D).

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of switch statement and character input

📖 Introduction

In many organizations, employees are categorized into different grades based on their experience, qualifications, or job level. Each grade has a corresponding salary range.

In this practical, you will use the switch statement to display the salary range for a given grade:

  • Grade A: Rs. 80,000 - Rs. 120,000
  • Grade B: Rs. 50,000 - Rs. 80,000
  • Grade C: Rs. 30,000 - Rs. 50,000
  • Grade D: Rs. 15,000 - Rs. 30,000

You will also handle both uppercase and lowercase input and display an error message for invalid grades.

💡 Tip: The switch statement is perfect for this type of multi-way decision-making where you need to check a single variable against multiple constant values.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named salary_grade.c.
  3. Write the following code:
/* Practical 32: Program to display salary range using switch */ #include <stdio.h> int main() { // Declare a variable to store the grade char grade; // Take input from user printf("Enter employee grade (A, B, C, D): "); scanf("%c", &grade); // Display the grade entered printf("\n========== SALARY INFORMATION ==========\n"); printf("Employee Grade: %c\n", grade); /* ============================================ Using switch statement to display salary range ============================================ */ switch(grade) { case 'A': case 'a': printf("Salary Range: Rs. 80,000 - Rs. 120,000\n"); printf("📌 Grade A: Top-level management\n"); break; case 'B': case 'b': printf("Salary Range: Rs. 50,000 - Rs. 80,000\n"); printf("📌 Grade B: Mid-level management\n"); break; case 'C': case 'c': printf("Salary Range: Rs. 30,000 - Rs. 50,000\n"); printf("📌 Grade C: Entry-level professional\n"); break; case 'D': case 'd': printf("Salary Range: Rs. 15,000 - Rs. 30,000\n"); printf("📌 Grade D: Junior staff / Intern\n"); break; default: printf("❌ Invalid Grade!\n"); printf("📌 Please enter A, B, C, or D.\n"); break; } printf("==========================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. char grade; — Declares a character variable for the grade.
  3. scanf("%c", &grade); — Reads the grade from the user.
  4. switch(grade) — Switches based on the grade value.
  5. case 'A': case 'a': — Handles both uppercase and lowercase.
  6. default: — Handles invalid grades.

Step 3: Save, Compile, and Run

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

Run the program:

./salary_grade

Sample Input & Output:

Enter employee grade (A, B, C, D): A ========== SALARY INFORMATION ========== Employee Grade: A Salary Range: Rs. 80,000 - Rs. 120,000 📌 Grade A: Top-level management ==========================================
Enter employee grade (A, B, C, D): b ========== SALARY INFORMATION ========== Employee Grade: b Salary Range: Rs. 50,000 - Rs. 80,000 📌 Grade B: Mid-level management ==========================================
Enter employee grade (A, B, C, D): C ========== SALARY INFORMATION ========== Employee Grade: C Salary Range: Rs. 30,000 - Rs. 50,000 📌 Grade C: Entry-level professional ==========================================
Enter employee grade (A, B, C, D): X ========== SALARY INFORMATION ========== Employee Grade: X ❌ Invalid Grade! 📌 Please enter A, B, C, or D. ==========================================

Step 4: Test with Different Inputs

  1. Test 1: 'A' → Rs. 80,000 - Rs. 120,000
  2. Test 2: 'b' → Rs. 50,000 - Rs. 80,000
  3. Test 3: 'C' → Rs. 30,000 - Rs. 50,000
  4. Test 4: 'd' → Rs. 15,000 - Rs. 30,000
  5. Test 5: 'X' → Invalid Grade
  6. Test 6: '1' → Invalid Grade

📊 Grade and Salary Information

Grade Salary Range (PKR) Description
A Rs. 80,000 - Rs. 120,000 Top-level management
B Rs. 50,000 - Rs. 80,000 Mid-level management
C Rs. 30,000 - Rs. 50,000 Entry-level professional
D Rs. 15,000 - Rs. 30,000 Junior staff / Intern

📊 Alternative Method (Using if-else)

/* Alternative: Using if-else ladder */ if (grade == 'A' || grade == 'a') printf("Salary Range: Rs. 80,000 - Rs. 120,000"); else if (grade == 'B' || grade == 'b') printf("Salary Range: Rs. 50,000 - Rs. 80,000"); else if (grade == 'C' || grade == 'c') printf("Salary Range: Rs. 30,000 - Rs. 50,000"); else if (grade == 'D' || grade == 'd') printf("Salary Range: Rs. 15,000 - Rs. 30,000"); else printf("Invalid Grade");

📊 Observations

  • The program successfully takes a grade as input from the user.
  • The switch statement checks the grade and displays the corresponding salary range.
  • Both uppercase and lowercase inputs are handled.
  • The default case handles invalid grades.
  • The program provides a clear description of each grade level.

✅ Result

You have successfully written a C program that uses the switch statement to display salary ranges based on employee grades. You have learned how to use the switch statement for real-world applications like salary categorization.

📌 Important: The switch statement is a powerful tool for multi-way decision-making. It is especially useful when you need to compare a single variable against many constant values.
💡 Tip: You can extend this program to include more grades (e.g., E, F) or more detailed information (e.g., benefits, bonuses) for each grade.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو switch بیان کا استعمال کرتے ہوئے ملازم کے گریڈ (A, B, C, D) کی بنیاد پر تنخواہ کی رینج ظاہر کرتا ہے۔

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

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • switch بیان اور حروف کی معلومات

📖 تعارف

بہت سی تنظیموں میں، ملازمین کو ان کے تجربے، قابلیت، یا ملازمت کی سطح کی بنیاد پر مختلف گریڈز میں تقسیم کیا جاتا ہے۔ ہر گریڈ کی ایک مخصوص تنخواہ کی رینج ہوتی ہے۔

اس عملی میں، آپ switch بیان کا استعمال کرتے ہوئے دیے گئے گریڈ کے لیے تنخواہ کی رینج ظاہر کریں گے:

  • گریڈ A: Rs. 80,000 - Rs. 120,000
  • گریڈ B: Rs. 50,000 - Rs. 80,000
  • گریڈ C: Rs. 30,000 - Rs. 50,000
  • گریڈ D: Rs. 15,000 - Rs. 30,000
💡 مشورہ: switch بیان اس قسم کی کثیر شاخی فیصلہ سازی کے لیے بہترین ہے جہاں آپ کو ایک ہی متغیر کا متعدد مستقل اقدار سے موازنہ کرنا ہو۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. salary_grade.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 32: Program to display salary range using switch */ #include <stdio.h> int main() { // Declare a variable to store the grade char grade; // Take input from user printf("Enter employee grade (A, B, C, D): "); scanf("%c", &grade); // Display the grade entered printf("\n========== SALARY INFORMATION ==========\n"); printf("Employee Grade: %c\n", grade); /* ============================================ Using switch statement to display salary range ============================================ */ switch(grade) { case 'A': case 'a': printf("Salary Range: Rs. 80,000 - Rs. 120,000\n"); printf("📌 Grade A: Top-level management\n"); break; case 'B': case 'b': printf("Salary Range: Rs. 50,000 - Rs. 80,000\n"); printf("📌 Grade B: Mid-level management\n"); break; case 'C': case 'c': printf("Salary Range: Rs. 30,000 - Rs. 50,000\n"); printf("📌 Grade C: Entry-level professional\n"); break; case 'D': case 'd': printf("Salary Range: Rs. 15,000 - Rs. 30,000\n"); printf("📌 Grade D: Junior staff / Intern\n"); break; default: printf("❌ Invalid Grade!\n"); printf("📌 Please enter A, B, C, or D.\n"); break; } printf("==========================================\n"); return 0; }

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

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. char grade; — گریڈ کو ذخیرہ کرنے کے لیے ایک کریکٹر متغیر کا اعلان کرتا ہے۔
  3. scanf("%c", &grade); — صارف سے گریڈ پڑھتا ہے۔
  4. switch(grade) — گریڈ کی قیمت کی بنیاد پر سوئچ کرتا ہے۔
  5. case 'A': case 'a': — بڑے اور چھوٹے دونوں حروف کو ہینڈل کرتا ہے۔
  6. default: — غلط گریڈ کو ہینڈل کرتا ہے۔

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

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

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

./salary_grade

نمونہ ان پٹ اور آؤٹ پٹ:

Enter employee grade (A, B, C, D): A ========== SALARY INFORMATION ========== Employee Grade: A Salary Range: Rs. 80,000 - Rs. 120,000 📌 Grade A: Top-level management ==========================================

📊 گریڈ اور تنخواہ کی معلومات

گریڈ تنخواہ کی رینج (PKR) وضاحت
A Rs. 80,000 - Rs. 120,000 اعلیٰ انتظامیہ
B Rs. 50,000 - Rs. 80,000 درمیانی انتظامیہ
C Rs. 30,000 - Rs. 50,000 ابتدائی پیشہ ور
D Rs. 15,000 - Rs. 30,000 جونیئر سٹاف / انٹرن

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے گریڈ ان پٹ کے طور پر لیتا ہے۔
  • switch بیان گریڈ کو چیک کرتا ہے اور متعلقہ تنخواہ کی رینج ظاہر کرتا ہے۔
  • بڑے اور چھوٹے دونوں حروف کو ہینڈل کیا جاتا ہے۔
  • default کیس غلط گریڈ کو ہینڈل کرتا ہے۔
  • پروگرام ہر گریڈ کی سطح کی واضح وضاحت فراہم کرتا ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو switch بیان کا استعمال کرتے ہوئے ملازم کے گریڈ کی بنیاد پر تنخواہ کی رینج ظاہر کرتا ہے۔ آپ نے حقیقی دنیا کی ایپلیکیشنز جیسے تنخواہ کی درجہ بندی کے لیے switch بیان کا استعمال کرنا سیکھ لیا ہے۔

📌 اہم: switch بیان کثیر شاخی فیصلہ سازی کے لیے ایک طاقتور ٹول ہے۔ یہ خاص طور پر اس وقت مفید ہے جب آپ کو ایک ہی متغیر کا متعدد مستقل اقدار سے موازنہ کرنا ہو۔
💡 مشورہ: آپ اس پروگرام میں مزید گریڈز (مثال: E, F) یا ہر گریڈ کے لیے مزید تفصیلی معلومات (مثال: فوائد، بونس) شامل کر سکتے ہیں۔
⬅ Back to Practical Notebooks