🎯 Aim
To write a C/C++ program that takes two numbers as input from the user and finds the greater (larger) number using the if-else structure.
🛠️ What You Need
- Visual Studio Code or any C/C++ compiler
- Knowledge of if-else statements and comparison operators
📖 Introduction
In this practical, you will compare two numbers entered by the user and determine which one is greater. This is a common programming task that helps you understand decision-making in C/C++.
The logic for finding the greater number is:
- If num1 > num2, then num1 is greater.
- If num2 > num1, then num2 is greater.
- If num1 == num2, then both numbers are equal.
You will use:
- Comparison operators (
>, <, ==)
- if-else statements to compare the numbers
- printf() to display the result
💡 Tip: The if-else structure is perfect for comparing two values. You can use else if to handle the equal case.
📝 Steps
Step 1: Write the Program
- Open VS Code or any C compiler.
- Create a new file named greater_two.c.
- Write the following code:
#include <stdio.h>
int main()
{
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("\n========== COMPARISON ==========\n");
printf("First Number : %d\n", num1);
printf("Second Number : %d\n", num2);
if (num1 > num2)
{
printf("\n✅ %d is GREATER than %d.\n", num1, num2);
}
else if (num2 > num1)
{
printf("\n✅ %d is GREATER than %d.\n", num2, num1);
}
else
{
printf("\n✅ Both numbers are EQUAL.\n");
printf("📌 %d == %d\n", num1, num2);
}
printf("==================================\n");
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- int num1, num2; — Declares two integer variables.
- scanf("%d", &num1); — Reads the first number from the user.
- scanf("%d", &num2); — Reads the second number from the user.
- if (num1 > num2) — Checks if the first number is greater.
- else if (num2 > num1) — Checks if the second number is greater.
- else — Both numbers are equal.
Step 3: Save, Compile, and Run
- Save the file (Ctrl + S).
- Open the terminal and compile using the command:
gcc greater_two.c -o greater_two
./greater_two
Sample Input & Output:
Enter first number: 25
Enter second number: 18
========== COMPARISON ==========
First Number : 25
Second Number : 18
✅ 25 is GREATER than 18.
==================================
Enter first number: 12
Enter second number: 30
========== COMPARISON ==========
First Number : 12
Second Number : 30
✅ 30 is GREATER than 12.
==================================
Enter first number: 20
Enter second number: 20
========== COMPARISON ==========
First Number : 20
Second Number : 20
✅ Both numbers are EQUAL.
📌 20 == 20
==================================
Step 4: Test with Different Inputs
- Test 1: 15, 10 → 15 is greater
- Test 2: 8, 20 → 20 is greater
- Test 3: -5, -10 → -5 is greater (less negative)
- Test 4: 0, 0 → Both are equal
- Test 5: -3, 5 → 5 is greater
📊 Comparison Operators Used
| Operator |
Meaning |
Example |
> |
Greater than |
num1 > num2 |
< |
Less than |
num1 < num2 |
== |
Equal to |
num1 == num2 |
>= |
Greater than or equal to |
num1 >= num2 |
<= |
Less than or equal to |
num1 <= num2 |
📊 Alternative Methods
int max = (num1 > num2) ? num1 : num2;
printf("Greater number is: %d", max);
if (num1 > num2)
printf("%d is greater", num1);
else if (num2 > num1)
printf("%d is greater", num2);
else
printf("Both are equal");
int max = max(num1, num2);
printf("Greater number is: %d", max);
📊 Observations
- The program successfully takes two numbers as input from the user.
- It compares the numbers using the
if-else structure.
- It correctly identifies the greater number or handles the equal case.
- Comparison operators (
>, ==) are used to compare the values.
- The program works for positive, negative, and zero values.
✅ Result
You have successfully written a C program that finds the greater of two numbers entered by the user. You have learned how to use the if-else structure with comparison operators to make decisions in your program.
📌 Important: The if-else structure is used to compare two numbers. The condition num1 > num2 checks if the first number is greater than the second number.
💡 Tip: You can extend this program to find the greater of three or more numbers by adding more comparisons using else if or nested if statements.
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو صارف سے دو اعداد ان پٹ کے طور پر لے اور if-else ڈھانچے کا استعمال کرتے ہوئے بڑا عدد تلاش کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- if-else بیانات اور موازنہ آپریٹرز کی معلومات
📖 تعارف
اس عملی میں، آپ صارف کے درج کردہ دو اعداد کا موازنہ کریں گے اور یہ تعین کریں گے کہ کون سا بڑا ہے۔ یہ ایک عام پروگرامنگ کام ہے جو آپ کو C/C++ میں فیصلہ سازی کو سمجھنے میں مدد کرتا ہے۔
بڑا عدد تلاش کرنے کی منطق یہ ہے:
- اگر num1 > num2، تو num1 بڑا ہے۔
- اگر num2 > num1، تو num2 بڑا ہے۔
- اگر num1 == num2، تو دونوں اعداد برابر ہیں۔
آپ استعمال کریں گے:
- موازنہ آپریٹرز (
>، <، ==)
- اعداد کا موازنہ کرنے کے لیے if-else بیانات
- نتیجہ ظاہر کرنے کے لیے printf()
💡 مشورہ: if-else ڈھانچہ دو اقدار کا موازنہ کرنے کے لیے بہترین ہے۔ آپ برابر کی صورت کو ہینڈل کرنے کے لیے else if استعمال کر سکتے ہیں۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- greater_two.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("\n========== COMPARISON ==========\n");
printf("First Number : %d\n", num1);
printf("Second Number : %d\n", num2);
if (num1 > num2)
{
printf("\n✅ %d is GREATER than %d.\n", num1, num2);
}
else if (num2 > num1)
{
printf("\n✅ %d is GREATER than %d.\n", num2, num1);
}
else
{
printf("\n✅ Both numbers are EQUAL.\n");
printf("📌 %d == %d\n", num1, num2);
}
printf("==================================\n");
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- int num1, num2; — دو انٹیجر متغیرات کا اعلان کرتا ہے۔
- scanf("%d", &num1); — صارف سے پہلا عدد پڑھتا ہے۔
- scanf("%d", &num2); — صارف سے دوسرا عدد پڑھتا ہے۔
- if (num1 > num2) — چیک کرتا ہے کہ آیا پہلا عدد بڑا ہے۔
- else if (num2 > num1) — چیک کرتا ہے کہ آیا دوسرا عدد بڑا ہے۔
- else — دونوں اعداد برابر ہیں۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc greater_two.c -o greater_two
./greater_two
نمونہ ان پٹ اور آؤٹ پٹ:
Enter first number: 25
Enter second number: 18
========== COMPARISON ==========
First Number : 25
Second Number : 18
✅ 25 is GREATER than 18.
==================================
📊 موازنہ آپریٹرز کا استعمال
| آپریٹر |
معنی |
مثال |
> |
بڑا ہے |
num1 > num2 |
< |
چھوٹا ہے |
num1 < num2 |
== |
برابر ہے |
num1 == num2 |
>= |
بڑا یا برابر ہے |
num1 >= num2 |
<= |
چھوٹا یا برابر ہے |
num1 <= num2 |
📊 مشاہدات
- پروگرام کامیابی سے صارف سے دو اعداد ان پٹ کے طور پر لیتا ہے۔
- یہ
if-else ڈھانچے کا استعمال کرتے ہوئے اعداد کا موازنہ کرتا ہے۔
- یہ بڑے عدد کی درست شناخت کرتا ہے یا برابر کی صورت کو ہینڈل کرتا ہے۔
- موازنہ آپریٹرز (
>، ==) اقدار کا موازنہ کرنے کے لیے استعمال ہوتے ہیں۔
- پروگرام مثبت، منفی اور صفر اقدار کے لیے کام کرتا ہے۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو صارف کے درج کردہ دو اعداد میں سے بڑا عدد تلاش کرتا ہے۔ آپ نے if-else ڈھانچے کو موازنہ آپریٹرز کے ساتھ استعمال کرتے ہوئے اپنے پروگرام میں فیصلے کرنا سیکھ لیا ہے۔
📌 اہم: if-else ڈھانچہ دو اعداد کا موازنہ کرنے کے لیے استعمال ہوتا ہے۔ شرط num1 > num2 چیک کرتی ہے کہ آیا پہلا عدد دوسرے عدد سے بڑا ہے۔
💡 مشورہ: آپ else if یا nested if بیانات کا استعمال کرتے ہوئے مزید موازنے شامل کرکے اس پروگرام کو تین یا زیادہ اعداد میں بڑا عدد تلاش کرنے کے لیے بڑھا سکتے ہیں۔