Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Program to Find Largest of 3 Numbers

Last Updated on April 20, 2023 by Prepbytes

Finding the largest among 3 numbers is a common programming task. In order to solve this problem, there are several approaches that can be used. In this article, we will discuss all the approaches to writing a program to find the largest of 3 numbers. By going through this article, you will be able to choose the best approach that suits your programming style and requirements.

How to Find Largest of 3 Numbers

Write a program to find largest of 3 numbers. The program should take three input numbers from the user and output the largest of the three numbers.

Input:

10 20 12

Output:

20

Input:

2 8 1

Output:

8

Algorithm of the Program To Find Largest of 3 Numbers

Here’s the algorithm of the program to find largest of 3 numbers:

  • Step 1 – Start
  • Step 2 – Initialize three integer variables as num1, num2, and num3 to store three input numbers.
  • Step 3 – Read three integer numbers num1, num2, and num3 from the user.
  • Step 4 – Compare num1 with num2 and num3 to find the largest of num1, num2, and num3.
    • 4.1 – If num1 is greater than num2 and num3, then output "num1 is the largest number".
    • 4.2 – Otherwise, compare num2 and num3 to find the largest of the two numbers.
    • 4.3 – If num2 is greater than num3, then output " num2 is the largest number".
    • 4.4 – Otherwise, output " num3 is the largest number".
  • Step 5 – Stop.

Flowchart of the Program To Find Largest of 3 Numbers

Different Approaches to Find Largest of 3 Numbers

In this section, we will learn how to create a program to find largest of 3 numbers using various approaches.

Approach 1 – Using Only If Statements to Find the Largest of 3 Numbers
Here we will learn how to create a program to find largest of 3 numbers using only If Statements.

#include <stdio.h>

int main() {
    int num1, num2, num3, largest;
    
    scanf("%d %d %d", &num1, &num2, &num3);

    if (num1 > num2 && num1 > num3) {
        largest = num1;
    }
    if (num2 > num1 && num2 > num3) {
        largest = num2;
    }
    if (num3 > num1 && num3 > num2) {
        largest = num3;
    }

    printf("The largest of the three numbers is %d\n", largest);
    return 0;
}
#include <iostream>
using namespace std;

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

    cin >> num1 >> num2 >> num3;

    if (num1 > num2 && num1 > num3) {
        largest = num1;
    }
    if (num2 > num1 && num2 > num3) {
        largest = num2;
    }
    if (num3 > num1 && num3 > num2) {
        largest = num3;
    }

    cout << "The largest of the three numbers is " << largest << endl;
    return 0;
}
import java.util.Scanner;

class PrepBytes {

    public static void main(String[] args) {
        int num1, num2, num3, largest = Integer.MIN_VALUE;
        Scanner input = new Scanner(System.in);

        num1 = input.nextInt();
        num2 = input.nextInt();
        num3 = input.nextInt();

        if (num1 > num2 && num1 > num3) {
            largest = num1;
        }
        if (num2 > num1 && num2 > num3) {
            largest = num2;
        }
        if (num3 > num1 && num3 > num2) {
            largest = num3;
        }

        System.out.println("The largest of the three numbers is " + largest);
    }
}
num1 = int(input())
num2 = int(input())
num3 = int(input())

if num1 > num2 and num1 > num3:
    largest = num1
if num2 > num1 and num2 > num3:
    largest = num2
if num3 > num1 and num3 > num2:
    largest = num3

print("The largest of the three numbers is", largest)

Output:

The largest of the three numbers is 20

Explanation: In the above approach, the program first reads the user inputs and stores them in variables num1, num2, and num3. Then, the program compares these three numbers using a series of if statements to find the largest number among them. Finally, the program displays the largest number to the user.

Approach 2 – Using the If-Else Statement to Find Largest of 3 Numbers
Here we will learn how to create a program to find largest of 3 numbers using if-else statements.

