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