177k views
1 vote
Write a program that contains three methods:

Method max (int x, int y, int z) returns the maximum value of three integer values.
Method min (int X, int y, int z) returns the minimum value of three integer values.
Method average (int x, int y, int z) returns the average of three integer values.

1 Answer

6 votes

Answer:

#include <stdio.h>//defining header file

int max (int x, int y, int z) //defining a method max that hold three parameters

{

if(x>=y && x>=z)//defining if block that checks x is greater then y and x is greater then z

{

return x;//return the value x

}

else if(y>z)//defining else if block it check y is greater then z

{

return y;//return the value y

}

else//else block

{

return z;//return the value z

}

}

int min (int x, int y, int z) //defining a method max that holds three parameters

{

if(x<=y && x<=z) //defining if block that check x value is less then equal to y and less then equal to z

{

return x;//return the value of x

}

if(y<=x && y<=z) //defining if block that check y value is less then equal to x and less then equal to z

{

return y;//return the value of y

}

if(z<=x && z<=x)//defining if block that check z value is less then equal to x

{

return z;//return the value of z

}

return x;//return the value of z

}

int average (int x, int y, int z) //defining average method that take three parameters

{

int avg= (x+y+z)/3;//defining integer variable avg that calculate the average

return avg;//return avg value

}

int main()//defining main method

{

int x,y,z;//defining integer variable

printf("Enter first value: ");//print message

scanf("%d",&x);//input value from the user end

printf("Enter Second value: ");//print message

scanf("%d",&y);//input value from the user end

printf("Enter third value: ");//print message

scanf("%d",&z);//input value from the user end

printf("The maximum value is: %d\\", max(x,y,z));//calling the method max

printf("The minimum value is: %d\\", min(x,y,z));//calling the method min

printf("The average value is: %d\\", average(x,y,z));//calling the method average

return 0;

}

Output:

Enter first value: 45

Enter Second value: 35

Enter third value: 10

The maximum value is: 45

The minimum value is: 10

The average value is: 30

Step-by-step explanation:

In the above-given code, three methods "max, min, and average" is declared that holds three integer variable "x,y, and z" as a parameter and all the method work as their respective name.

  • In the max method, it uses a conditional statement to find the highest number among them.
  • In the min method, it also uses the conditional statement to find the minimum value from them.
  • In the average method, it defined an integer variable "avg" that holds the average value of the given parameter variable.
  • In the main method, three variable is defined that inputs the value from the user end and passes to the method and print its value.
User Victor Le
by
5.2k points