Largest Among 3

Program to find the largest of three numbers

BeginnerTopic: Conditional Programs
Back

C++ Largest Among 3 Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int a, b, c, largest;
    
    cout << "Enter three numbers: ";
    cin >> a >> b >> c;
    
    if (a >= b && a >= c) {
        largest = a;
    } else if (b >= a && b >= c) {
        largest = b;
    } else {
        largest = c;
    }
    
    cout << "Largest number is: " << largest << endl;
    
    return 0;
}
Output
Enter three numbers: 10 25 15
Largest number is: 25

Understanding Largest Among 3

This program uses nested if-else statements to compare three numbers. We check each number against the other two using logical AND (&&) operator. The number that is greater than or equal to both others is the largest.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Table of Contents