writing a program
Sep 16, 2012 at 5:57am UTC
How do i write a program to print the larger of any 2#.
write a program to print the largest of any distinct 3#
Sep 16, 2012 at 6:12am UTC
Assuming that '#' is to be replaced by 'numbers'
1) you compare them using a '<' or '>' operator. You can use an if or the ternary operator.
2) use the program you made before and call it twice.
Sep 16, 2012 at 6:40am UTC
1) how do i compare them ]
2) and how do i call it twice
Sep 16, 2012 at 8:24am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
using namespace std;
int main()
{
int nOne;
int nTwo;
cout << "input first number" <<endl;
cin >> nOne;
cout << "input second number" <<endl;
cin >> nTwo;
if (nOne>nTwo)
{
cout << nOne;
}
else if (nOne<nTwo)
{
cout << nTwo;
}
else
{
cout << "they're equal" ;
}
return 0;
}
here's the code for your first program
Last edited on Sep 16, 2012 at 8:26am UTC
Sep 16, 2012 at 8:38am UTC
1 2
#include <iostream>
int main(){int a[2];std::cin>>a[0];std::cin>>a[1];a[0]>a[1]?std::cout<<a[0]:a[1]>a[0]?std::cout<<a[1]:std::cout<<"even" ;}
Here you go op.
Last edited on Sep 16, 2012 at 9:01am UTC
Sep 16, 2012 at 8:44am UTC
For your second program, you can simply add another number then compare the three of them one by one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>
using namespace std;
int main()
{
int nOne;
int nTwo;
int nThree;
cout << "input first number" <<endl;
cin >> nOne;
cout << "input second number" <<endl;
cin >> nTwo;
cout << "input third number" <<endl;
cin >> nThree;
if (nOne>nTwo>nThree)
{
cout << nOne;
}
else if (nOne<nTwo<nThree)
{
cout << nThree;
}
else if (nTwo>nThree>nOne)
{
cout << nThree;
}
else
{
cout << "they're equal" ;
}
return 0;
}
you're welcome op.
Topic archived. No new replies allowed.