EZ Notes Logo

EZNotes

🎯 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

  1. Open VS Code or any C compiler.
  2. Create a new file named greater_two.c.
  3. Write the following code:
/* Practical 25: Program to find the greater of two numbers */ #include <stdio.h> int main() { // Declare variables to store two numbers int num1, num2; // Take input from user printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); // Display the numbers entered printf("\n========== COMPARISON ==========\n"); printf("First Number : %d\n", num1); printf("Second Number : %d\n", num2); /* ============================================ Using if-else structure to find greater number ============================================ */ 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

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int num1, num2; — Declares two integer variables.
  3. scanf("%d", &num1); — Reads the first number from the user.
  4. scanf("%d", &num2); — Reads the second number from the user.
  5. if (num1 > num2) — Checks if the first number is greater.
  6. else if (num2 > num1) — Checks if the second number is greater.
  7. else — Both numbers are equal.

Step 3: Save, Compile, and Run

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

Run the program:

./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

  1. Test 1: 15, 10 → 15 is greater
  2. Test 2: 8, 20 → 20 is greater
  3. Test 3: -5, -10 → -5 is greater (less negative)
  4. Test 4: 0, 0 → Both are equal
  5. 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

/* Alternative 1: Using ternary operator */ int max = (num1 > num2) ? num1 : num2; printf("Greater number is: %d", max);
/* Alternative 2: Using nested if-else */ if (num1 > num2) printf("%d is greater", num1); else if (num2 > num1) printf("%d is greater", num2); else printf("Both are equal");
/* Alternative 3: Using max() function (if available) */ 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 استعمال کر سکتے ہیں۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. greater_two.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 25: Program to find the greater of two numbers */ #include <stdio.h> int main() { // Declare variables to store two numbers int num1, num2; // Take input from user printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); // Display the numbers entered printf("\n========== COMPARISON ==========\n"); printf("First Number : %d\n", num1); printf("Second Number : %d\n", num2); /* ============================================ Using if-else structure to find greater number ============================================ */ 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; }

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

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

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

  1. فائل کو محفوظ کریں (Ctrl + S)۔
  2. ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
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 بیانات کا استعمال کرتے ہوئے مزید موازنے شامل کرکے اس پروگرام کو تین یا زیادہ اعداد میں بڑا عدد تلاش کرنے کے لیے بڑھا سکتے ہیں۔
⬅ Back to Practical Notebooks