🎯 Aim
To write a C/C++ program that takes three integers as input from the user and finds the greatest (largest) among them using the if-else structure and logical operators.
🛠️ What You Need
- Visual Studio Code or any C/C++ compiler
- Knowledge of if-else statements, logical AND (&&), and comparison operators
📖 Introduction
In this practical, you will compare three integers entered by the user and determine which one is the greatest (largest). This is a very common programming problem that helps you understand nested conditions and logical thinking.
The logic for finding the greatest of three numbers is:
- If num1 > num2 && num1 > num3, then num1 is the greatest.
- Else if num2 > num1 && num2 > num3, then num2 is the greatest.
- Else, num3 is the greatest.
You will use:
- Comparison operators (
>, ==)
- Logical AND (&&) operator to combine conditions
- if-else if-else statements
💡 Tip: The logical AND (&&) 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
- Open VS Code or any C compiler.
- Create a new file named greatest_three.c.
- Write the following code:
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("Enter third number: ");
scanf("%d", &num3);
printf("\n========== COMPARISON ==========\n");
printf("First Number : %d\n", num1);
printf("Second Number : %d\n", num2);
printf("Third Number : %d\n", num3);
if (num1 > num2 && num1 > num3)
{
printf("\n✅ %d is the GREATEST number.\n", num1);
}
else if (num2 > num1 && num2 > num3)
{
printf("\n✅ %d is the GREATEST number.\n", num2);
}
else
{
printf("\n✅ %d is the GREATEST number.\n", num3);
}
printf("==================================\n");
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- int num1, num2, num3; — Declares three integer variables.
- scanf() — Reads three numbers from the user.
- if (num1 > num2 && num1 > num3) — Checks if num1 is greater than both num2 and num3.
- else if (num2 > num1 && num2 > num3) — Checks if num2 is greater than both num1 and num3.
- else — If both conditions are false, num3 is the greatest.
Step 3: Save, Compile, and Run
- Save the file (Ctrl + S).
- Open the terminal and compile using the command:
gcc greatest_three.c -o greatest_three
./greatest_three
Sample Input & Output:
Enter first number: 25
Enter second number: 18
Enter third number: 30
========== COMPARISON ==========
First Number : 25
Second Number : 18
Third Number : 30
✅ 30 is the GREATEST number.
==================================
Enter first number: 45
Enter second number: 22
Enter third number: 30
========== COMPARISON ==========
First Number : 45
Second Number : 22
Third Number : 30
✅ 45 is the GREATEST number.
==================================
Enter first number: 12
Enter second number: 35
Enter third number: 20
========== COMPARISON ==========
First Number : 12
Second Number : 35
Third Number : 20
✅ 35 is the GREATEST number.
==================================
Step 4: Test with Different Inputs
- Test 1: 10, 20, 15 → Greatest is 20
- Test 2: 30, 25, 22 → Greatest is 30
- Test 3: 12, 8, 9 → Greatest is 12
- Test 4: -5, 0, 5 → Greatest is 5
- Test 5: -10, -20, -5 → Greatest is -5 (less negative)
- Test 6: 7, 7, 5 → Greatest is 7
📊 Comparison and Logical Operators Used
| Operator |
Type |
Meaning |
Example |
> |
Comparison |
Greater than |
num1 > num2 |
&& |
Logical AND |
Both conditions must be true |
num1 > num2 && num1 > num3 |
📊 Alternative Methods
int greatest = num1;
if (num2 > greatest)
greatest = num2;
if (num3 > greatest)
greatest = num3;
printf("Greatest is: %d", greatest);
int greatest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
printf("Greatest is: %d", greatest);
int max(int a, int b, int c)
{
if (a >= b && a >= c) return a;
else if (b >= a && b >= c) return b;
else return c;
}
📊 Observations
- The program successfully takes three integers as input from the user.
- It compares the numbers using the
if-else structure.
- The logical AND (
&&) operator is used to check if one number is greater than both others.
- It correctly identifies the greatest number.
- The program works for positive, negative, zero, and equal numbers.
✅ Result
You have successfully written a C program that finds and prints the greatest of three integers entered by the user. You have learned how to use the if-else structure with logical AND (&&) operator to compare three values and find the maximum.
📌 Important: The logical AND (&&) operator is used to combine multiple conditions. The condition num1 > num2 && num1 > num3 checks if num1 is greater than both num2 and num3.
💡 Tip: You can extend this program to find the greatest of four or more numbers by adding more comparisons using else if or by using a loop.
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو صارف سے تین انٹیجرز ان پٹ کے طور پر لے اور if-else ڈھانچے اور منطقی آپریٹرز کا استعمال کرتے ہوئے ان میں سے سب سے بڑا عدد تلاش کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- if-else بیانات، منطقی AND (&&)، اور موازنہ آپریٹرز کی معلومات
📖 تعارف
اس عملی میں، آپ صارف کے درج کردہ تین انٹیجرز کا موازنہ کریں گے اور یہ تعین کریں گے کہ ان میں سے سب سے بڑا کون سا ہے۔ یہ ایک بہت عام پروگرامنگ مسئلہ ہے جو آپ کو نested شرائط اور منطقی سوچ کو سمجھنے میں مدد کرتا ہے۔
تین اعداد میں سب سے بڑا عدد تلاش کرنے کی منطق یہ ہے:
- اگر num1 > num2 && num1 > num3، تو num1 سب سے بڑا ہے۔
- ورنہ اگر num2 > num1 && num2 > num3، تو num2 سب سے بڑا ہے۔
- ورنہ، num3 سب سے بڑا ہے۔
آپ استعمال کریں گے:
- موازنہ آپریٹرز (
>)
- شرائط کو یکجا کرنے کے لیے منطقی AND (&&) آپریٹر
- if-else if-else بیانات
💡 مشورہ: منطقی AND (&&) آپریٹر چیک کرتا ہے کہ آیا دونوں شرائط درست ہیں۔ مثال کے طور پر، num1 > num2 && num1 > num3 کا مطلب ہے "num1 num2 سے بڑا ہے اور num1 num3 سے بڑا ہے"۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- greatest_three.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("Enter third number: ");
scanf("%d", &num3);
printf("\n========== COMPARISON ==========\n");
printf("First Number : %d\n", num1);
printf("Second Number : %d\n", num2);
printf("Third Number : %d\n", num3);
if (num1 > num2 && num1 > num3)
{
printf("\n✅ %d is the GREATEST number.\n", num1);
}
else if (num2 > num1 && num2 > num3)
{
printf("\n✅ %d is the GREATEST number.\n", num2);
}
else
{
printf("\n✅ %d is the GREATEST number.\n", num3);
}
printf("==================================\n");
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- int num1, num2, num3; — تین انٹیجر متغیرات کا اعلان کرتا ہے۔
- scanf() — صارف سے تین اعداد پڑھتا ہے۔
- if (num1 > num2 && num1 > num3) — چیک کرتا ہے کہ کیا num1 num2 اور num3 دونوں سے بڑا ہے۔
- else if (num2 > num1 && num2 > num3) — چیک کرتا ہے کہ کیا num2 num1 اور num3 دونوں سے بڑا ہے۔
- else — اگر دونوں شرائط غلط ہیں تو num3 سب سے بڑا ہے۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc greatest_three.c -o greatest_three
./greatest_three
نمونہ ان پٹ اور آؤٹ پٹ:
Enter first number: 25
Enter second number: 18
Enter third number: 30
========== COMPARISON ==========
First Number : 25
Second Number : 18
Third Number : 30
✅ 30 is the GREATEST number.
==================================
📊 موازنہ اور منطقی آپریٹرز کا استعمال
| آپریٹر |
قسم |
معنی |
مثال |
> |
موازنہ |
بڑا ہے |
num1 > num2 |
&& |
منطقی AND |
دونوں شرائط درست ہونی چاہئیں |
num1 > num2 && num1 > num3 |
📊 مشاہدات
- پروگرام کامیابی سے صارف سے تین انٹیجرز ان پٹ کے طور پر لیتا ہے۔
- یہ
if-else ڈھانچے کا استعمال کرتے ہوئے اعداد کا موازنہ کرتا ہے۔
- منطقی AND (
&&) آپریٹر کا استعمال یہ چیک کرنے کے لیے کیا جاتا ہے کہ آیا ایک عدد دوسرے دونوں اعداد سے بڑا ہے۔
- یہ سب سے بڑے عدد کی درست شناخت کرتا ہے۔
- پروگرام مثبت، منفی، صفر اور مساوی اعداد کے لیے کام کرتا ہے۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو صارف کے درج کردہ تین انٹیجرز میں سے سب سے بڑا عدد تلاش کرتا ہے اور پرنٹ کرتا ہے۔ آپ نے if-else ڈھانچے کو منطقی AND (&&) آپریٹر کے ساتھ استعمال کرتے ہوئے تین اقدار کا موازنہ کرنا سیکھ لیا ہے۔
📌 اہم: منطقی AND (&&) آپریٹر کا استعمال متعدد شرائط کو یکجا کرنے کے لیے کیا جاتا ہے۔ شرط num1 > num2 && num1 > num3 چیک کرتی ہے کہ کیا num1 num2 اور num3 دونوں سے بڑا ہے۔
💡 مشورہ: آپ else if کا استعمال کرتے ہوئے مزید موازنے شامل کرکے یا لوپ کا استعمال کرکے اس پروگرام کو چار یا زیادہ اعداد میں سب سے بڑا عدد تلاش کرنے کے لیے بڑھا سکتے ہیں۔