#include <stdio.h>

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

    scanf("%d %d %d", &num1, &num2, &num3);

    if (num1 > num2 && num1 > num3) {
        largest = num1;
    } else if (num2 > num1 && num2 > num3) {
        largest = num2;
    } else {
        largest = num3;
    }

    printf("The largest of the three numbers is %d\n", largest);
    return 0;
}
#include <iostream>
using namespace std;

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

    cin >> num1 >> num2 >> num3;

    if (num1 > num2 && num1 > num3) {
        largest = num1;
    } else if (num2 > num1 && num2 > num3) {
        largest = num2;
    } else {
        largest = num3;
    }

    cout << "The largest of the three numbers is " << largest << endl;
    return 0;
}
import java.util.Scanner;

class PrepBytes {

    public static void main(String[] args) {
        int num1, num2, num3, largest;
        Scanner input = new Scanner(System.in);

        num1 = input.nextInt();
        num2 = input.nextInt();
        num3 = input.nextInt();

        if (num1 > num2 && num1 > num3) {
            largest = num1;
        } else if (num2 > num1 && num2 > num3) {
            largest = num2;
        } else {
            largest = num3;
        }

        System.out.println("The largest of the three numbers is " + largest);
    }
}
num1 = int(input())
num2 = int(input())
num3 = int(input())

if num1 > num2 and num1 > num3:
    largest = num1
elif num2 > num1 and num2 > num3:
    largest = num2
else:
    largest = num3

print("The largest of the three numbers is", largest)

Output:

The largest of the three numbers is 20

Explanation: In this approach, the program first checks whether the first number is greater than the other two numbers. If it is, then it assigns the first number as the largest number. If the first number is not the largest, then the program checks whether the second number is greater than the other two numbers. If it is, then it assigns the second number as the largest number. If neither the first nor the second number is the largest, then the program assigns the third number as the largest number.

Approach 3 – Using the Ternary Operator to Find Largest of 3 Numbers
Here we will see how to create a program to find largest of 3 numbers using ternary operator.

#include <stdio.h>
int main() {
  int num1, num2, num3, largest;
  scanf("%d %d %d", &num1, &num2, &num3);
  largest = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
  printf("Largest number is %d", largest);
  return 0;
}
#include <iostream>
using namespace std;
int main() {
  int num1, num2, num3, largest;
  cin >> num1 >> num2 >> num3;
  largest = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
  cout << "Largest number is " << largest;
  return 0;
}
import java.util.Scanner;

class PrepBytes {
  public static void main(String[] args) {
    int num1, num2, num3, largest;
    Scanner input = new Scanner(System.in);
    num1 = input.nextInt();
    num2 = input.nextInt();
    num3 = input.nextInt();
    largest = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
    System.out.println("Largest number is " + largest);
  }
}
num1, num2, num3 = map(int, input().split())
largest = num1 if num1 > num2 and num1 > num3 else num2 if num2 > num3 else num3
print("Largest number is", largest)

Output:

Largest number is 20

Explanation: In this approach, we use a ternary operator to compare three given numbers and find the largest among them. The ternary operator takes three operands and returns one of two values based on a given condition. We use nested ternary operators to compare the numbers and return the largest one. If the condition is true, the first value is returned, and if it is false, the second value is returned. We take input from the user in three variables, num1, num2, and num3. This approach is concise and uses a single line of code to find the largest of three numbers.

Conclusion
In this article, we discussed the program to find the largest of 3 numbers using different approaches such as only If, If-Else, and Ternary Operator. By going through this article, readers can choose the most suitable approach for their program and implement it in their code.

FAQs

Here are some frequently asked questions related to this topic.

Q1: Which is the most efficient approach to find the largest of 3 numbers?
Ans: The ternary operator is considered to be the most efficient approach to find the largest of 3 numbers as it uses a single line of code.

Q2: Can we find the largest of 3 numbers using a switch statement?
Ans: No, we cannot use a switch statement to find the largest of 3 numbers.

Q3: What will happen if two or more numbers are the largest in the given set of 3 numbers?
Ans: In this case, the program will return any of the largest numbers.

Q4: Can we find the largest of 3 numbers without using conditional statements?
Ans: No, we cannot find the largest of 3 numbers without using conditional statements.

Q5: What can we do if the given numbers are very large and cannot fit in the variable data type?
Ans: In this case, we can use big number libraries to handle such large numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *