maximum program

Instructions:Write a program that reads two numbers and store them into variables double x,y; Then the code finds double M the maximum of the two numbers and prints its value.

So far i got
#include <iostream>
using namespace std;

int main() {

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



cout << "The maximum of the two numbers is" << M <<endl;


Am i suppose to put #include <math.h> to find maximum???
how do i do "Then the code finds double M the maximum of the two numbers and prints its value."


you could probably just add this code after the cin >> y;

if (x > y)
m = y
else if (y > x)
m = x;
else
m = x; //since both values must be equal to each other

cout << "The maximum of the two numbers is" << M <<endl;
giblit's approach is better for boy3005.
Last edited on
First of all , what does maximum of two numbers mean?
That means the larger number.

Also Josue if he doesn't know how to do this program he probably doesn't know the ternary operator.

Basically its saying if a > b then a is the maximum otherwise b is the maximum.

Because think about it one 1 is not greater than 2 then 2 must be the max and if 1 is not greater than 1 then 1 must be the max.

*edit
an easy solution would be this

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    double a , b , c;
    std::cin >> a >> b;
    c = b;
    if( a > b )
        c = a;
    std::cout << c;
}
Last edited on
so is this correct?


#include <iostream>
using namespace std;

int main() {

double x , y, M;
cout << " Enter the value of x."<<endl;
cin >> x;
cout << " Enter the value of y."<<endl;
cin >> y;
if ( x > y )
M = x;
else
M = y;

cout << "The maximum of the two numbers is " << M << "."<<endl;


return 0;
}
and how would i do the code when it requires me to find the minimum of three variables
the above code is correct for the task that you originally mentioned. You did not mention " find the minimum of three variables " so I can only assume what you provide. The second task will required similar code from above except the opposite and with three variables to check. Many ways to solve that problem. To me, it sounds like maybe you just need to take many integers as input and then just sort them from smallest to largest. This would require an array. But again, I am not sure what the requirements are of the assignment. Are you in a college class?
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.

I only learned the basics up to if or else functions and while loops.
Not sure what an array is. I think we didnt learn that yet.
i dont think im allowed to use them in this assignment since we didnt learn it yet..
Then use some logic:

Say you have three numbers a, b, and c.

1
2
3
4
5
6
7
8
9
if a > b
    if a > c
        display a
    else
        display c
if b > c
    display b
else
    display c
Are you a college student? I am asking because I am a CS student at Uni. I wish my assignments were this easy. But then again, at one point they may have been and I just forgot. Happy coding
Yeah I am sorry about that.
You did forget, erock 88.

I still remember the day printing to the console was the most obscure thing in the world.

Three years later my programs were recognizing faces in pictures.
Thank all of you for helping me. I really appreciate it.
Use some logic:
in This logic u can get max and minimum no. both:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int n,x,l,s;
for (n=1;n<=3;n++)
{
cout<<"\nEnter any no ";
cin>>x;
// assuming that the first no is largest no & smallest no
if (n==1)
{ l=x;
s=x;}
 
if (x>l)
l=x;
if (x<s)
s=x;
}
cout<<"\nThe largest no is "<<l;
cout<<"\nThe smallest no is "<<s;


I would personally recommend because is very cool and allows you to do simple way of programming
cbtsam
Last edited on
Topic archived. No new replies allowed.