EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that takes marks as input from the user and checks whether the student has passed (marks >= 40) or failed (marks < 40).

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Basic knowledge of conditional statements (if-else)

📖 Introduction

In many educational systems, the passing marks are set at 40 out of 100.

  • Pass: Marks >= 40
  • Fail: Marks < 40

In this practical, you will write a program that:

  • Takes marks as input from the user
  • Checks if the marks are greater than or equal to 40
  • Displays "PASS" if the condition is true, otherwise "FAIL"
  • Also checks for invalid input (marks > 100 or marks < 0)
💡 Tip: You can use the if-else statement to check conditions. The condition marks >= 40 checks if the student has passed.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named pass_fail.c.
  3. Write the following code:
/* Practical 22: Program to check pass or fail */ #include <stdio.h> int main() { // Declare a variable to store marks int marks; // Take input from user printf("Enter marks (out of 100): "); scanf("%d", &marks); // Display the marks entered printf("\n========== RESULT ==========\n"); printf("Marks Obtained: %d\n", marks); // Check for invalid marks if (marks > 100 || marks < 0) { printf("⚠️ Invalid marks! Please enter marks between 0 and 100.\n"); } // Check if student has passed else if (marks >= 40) { printf("✅ PASS\n"); printf("🎉 Congratulations! You have passed.\n"); } // Student has failed else { printf("❌ FAIL\n"); printf("😞 Sorry! You have failed. Better luck next time.\n"); } printf("============================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int marks; — Declares an integer variable to store marks.
  3. scanf("%d", &marks); — Reads marks from the user.
  4. if (marks > 100 || marks < 0) — Checks for invalid marks.
  5. else if (marks >= 40) — Checks if marks are 40 or more (PASS).
  6. else — Marks are less than 40 (FAIL).

Step 3: Save, Compile, and Run

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

Run the program:

./pass_fail

Sample Input & Output:

Enter marks (out of 100): 75 ========== RESULT ========== Marks Obtained: 75 ✅ PASS 🎉 Congratulations! You have passed. ============================
Enter marks (out of 100): 35 ========== RESULT ========== Marks Obtained: 35 ❌ FAIL 😞 Sorry! You have failed. Better luck next time. ============================
Enter marks (out of 100): 150 ========== RESULT ========== Marks Obtained: 150 ⚠️ Invalid marks! Please enter marks between 0 and 100. ============================

Step 4: Test with Different Inputs

  1. Test 1: 85 → PASS
  2. Test 2: 40 → PASS (exactly passing marks)
  3. Test 3: 39 → FAIL
  4. Test 4: 0 → FAIL
  5. Test 5: 100 → PASS
  6. Test 6: -5 → Invalid
  7. Test 7: 105 → Invalid

📊 Passing Criteria

Marks Range Result Grade (Optional)
90 - 100 ✅ PASS A+
80 - 89 ✅ PASS A
70 - 79 ✅ PASS B
60 - 69 ✅ PASS C
40 - 59 ✅ PASS D
0 - 39 ❌ FAIL F
< 0 or > 100 ⚠️ Invalid -

📊 Alternative Method (Using Ternary Operator)

/* Alternative: Using ternary operator */ (marks >= 40) ? printf("PASS") : printf("FAIL");

📊 Observations

  • The program successfully takes marks as input from the user.
  • It checks if marks are between 0 and 100 (valid range).
  • It displays "PASS" if marks are greater than or equal to 40.
  • It displays "FAIL" if marks are less than 40.
  • The program handles invalid input appropriately.

✅ Result

You have successfully written a C program that checks whether a student has passed or failed based on their marks. You have learned how to use if-else statements to make decisions and handle invalid input.

📌 Important: The condition marks >= 40 checks if the student has passed. You can change the passing marks to any other value by modifying this condition.
💡 Tip: You can extend this program to assign grades based on marks (e.g., A, B, C, D, F) using nested if-else or else-if statements.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو صارف سے نمبرز ان پٹ کے طور پر لے اور چیک کرے کہ آیا طالب علم پاس ہوا ہے (نمبرز >= 40) یا فیل (نمبرز < 40)۔

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

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • مشروط بیانات (if-else) کی بنیادی معلومات

📖 تعارف

بہت سے تعلیمی نظاموں میں، پاسنگ نمبرز 40 میں سے 100 مقرر کیے گئے ہیں۔

  • پاس (Pass): نمبرز >= 40
  • فیل (Fail): نمبرز < 40

