minimum of three numbers

Instructions:Write a program that reads three numbers and stores them into variables double x,y, z; Then the code finds double m the minimum of the three numbers and prints its value


#include <iostream>
using namespace std;

int main() {

double x , y, z, m;
cout << " Enter the value of x."<<endl;
cin >> x;
cout << " Enter the value of y."<<endl;
cin >> y;
cout << " Enter the value of z."<<endl;
cin >> z;

if ( x > y )
m = y;
else
m = x;
if ( x > z )
m = z;
else
m = x;
if ( y > z )
m = z;
else
m = y;
cout << "The minimum of the three numbers is " << m << "."<<endl;


return 0;
}

Is this right?
I typed in the numbers 1 for x , 2 for y , 3 for z and i got 2 as the minimum.
Isnt the minimum 1? Do i do something wrong?
The quick solution would be
 
cout << min(x,min(y,z)) << endl;


if you have to use "if"

then you should compare it the current minimum ( this is where your code is wrong )

1
2
3
4
int m = 0;
if( x < y ) m = x; else m = y;
if( z < m ) m = z;
cout << m << endl;

Thank you so much. I appreciate it.
Topic archived. No new replies allowed.