Program to Find Greatest of Three Numbers in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

We often encounter situations where we need to find the greatest number among given numbers. In C language, we can very easily do it using different forms of if and else conditions. The problem statement our article solves is: You are provided three input numbers from the user, and your objective is to write a program to find the greatest of three numbers in c.

Introduction

Syntax of if Statement:

  • Firstly, the condition inside the parentheses is evaluated by the if statement ().
    • If the condition is true, the statements within the if the body is executed.
    • If the condition is interpreted as false, the statements within the if the body is not executed.

Syntax of if-else Statements:

Here, we first check the condition written inside the parenthesis. If the condition inside the parenthesis is found to be true,

  • Statements of if block is executed.
  • Statements of else block are not executed.

If the condition inside the parenthesis is evaluated as false,

  • Statements of if block is not executed.
  • Statements of else block are executed.

Syntax of if-else Ladder:

Depending on whether the test condition is true or false, the if...else statement runs only two separate statements. But sometimes a decision must be made between more than two options, Then we use if-else ladder. It allows us to compare numerous conditions and execute various statements based on those conditions.

Syntax of Ternary Condition:

The ternary operator accepts three operands (condition, expression1, and expression2). Therefore, the term "ternary operator" is used. The condition here is a boolean expression that results in either true or false.

  • If the condition is evaluated as true - expression1 (before the colon) is executed.
  • If the condition is evaluated as false - expression2 (after the colon) is executed.

Program to Find Greatest of Three Numbers in C

Using Only if Statements

Here in this program to find the greatest of three numbers in c, we will make use of only the if statement. We will use 3 different if statements one for each number to check whether the number is the greatest or no

Code:

Output:

Explanation:

Here we implemented three separate if statements. The first determines if the number1 is the greatest number.  The second determines if the number2 is the greatest number and the similary third if statement determines if the number3 is the greatest number. The main disadvantage of this program is that it executes all three if statements, regardless of whichever number is the largest.

Time Complexity: O(1). Space Complexity: O(1).

Using if-else Statement

Here in this program to find the greatest of three numbers in c, we will use both if-else statements.

Code:

Output:

Explanation:

Outer If Statement:

  • We're looking to see if the number1 is bigger than or equal to number2. If it is, program control is transferred to the inner if...else block.
  • The inner if statement determines whether or not the number1 is bigger than or equal to number3.
  • If it is, then number1 is either equal to both number2 and number3 or bigger than both number2 and number3, i.e. number1 >= number2 >= number3. As a result, the number1 is the greatest number.
  • Otherwise, number1 is higher than or equal to number2, but less than number3, implying that number3 > number1 >= number2. As a result, the number3 is the greatest number.

Outer else Statement:

  • When number2 is greater than number1, then outer else statement is executed.
  • This program's inner if...else logic is the same as the one we previously discussed. The main difference here is that we're determining if number2 is bigger than the number3 or not.

Time Complexity: O(1). Space Complexity: O(1).

Using if-else Ladder

Here in this program to find the greatest of three numbers in c, we will use the if else ladder to make our code simpler and easy to understand.

Code:

Output:

Explanation

In the above program, when the number1 is the greatest, just the if statement is executed, and when number2 is the largest, just the else if statement is executed, and similarly when the number3 is greatest then only else block is executed.

Time Complexity: O(1). Space Complexity: O(1).

Using Ternary Condition

This example discusses a C program that uses the ternary operator to determine the greatest of three provided integers. There are three numbers n1, n2, and n3. The goal is determining which of the three numbers has the greatest value.

Code:

Output:

Explanation

  • In the outer ternary operator condition, we determine whether n1 is bigger than n2.
    • If this is true, then we see if n1 is bigger than n3.
      • If this is true, display 'n1' as the greatest number.
      • If false, display 'n3' as the greatest number.
    • If this is false, see if n2 is higher than n3.
      • If this is true, display 'n2' as the greatest number.
      • If false, display 'n3' as the largest number.

Time Complexity: O(1). Space Complexity: O(1).

Conclusion

  • Different forms of if-else statements, such as simple if, if else, or if-else ladder, can be used to find the greatest among three given numbers.
  • We could also use the ternary operator to find the greatest number among the three given numbers.
  • Time Complexity of all the above programs is O(1).
  • Space Complexity of all the above programs is O(1).