EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that takes three numbers as input from the user and displays the largest number using logical operators (&& and ||).

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of logical operators and conditional statements

📖 Introduction

In this practical, you will learn how to find the largest of three numbers using logical operators. Logical operators are used to combine multiple conditions:

  • && (AND) — Both conditions must be true
  • || (OR) — At least one condition must be true

The logic for finding the largest number:

  • If num1 > num2 && num1 > num3, then num1 is the largest.
  • Else if num2 > num1 && num2 > num3, then num2 is the largest.
  • Else num3 is the largest.
💡 Tip: The && operator checks if both conditions are true. For example, num1 > num2 && num1 > num3 means "num1 is greater than num2 AND num1 is greater than num3".

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named largest3.c.
  3. Write the following code:
/* Practical 19: Program to find largest of 3 numbers using logical operators */ #include <stdio.h> int main() { // Declare variables to store three numbers int num1, num2, num3; // Take input from user printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); printf("Enter third number: "); scanf("%d", &num3); // Display the numbers entered printf("\n========== NUMBERS ENTERED ==========\n"); printf("First Number : %d\n", num1); printf("Second Number : %d\n", num2); printf("Third Number : %d\n", num3); printf("======================================\n"); // Find the largest number using logical operators if (num1 > num2 && num1 > num3) { printf("\n✅ The largest number is: %d\n", num1); } else if (num2 > num1 && num2 > num3) { printf("\n✅ The largest number is: %d\n", num2); } else { printf("\n✅ The largest number is: %d\n", num3); } return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int num1, num2, num3; — Declares three integer variables.
  3. scanf("%d", &num1); — Reads the first number from the user.
  4. if (num1 > num2 && num1 > num3) — Checks if num1 is greater than both num2 and num3 using the AND (&&) operator.
  5. else if (num2 > num1 && num2 > num3) — Checks if num2 is the largest.
  6. else — If neither condition is true, num3 is the largest.

Step 3: Save, Compile, and Run

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

Run the program:

./largest3

Sample Input & Output:

Enter first number: 25 Enter second number: 18 Enter third number: 30 ========== NUMBERS ENTERED ========== First Number : 25 Second Number : 18 Third Number : 30 ====================================== ✅ The largest number is: 30

Step 4: Test with Different Inputs

  1. Test 1: 10, 20, 15 → Largest is 20
  2. Test 2: 45, 30, 22 → Largest is 45
  3. Test 3: 12, 8, 9 → Largest is 12
  4. Test 4: 7, 7, 5 → Largest is 7 (first or second)

📊 Logical Operators Used

Operator Name Meaning Example
&& AND Both conditions must be true num1 > num2 && num1 > num3
|| OR At least one condition must be true num1 > num2 || num1 > num3

📊 Alternative Method (Using Nested if-else)

/* Alternative: Using nested if-else without logical operators */ if (num1 > num2) { if (num1 > num3) printf("Largest is: %d", num1); else printf("Largest is: %d", num3); } else { if (num2 > num3) printf("Largest is: %d", num2); else printf("Largest is: %d", num3); }

📊 Observations

  • The program successfully takes three numbers as input from the user.
  • The && operator is used to check if one number is greater than both other numbers.
  • The if-else structure correctly identifies the largest number.
  • Logical operators make the conditions clear and easy to read.
  • The program works for all positive, negative, and equal numbers.

✅ Result

You have successfully written a C program that finds the largest of three numbers using logical operators (&&). You have learned how to combine multiple conditions in an if statement to compare three values.

📌 Important: The && operator checks if both conditions are true. If both are true, the entire condition becomes true. If either condition is false, the entire condition is false.
💡 Tip: You can extend this program to find the largest of 4, 5, or more numbers by adding more conditions with && operators.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو صارف سے تین اعداد ان پٹ کے طور پر لے اور منطقی آپریٹرز (&& اور ||) کا استعمال کرتے ہوئے سب سے بڑا عدد ظاہر کرے۔

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

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • منطقی آپریٹرز اور مشروط بیانات کی معلومات

📖 تعارف

