(C language) <stdio.h> decisions (else if or switch)
Write a program to calculate the average of the 2 highest numbers entered. Ask the user to enter 3 integers, determine which 2 integers are the 2 highest, and then calculate and display the average of the 2 highest numbers. Format the answer to 2 decimal places.
The numbers can be entered in any sequence - lowest to highest, highest to lowest, or completely random. A decision block will be necessary to determine the 2 highest numbers. Since calculating an average involves division, type casting will be necessary.
Hints:
Example Run #1:
(bold type is what is entered by the user)
Enter the 1st integer: 2
Enter the 2nd integer: 3
Enter the 3rd integer: 4
The average of the 2 highest numbers
(3 and 4) is x.xx.
The example runs show EXACTLY how your program input and output will look.
#include <stdio.h> int main() { int n1, n2, n3; printf("Enter the 1st integer: "); scanf("%d", &n1); printf("Enter the 2nd integer: "); scanf("%d", &n2); printf("Enter the 3rd integer: "); scanf("%d", &n3); if (n1 >= n3 && n2 >= n3) { printf("(%d and %d) is %.2f.\n", n1, n2, (n1+n2)/2.0); } else if (n2 >= n1 && n3 >= n1) { printf("(%d and %d) is %.2f.\n", n2, n3, (n2+n3)/2.0); } else { printf("(%d and %d) is %.2f.\n", n1, n3, (n1+n3)/2.0); } return 0; }
Get Answers For Free
Most questions answered within 1 hours.