Functions in C -Hacker Rank Solution.

 

Objective

In this challenge, you will learn simple usage of functions in C. Functions is a bunch of statements grouped together. A function is provided with zero or more arguments, and it executes the statements on it. Based on the return type, it either returns nothing (void) or something.

A sample syntax for a function is

	return_type function_name(arg_type_1 arg_1, arg_type_2 arg_2, ...) {
    	...
        ...
        ...
        [if return_type is non void]
        	return something of type `return_type`;
    }

Task

Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them.

Note

There is not built in max function in C. Code that will be reused is often put in a separate function, e.g. int max(x, y) that returns the greater of the two values.


Input Format

Input will contain four integers -  , one on each line.

Output Format

Print the greatest of the four integers.
Note: I/O will be automatically handled.

Sample Input

3
4
6
5

Sample Output

6

Solution:-

#include<stdio.h>

int max_of_four(int a,int b,int c,int d)
{
    int greater;
    if(a>b && a>c && a>d)
    greater=a;
    if(b>a && b>c && b>d)
    greater=b;
    if(c>a && c>b && c>d)
    greater=c;
    if(d>a && d>b && d>c)
    greater=d;
    
    return greater;
}
int main()
{
    int a,b,c,d;
    scanf("%d %d %d %d",&a,&b,&c,&d);
    int result=max_of_four(a,b,c,d);
    printf("%d",result);
    return 0;
}




Popular posts from this blog

When a method in a subclass overrides a method in superclass, it is still possible to call the overridden method using super keyword - Hacker Rank Solution.

Java's System.out.printf function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using printf. To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution.