Function Maximum and Minimum
Write a program that asks a user to
input 3 separate integer, the program reads it and determines the maximum and
minimum integer.
Understanding Functions
Modules in C are called functions. C
programs are typically written by combining new functions you write with prepackaged
functions available in the C standard library.
The C standard library provides a rich
collection of functions for performing common mathematical calculations,
string manipulations, character manipulations, input/output,
and many other useful operations. This makes your
job easier, because these functions
provide many of the capabilities you need.
Functions
are invoked by a function call,
which specifies the function name and provides information (as arguments) that
the function needs to perform its designated task. for more on functions click here
/*Finding the maximum/minimum of three
integers */
#include<stdio.h>
int maximum(int x,int y, int z);
int minimum(int x, int y, int z);
/*function main begins program execution */
/*function main begins program execution */
int main(void)
{
/*Initialization phase */
int num1; // first integer entered by the user
int num2; // second integer entered by the user
int num3; // third integer entered by the user
printf("Enter Three digits integers to know the
maximum/minimum:"); /*Prompting for input */
scanf("%d%d%d", &num1,&num2,&num3); /*reading grade from user */
// number1, number2 and number3 are arguments
// to the maximum and minimum function call
printf("\nMAXIMUM IS:%d\n", maximum(num1,num2,num3)); /*displaying result */
printf("\nMINIMUM IS:%d\n", minimum(num1,num2,num3)); /*displaying result */
return 0; /* indicates that program ended successfully */
}
// Function maximum definition
// x, y and z are parameters
int maximum(int x, int y, int z)
{
int max=x; /*assume x is the largest integer */
if(y>max){
max=y;
}
if(z=max){
max=z;
}
return max;
}
// Function minimum definition
// x, y and z are parameters
int minimum(int x, int y, int z)
{
int min=x;/*assume x is the smallest */
if(y<min){
min=y;
}
if(z<min){
min=z;
}
}
Explanation of the program
Explanation of the program
Our program above uses
a programmer-defined function maximum and minimum to determine and return the
largest and smallest of three integers .
The integers are
input with scanf. (scanf("%d%d%d", &num1,&num2,&num3);)
Next, they’re
passed to maximum and minimum, which determines the largest and smallest
integer. printf("\nMAXIMUM IS:%d\n", maximum(num1,num2,num3));
printf("\nMINIMUM IS:%d\n", minimum(num1,num2,num3));
This value is
returned to main by the return statement in maximum and minimum. The value
returned is then printed in the printf statement .
Comments
Post a Comment
Please feel free to comments if you have any ideas, questions and subjection concerning our post and programs