Write 2 C functions.
One returns the maximum of 5 numbers. The other function, returns the minimum of 5 numbers.
In your main function, prompt the user to input 5 numbers and call the 2 functions above.
I have used variable names which are self-explanatory.
Happy Coding!!!!
#include <stdio.h>
int max_finder(int arr[5])
{
int ans=-1000000007; //Almost like -infinity
//Looping over arr to find Maximum
for(int i=0;i<5;i++)
{
//If arr[i] is greater than current ans the update ans
if(arr[i]>ans)
{
ans=arr[i];
}
}
return ans; //Return ans as its max in given array
}
int min_finder(int arr[5])
{
int ans=1000000007; //Almost like -infinity
//Looping over arr to find Maximum
for(int i=0;i<5;i++)
{
//If arr[i] is smaller than current ans the update ans
if(arr[i]<ans)
{
ans=arr[i];
}
}
return ans; //Return ans as its min in given array
}
int main()
{
int values[5];
printf("Enter 5 number: ");
for(int i=0;i<5;i++)
{
//Taking Input from the user
scanf("%d", &values[i]);
}
int max=max_finder(values); //Calling max_finder() to get Maximum
int min=min_finder(values); //Calling min_finder() to get Minimum
printf("Maximum of 5 numbers is %d",max);
printf("\n");
printf("Minimum of 5 numbers is %d",min);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.