Monday, 14 March 2016

Hacker Rank Solution Program In C++ For "Functions "

Method 1:- Here i am written return after each output for understanding purpose thats is not compulsory to write return after each and every statement you can see method 2 full solution with one return ;

#include <iostream>
#include <cstdio>
using namespace std;

int max_of_four(int a, int b, int c, int d)
    {

        if(a>b&&a>c&&a>d)
       {
        cout<<a;
            return(a);
        }
        else if(b>a&&b>c&&b>d)
       {
        cout<<b;
            return(b);
        }
        else if(c>b&&c>a&&c>d)
       {
        cout<<c;
            return(c);
        }  
        else
       {
        cout<<d;
            return(d);
       }
    }


int main()
{
    int a, b, c, d,max;
    cin>>a>>b>>c>>d;
    max=max_of_four(a,b,c,d);
}

Method 2:- you no need to write return after each and every output just write return after all the case's and output .


#include <iostream>
#include <cstdio>
using namespace std;

int max_of_four(int a, int b, int c, int d)
    {

        if(a>b&&a>c&&a>d)
       {
        cout<<a;
        }
        else if(b>a&&b>c&&b>d)
       {
        cout<<b;
        }
        else if(c>b&&c>a&&c>d)
       {
        cout<<c;
        }
        else
       {
        cout<<d;
       }
       return 0;
    }


int main()
{
    int a, b, c, d,max;
    cin>>a>>b>>c>>d;
    max=max_of_four(a,b,c,d);
}


output:-

programmingwithbasics

3 comments:
Write comments
  1. why did you use return(a), return(b), return(c)? wont it simply work if you just write cout statement and skip the return part? why do you need to write return for each condition ?

    ReplyDelete
  2. why did you use return(a), return(b), return(c)? wont it simply work if you just write cout statement and skip the return part? why do you need to write return for each condition ?

    ReplyDelete
    Replies
    1. no need to write function after each and every statement this is just for understanding purpose their are two method you can choose any one

      Delete

Recommended Posts × +