Find Greatest of Two or Three Numbers | C, C++, Java

Find Greatest of Two or Three Numbers | C, C++, Java

Find Greatest of Two or Three Numbers | C, C++, Java

Algorithm

  1. Input two numbers from the user.
  2. Compare both numbers using if-else.
  3. Print the greatest number accordingly.

Implementation in C

#include <stdio.h>

int main() {
int num1, num2;

// Input two numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Find the greatest number
if (num1 > num2) {
printf("The greatest number is: %d\n", num1);
} else if (num2 > num1) {
printf("The greatest number is: %d\n", num2);
} else {
printf("Both numbers are equal.\n");
}

return 0;
}

Explanation

  • Takes two integer inputs.
  • Uses an if-else statement to compare the numbers.
  • Prints the greater number, or states if both numbers are equal.

Finding the Greatest of Three Numbers

Algorithm

  1. Input three numbers from the user.
  2. Use if-else statements to compare the numbers.
  3. Determine the greatest number and print the result.

Implementation in C

#include <stdio.h>

int main() {
int num1, num2, num3;

// Input three numbers
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

// Find the greatest number using if-else
if (num1 >= num2 && num1 >= num3) {
printf("The greatest number is: %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The greatest number is: %d\n", num2);
} else {
printf("The greatest number is: %d\n", num3);
}

return 0;
}

Explanation

  • Takes three integer inputs.
  • Uses logical AND (&&) to check which number is the largest.
  • Prints the greatest number among the three.

Alternative Approach Using Ternary Operator

Instead of using if-else, the ternary operator (? :) can be used for concise code.

C Program to Find Greatest of Two Numbers Using Ternary Operator

#include <stdio.h>

int main() {
int num1, num2;

// Input two numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Find the greatest number using ternary operator
int greatest = (num1 > num2) ? num1 : num2;

printf("The greatest number is: %d\n", greatest);

return 0;
}

C Program to Find Greatest of Three Numbers Using Ternary Operator

#include <stdio.h>

int main() {
int num1, num2, num3;

// Input three numbers
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

// Find the greatest number using nested ternary operator
int greatest = (num1 > num2) ? ((num1 > num3) ? num1 : num3)
: ((num2 > num3) ? num2 : num3);

printf("The greatest number is: %d\n", greatest);

return 0;
}

Explanation of Ternary Operator Approach

  • Uses nested ternary operators for comparison.
  • Makes the program more compact and efficient.

Test Cases

InputExpected Output
10 20The greatest number is: 20
-5 -10The greatest number is: -5
30 30Both numbers are equal.
10 15 7The greatest number is: 15
50 25 75The greatest number is: 75
-2 -5 -10The greatest number is: -2

Complexity Analysis

ApproachTime ComplexitySpace Complexity
If-else approachO(1)O(1)
Ternary operator approachO(1)O(1)

Both approaches run in constant time O(1) since only a few comparisons are made.


Conclusion

  • The if-else method is easy to understand and implement.
  • The ternary operator method is concise and efficient.
  • Both methods provide optimal performance for determining the largest number.

These programs are frequently asked in placement interviews and are useful for building logic in competitive programming.