a. Critically explain why you would consider the concept of using methods in your
program again running a couple of if else statements.
b. Declare a method with three arguments all of type int. The method should return the
greatest of the three arguments, however if any two or all three are the same, you
method should return that value ?
c. Embed you method in a complete program that requests for three variable all of type
int and display the conditions explained in “b” above.
d. Critically explain the logic behind the code?
a. Functions reduces space and they than be recalled several times rather than writing the codes again and again.
b.
int greatest(int n1,int n2,int n3){
if(n1 >= n2 && n1 >= n3)
return n1;
if(n2 >= n1 && n2 >= n3)
return n2;
if(n3 >= n1 && n3 >= n2)
return n3;
}
c.
#include <iostream>
using namespace std;
int greatest(int n1,int n2,int n3){
if(n1 >= n2 && n1 >= n3)
return n1;
if(n2 >= n1 && n2 >= n3)
return n2;
if(n3 >= n1 && n3 >= n2)
return n3;
}
int main()
{
int a,b,c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
cout<<"The greatest number is "<<greatest(a,b,c);
return 0;
}
d. The code takes three numbers, compares then and return the greatest number among them.
Comments
Leave a comment