0

Write a program in C++ to input three numbers. Find and display the maximum

30th Jul 2022, 5:05 AM
HIV
HIV - avatar
7 ответов
+ 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
30th Jul 2022, 5:46 AM
Ipang
+ 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!
30th Jul 2022, 6:10 AM
Erarnitox
Erarnitox - avatar
+ 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
30th Jul 2022, 6:06 AM
Tharinda Nimnajith
Tharinda Nimnajith - avatar
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; }
31st Jul 2022, 3:57 PM
Dhruv R Jain
Dhruv R Jain - avatar
0
OoOoo
22nd Nov 2024, 5:25 AM
Kishan Sarkar
Kishan Sarkar - avatar