Skip to main content

Greatest of Four Number using Functions in C Programming

                        Hello everyone, this blog is to find the greatest of four numbers and we i am using the functions concept for this program. First we need a function named max_of_four(), and that accepts four number as parameter named a, b, c, d.
                            Our function is a combination of two greatest of two numbers, and it checks first two number and return the greatest then returns the greatest of next two numbers. This function is called form the main function and there  we get all inputs from the user.

Code:

# include <stdio.h>

int max_of_four(int a,int b,int c,int d)
{
    int max,max1;

    //max of a and b

    if(a>b)
    {
        max=a;
    }
    else {
    max=b;
    }

    //max of c and d

    if (c>d)
    {
        max1=c;
    }
    else {
    max1=d;
    }

    //greatest of four

    if (max > max1)
    
    {
        return max;
    }
    else {
    return max1;
    }
}

int main()
{
    
    int a,b,c,d,r;

    //getting input

    printf("Enter four numbers:");
    scanf("%d %d %d %d",&a,&b,&c,&d);

    //function call
    r=max_of_four(a,b,c,d);
    
    printf("Greatest of four number is %d",r);
}

Output:










Comments