Find Maximum Number in Three Given Numbers

C++C++Beginner
Practice Now

Introduction

In this lab, we will write a C++ program that will take three numbers as input from the user and find the maximum among them using if/else statements.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/IOandFileHandlingGroup -.-> cpp/user_input("`User Input`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/output -.-> lab-96192{{"`Find Maximum Number in Three Given Numbers`"}} cpp/variables -.-> lab-96192{{"`Find Maximum Number in Three Given Numbers`"}} cpp/user_input -.-> lab-96192{{"`Find Maximum Number in Three Given Numbers`"}} cpp/data_types -.-> lab-96192{{"`Find Maximum Number in Three Given Numbers`"}} cpp/conditions -.-> lab-96192{{"`Find Maximum Number in Three Given Numbers`"}} cpp/function_parameters -.-> lab-96192{{"`Find Maximum Number in Three Given Numbers`"}} end

Create a new file

Create a new file in the ~/project directory named main.cpp.

touch ~/project/main.cpp

Include the necessary libraries

We need to include the iostream library to allow input and output.

#include <iostream>

Write a function to find the maximum number

We will write a function that will take three numbers as input and return the maximum number among them. We will use if/else statements to compare the three numbers and find out the maximum number.

int findMax(int num1, int num2, int num3) {
    int max = num1;

    if (num2 > max) {
        max = num2;
    }

    if (num3 > max) {
        max = num3;
    }

    return max;
}

Write the main function

In the main function, we will prompt the user to enter three numbers and then call the findMax function to find the maximum number.

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

    std::cout << "Enter the three numbers: ";
    std::cin >> num1 >> num2 >> num3;

    std::cout << "The maximum number is: " << findMax(num1, num2, num3) << std::endl;

    return 0;
}

Compile and run the program

To compile the program, open the terminal and navigate to the ~/project directory. Then, run the following command:

g++ main.cpp -o main && ./main

You will see the following output:

Enter the three numbers: 10 20 30
The maximum number is: 30

Full code

Here is the full code for the main.cpp file:

#include <iostream>

int findMax(int num1, int num2, int num3) {
    int max = num1;

    if (num2 > max) {
        max = num2;
    }

    if (num3 > max) {
        max = num3;
    }

    return max;
}

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

    std::cout << "Enter the three numbers: ";
    std::cin >> num1 >> num2 >> num3;

    std::cout << "The maximum number is: " << findMax(num1, num2, num3) << std::endl;

    return 0;
}

Summary

In this lab, we learned how to create a C++ program that can find the maximum among three given numbers using if/else statements. We also learned how to write a function to find the maximum number and how to use input/output statements to prompt the user to enter the numbers and display the result.

Other C++ Tutorials you may like