0
Write a program in C++ to input three numbers. Find and display the maximum
7 Respuestas
+ 2
Always use relevant and appropriate tags
Just use nested std::max()
std::cout << std::max( number1, std::max( number2, number3 ) );
https://code.sololearn.com/W3uiji9X28C1/?ref=app
+ 2
there are many ways of doing it. I think one of the simplest:
int main(){
int in{};
int max{ 0 };
for(int i=0; i < 3; ++i){
std::cin >> in;
max = max > in? max : in;
}
std::cout
<< "Maximum: "
<< max
<< std::endl;
return 0;
}
good thing about this is that using this method with the for loop it is easily scaleable.
Note that this code only works for positive integers, as I have initialized max with 0. You could initialize max with the smallest integer possible or use a vector and add all integers to the vector to later sort it.
There are endless ways of doing it, each with different pros and cons.
I think given my code, you should be able to figure something out that works for you :) Good Luck!
+ 1
#include <iostream>
int main() {
int n1, n2, n3, max;
std::cin >> n1 >> n2 >> n3;
if (n1 > n2 && n1 > n3)
max = n1;
else if (n2 > n1 && n2 > n3)
max = n2;
else
max = n3;
std::cout << max;
return 0;
}
This is the simplest way to find maximum of 3 integers. But it is not a very scalable solution as it is limited to only 3 numbers as per the requirement of your query.
https://code.sololearn.com/c5SRgZ78cjDc/?ref=app
0
#include<iostream.h>
int main(){
int a,b,c;
cin>>a>>b>>c;
if(a>b){
if(a>c)
max=a;}
else
{
if(b>c)
max=b;
else
max=c;
}
cout<<max;
}
0
OoOoo