Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 8 months ago

# Write a C++ program that takes in three positive integers and then computes and prints the largest number of the three?

Write a program that reads three positive integers (> 0) from the command line (one at a time), then computes and prints the largest entered number.

Use if-else statements for three integer comparison and use loops for a user validation for this exercise

Note: Program must be able to handle multiple values that are equal to each other

Example:

Enter an integer

:

5

Enter an integer

:

23

Enter an integer

:

7

The largest number is: 23

Example 2:

Enter an integer

:

3

Enter an integer

:

6

Enter an integer

:

6

The largest number is: 6

My program so far is:

int num1, num2, num3;

int main() {

cout << "Enter an integer\n";

cin >> num1;

cout << "Enter an integer\n";

cin >> num2;

cout << "Enter an integer\n";

cin >> num3;

if (num1 > num2 && num1 > num3) {

cout << "The largest number is: " << num1;

}

else if (num2 > num1 && num2 > num3) {

cout << "The largest number is: " << num2;

}

else {

cout << "The largest number is: " << num3;

}

return 0;

}

I was told that my program has to handle a case if two or three values are equal to each other so idk what to add to this small program.

Update:

Whoever can help me quickest gets 5 stars! I dont think this problem is too hard (but it kinda is for me lol)

Relevance

Just change > to >=

int main() {

int num1, num2, num3;

cout << "Enter an integer\n";

cin >> num1;

cout << "Enter an integer\n";

cin >> num2;

cout << "Enter an integer\n";

cin >> num3;

if (num1 >= num2 && num1 >= num3) {

cout << "The largest number is: " << num1;

}

else if (num2 >= num1 && num2 >= num3) {

cout << "The largest number is: " << num2;

}

else {

cout << "The largest number is: " << num3;

}

return 0;

}

don't forget the loops for testing the integers

int num1 = 0, num2 = 0, num3 = 0;

while(num1<1){

cout << "Enter an integer\n";

cin >> num1;

}

Also if you want you can use < instead of >

if (num1 < num2 || num1 < num3)

if (num2 < num1 || num2 < num3)

cout << "The largest number is: " << num3;

else cout << "The largest number is: " << num2;

else cout << "The largest number is: " << num1;

also you can use a temporary variable and so have display separate from the calculation

int largest = num2;

if (num1 < num2 || num1 < num3) {

if(num2 < num1 || num2 < num3) {

largest = num3;

}

}

else {

largest = num1;

}

cout << "The largest number is: " << largest;