C++ Program to Find Greatest Number

C++C++Beginner
Practice Now

Introduction

In this lab, we will write a C++ program to find the greatest of three numbers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/IOandFileHandlingGroup -.-> cpp/user_input("`User Input`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") subgraph Lab Skills cpp/output -.-> lab-96240{{"`C++ Program to Find Greatest Number`"}} cpp/comments -.-> lab-96240{{"`C++ Program to Find Greatest Number`"}} cpp/variables -.-> lab-96240{{"`C++ Program to Find Greatest Number`"}} cpp/user_input -.-> lab-96240{{"`C++ Program to Find Greatest Number`"}} cpp/data_types -.-> lab-96240{{"`C++ Program to Find Greatest Number`"}} cpp/operators -.-> lab-96240{{"`C++ Program to Find Greatest Number`"}} cpp/conditions -.-> lab-96240{{"`C++ Program to Find Greatest Number`"}} end

Create a new file

We will create a new file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

Include necessary header files

We need to include the iostream and cstdlib header files to use the cout, cin, and system functions.

#include <iostream>
#include <cstdlib>

Create the main() function

Add the following code to create the main() function:

int main() {
  // code will go here
  return 0;
}

Declare three float variables

We need to declare three float variables to hold the three numbers entered by the user.

float n1, n2, n3;

Prompt user for input

We will use the cout function to prompt the user to enter three numbers, and use the cin function to store those numbers in the variables we just declared.

std::cout << "Enter three numbers: ";
std::cin >> n1 >> n2 >> n3;

Determine the largest number

We will use a series of if statements to determine and output the largest number of the three.

if (n1 >= n2 && n1 >= n3) {
  std::cout << "Largest number: " << n1;
}
if (n2 >= n1 && n2 >= n3) {
  std::cout << "Largest number: " << n2;
}
if (n3 >= n1 && n3 >= n2) {
  std::cout << "Largest number: " << n3;
}

Run the program

Use the following command to compile and run the program:

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

Full code

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

#include <iostream>
#include <cstdlib>

int main() {
  float n1, n2, n3;
  std::cout << "Enter three numbers: ";
  std::cin >> n1 >> n2 >> n3;
  if (n1 >= n2 && n1 >= n3) {
    std::cout << "Largest number: " << n1;
  }
  if (n2 >= n1 && n2 >= n3) {
    std::cout << "Largest number: " << n2;
  }
  if (n3 >= n1 && n3 >= n2) {
    std::cout << "Largest number: " << n3;
  }
  return 0;
}

Summary

In this lab, we wrote a C++ program to find the greatest of three numbers. We learned how to use the if statement to compare values, and prompt the user for input using cout and cin. We also learned how to compile and run C++ programs in the terminal.

Other C++ Tutorials you may like