اس عملی میں، آپ منطقی آپریٹرز کا استعمال کرتے ہوئے تین اعداد میں سے سب سے بڑا عدد تلاش کرنا سیکھیں گے۔ منطقی آپریٹرز کو متعدد شرائط کو یکجا کرنے کے لیے استعمال کیا جاتا ہے:

  • && (AND) — دونوں شرائط درست ہونی چاہئیں
  • || (OR) — کم از کم ایک شرط درست ہونی چاہیے

سب سے بڑا عدد تلاش کرنے کی منطق:

  • اگر num1 > num2 && num1 > num3، تو num1 سب سے بڑا ہے۔
  • ورنہ اگر num2 > num1 && num2 > num3، تو num2 سب سے بڑا ہے۔
  • ورنہ num3 سب سے بڑا ہے۔
💡 مشورہ: && آپریٹر چیک کرتا ہے کہ کیا دونوں شرائط درست ہیں۔ مثال کے طور پر، num1 > num2 && num1 > num3 کا مطلب ہے "num1 num2 سے بڑا ہے اور num1 num3 سے بڑا ہے"۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. largest3.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 19: Program to find largest of 3 numbers using logical operators */ #include <stdio.h> int main() { // Declare variables to store three numbers int num1, num2, num3; // Take input from user printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); printf("Enter third number: "); scanf("%d", &num3); // Display the numbers entered printf("\n========== NUMBERS ENTERED ==========\n"); printf("First Number : %d\n", num1); printf("Second Number : %d\n", num2); printf("Third Number : %d\n", num3); printf("======================================\n"); // Find the largest number using logical operators if (num1 > num2 && num1 > num3) { printf("\n✅ The largest number is: %d\n", num1); } else if (num2 > num1 && num2 > num3) { printf("\n✅ The largest number is: %d\n", num2); } else { printf("\n✅ The largest number is: %d\n", num3); } return 0; }

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

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

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

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

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

./largest3

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

Enter first number: 25 Enter second number: 18 Enter third number: 30 ========== NUMBERS ENTERED ========== First Number : 25 Second Number : 18 Third Number : 30 ====================================== ✅ The largest number is: 30

📊 استعمال شدہ منطقی آپریٹرز

آپریٹر نام معنی مثال
&& AND دونوں شرائط درست ہونی چاہئیں num1 > num2 && num1 > num3
|| OR کم از کم ایک شرط درست ہونی چاہیے num1 > num2 || num1 > num3

📊 متبادل طریقہ (نested if-else کا استعمال)

/* Alternative: Using nested if-else without logical operators */ if (num1 > num2) { if (num1 > num3) printf("Largest is: %d", num1); else printf("Largest is: %d", num3); } else { if (num2 > num3) printf("Largest is: %d", num2); else printf("Largest is: %d", num3); }

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے تین اعداد ان پٹ کے طور پر لیتا ہے۔
  • && آپریٹر کا استعمال یہ چیک کرنے کے لیے کیا جاتا ہے کہ آیا ایک عدد دوسرے دونوں اعداد سے بڑا ہے۔
  • if-else ڈھانچہ سب سے بڑے عدد کی درست شناخت کرتا ہے۔
  • منطقی آپریٹرز شرائط کو واضح اور پڑھنے میں آسان بناتے ہیں۔
  • پروگرام تمام مثبت، منفی اور مساوی اعداد کے لیے کام کرتا ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو منطقی آپریٹرز (&&) کا استعمال کرتے ہوئے تین اعداد میں سے سب سے بڑا عدد تلاش کرتا ہے۔ آپ نے سیکھ لیا ہے کہ تین قدروں کا موازنہ کرنے کے لیے if بیان میں متعدد شرائط کو کیسے یکجا کیا جاتا ہے۔

📌 اہم: && آپریٹر چیک کرتا ہے کہ آیا دونوں شرائط درست ہیں۔ اگر دونوں درست ہیں تو پوری شرط درست ہو جاتی ہے۔ اگر کوئی بھی شرط غلط ہے تو پوری شرط غلط ہے۔
💡 مشورہ: آپ && آپریٹرز کے ساتھ مزید شرائط شامل کرکے اس پروگرام کو 4، 5 یا اس سے زیادہ اعداد میں سب سے بڑا عدد تلاش کرنے کے لیے بڑھا سکتے ہیں۔
⬅ Back to Practical Notebooks