Hi there new to the forum and new to c++ and programming (been doing it for 2 days)
Anyway I am trying to self learn using a book and an online course and i'm stuck with one of the practice problems.
the problem is: Write a program that takes in 3 numbers and outputs the biggest number and the smallest number using if else.
so far i only have it outputting the biggest of the 3 but can't figure out how to get the smallest as well.
I know this is probably really easy but my brain just feels fried at the moment and i cant think straight.
Any tips or advice pointing me in the right direction would be greatly appreciated.
Thank you nisor
i thought about this but was trying to think of a way with less code involved
but this has helped me a lot, i should probably just do what i think and not try to ake things complicated for myself.
thanks
int max(int x, int y, int z) {
if(x>y && x>z) return x;
elseif(y>x && y>z) return y;
elsereturn z;
}
int main() {
int num1,num2,num3;
cout << "Please enter 3 numbers leaving a space between each: ";
cin >> num1 >> num2 >> num3;
int maximum = max(num1,num2,num3);
cout << maximum << endl
}
We supose that input numbers are integers, else in function max should be double type of variables.
You will understand better with function.
Best Regards.
#include <iostream>
usingnamespace std;
int main()
{
int num1 = 0;
int num2 = 0;
int num3 = 0;
cout << "Please enter 3 numbers leaving a space between each: ";
cin >> num1 >> num2 >> num3;
// greater number
if(num1>num2)
{
if(num1>num3)
{
cout<<num1<<" is the greatest number"<<endl;
}
else
{
cout<<num3<<" is the greatest number"<<endl;
}
}
elseif(num2>num3)
{
cout<<num2<<" is the greatest number"<<endl;
}
else
{
cout<<num3<<" is the greatest number"<<endl;
}
// smallest number
if(num1<num2)
{
if(num1<num3)
{
cout<<num1<<" is the smallest number"<<endl;
}
else
{
cout<<num3<<" is the smallest number"<<endl;
}
}
elseif(num2<num3)
{
cout<<num2<<" is the smallest number"<<endl;
}
else
{
cout<<num3<<" is the smallest number"<<endl;
}
return 0;
}