Monday, 14 March 2016

Hacker Rank solution for Functions

Problem:- Hacker Rank Solution Program In C++ For "Functions " or Hacker Rank Solution Program In C++ For Functions or Functions solution hacker rank or Hacker rank solution for c++ domain or Hacker rank solution for Functions subdomain or Functions solution in c++ of hacker rank or introduction solutions hacker rank.

Check This:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Logic:- So function solution in c++ is very simple and easy t understand, first we have to understand what is a function? After that, we can use the function. Now come to the point hare we have to pass the value in a function, we can do this by pass by value and pass by reference(basically we passed the address of a variable), but here we are passing the value in the function. After the passing the value putting some condition in function and returning the output form function. for better understand let's see program step by step.

What is a function?

A function is a group of statements that together perform a task. Most important every C/C++ program has at least one function same Java program has one class at least.Main() is the function in C/C++.

Syntax of Function

return_type function_name( parameter list ) 
{
statement.......1 //a body of the function
statement.......2
statement.......3
.
.
.
statement.......n
}

Explanation:- First function declaration(optional if you are define function above main() function). After that taking a user input in this case 4 value is taking and pass the values in function then the function will return the greatest value for four value by some statement and condition. 

Passing the value in function

 max=max_of_four(a,b,c,d);

Optional Hint:- No need to use a huge number of the header file if you want to use only one header file you can use the Master Header file. Master header in C++ is #include<bits/stdc++.h> after adding this header file no need to add any other header file.

Also Check- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

Copy the full solution or colored code and paste into the hacker rank editor and click to Run Code if the code runs successfully then click to Submit Code. Before pasting the code into the editor make sure you have chosen C++ or if C++ not worked fine then change it into C++14 an editor in the top right drop-down option.

Submit your solution here:- Click here

Tip:- I recommended read this full article/post, this will help you to build your own logic. 

If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about new post this Article.

Solution:- 

Method 1:- Here I have written return statement after each output for the understanding purpose, that 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:- No need to write return after each and every output just write return after all the cases 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:-

1. Output in Hacker Rank Editor


Hacker Rank solution for Functions

2. Output in Local Editor

Hacker Rank solution for Functions


You May Like This:-

1. Hacker Rank Solution for 30 Days of Code

2. Hacker Rank solution for Attribute Parser

3. Java Program For Find The Gross Salary Of An Employee

4. C++ Program For School Management System ( SMS Project ) With Source Code

5. Hacker Rank Solution For Mini-Max Sum

6. Hacker Rank Solution For Birthday Cake Candles

7. Geeksforgeeks Solution For " Sum of Middle Elements of two sorted arrays "

8. Java Program For Converting Temperature Celsius Into Fahrenheit

9. Geeksforgeeks Solution For " Two Repeated Elements "

10. Hacker Rank Solution For Virtual Functions

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 × +