اس عملی میں، آپ ایک پروگرام لکھیں گے جو:

  • صارف سے نمبرز ان پٹ کے طور پر لیتا ہے
  • چیک کرتا ہے کہ آیا نمبرز 40 سے زیادہ یا برابر ہیں
  • "PASS" ظاہر کرتا ہے اگر شرط درست ہے، ورنہ "FAIL"
  • غلط ان پٹ (نمبرز > 100 یا نمبرز < 0) کو بھی چیک کرتا ہے
💡 مشورہ: آپ شرائط کو چیک کرنے کے لیے if-else بیان کا استعمال کر سکتے ہیں۔ شرط marks >= 40 چیک کرتی ہے کہ آیا طالب علم پاس ہوا ہے۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. pass_fail.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 22: Program to check pass or fail */ #include <stdio.h> int main() { // Declare a variable to store marks int marks; // Take input from user printf("Enter marks (out of 100): "); scanf("%d", &marks); // Display the marks entered printf("\n========== RESULT ==========\n"); printf("Marks Obtained: %d\n", marks); // Check for invalid marks if (marks > 100 || marks < 0) { printf("⚠️ Invalid marks! Please enter marks between 0 and 100.\n"); } // Check if student has passed else if (marks >= 40) { printf("✅ PASS\n"); printf("🎉 Congratulations! You have passed.\n"); } // Student has failed else { printf("❌ FAIL\n"); printf("😞 Sorry! You have failed. Better luck next time.\n"); } printf("============================\n"); return 0; }

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

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. int marks; — نمبرز کو ذخیرہ کرنے کے لیے ایک انٹیجر متغیر کا اعلان کرتا ہے۔
  3. scanf("%d", &marks); — صارف سے نمبرز پڑھتا ہے۔
  4. if (marks > 100 || marks < 0) — غلط نمبرز کو چیک کرتا ہے۔
  5. else if (marks >= 40) — چیک کرتا ہے کہ آیا نمبرز 40 یا اس سے زیادہ ہیں (پاس)۔
  6. else — نمبرز 40 سے کم ہیں (فیل)۔

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

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

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

./pass_fail

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

Enter marks (out of 100): 75 ========== RESULT ========== Marks Obtained: 75 ✅ PASS 🎉 Congratulations! You have passed. ============================
Enter marks (out of 100): 35 ========== RESULT ========== Marks Obtained: 35 ❌ FAIL 😞 Sorry! You have failed. Better luck next time. ============================

📊 پاسنگ کا معیار

نمبرز کی حد نتیجہ گریڈ (اختیاری)
90 - 100 ✅ پاس A+
80 - 89 ✅ پاس A
70 - 79 ✅ پاس B
60 - 69 ✅ پاس C
40 - 59 ✅ پاس D
0 - 39 ❌ فیل F
< 0 یا > 100 ⚠️ غلط -

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے نمبرز ان پٹ کے طور پر لیتا ہے۔
  • یہ چیک کرتا ہے کہ آیا نمبرز 0 اور 100 کے درمیان ہیں (درست رینج)۔
  • یہ "PASS" ظاہر کرتا ہے اگر نمبرز 40 سے زیادہ یا برابر ہیں۔
  • یہ "FAIL" ظاہر کرتا ہے اگر نمبرز 40 سے کم ہیں۔
  • پروگرام غلط ان پٹ کو مناسب طریقے سے ہینڈل کرتا ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو طالب علم کے نمبرز کی بنیاد پر چیک کرتا ہے کہ آیا وہ پاس ہوا ہے یا فیل۔ آپ نے if-else بیانات کا استعمال کرتے ہوئے فیصلے کرنا اور غلط ان پٹ کو ہینڈل کرنا سیکھ لیا ہے۔

📌 اہم: شرط marks >= 40 چیک کرتی ہے کہ آیا طالب علم پاس ہوا ہے۔ آپ اس شرط کو تبدیل کرکے پاسنگ نمبرز کو کسی بھی دوسری قیمت میں تبدیل کر سکتے ہیں۔
💡 مشورہ: آپ اس پروگرام کو if-else یا else-if بیانات کا استعمال کرتے ہوئے نمبرز کی بنیاد پر گریڈز تفویض کرنے کے لیے بڑھا سکتے ہیں (مثال: A، B، C، D، F)۔
⬅ Back to Practical Notebooks