Create a program that will enter at least five numbers, compute the average, and print the said numbers in reverse order.
Sample output:
Enter the number of elements: 5
enter the value of item number 1: 80
enter the value of item number 2: 82
enter the value of item number 3: 84
enter the value of item number 4: 82
enter the value of item number 5: 85
The average of 5 element is 82.60
Reverse order of 5 elements: 85, 82, 84, 82, 80
import java.util.Scanner;
public class App {
/** Main Method */
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in); // Create a Scanner
System.out.print("Enter the number of elements: ");
int size = keyBoard.nextInt();
double numbers[] = new double[size];
double sum = 0;
for (int i = 0; i < size; i++) {
System.out.print("enter the value of item number " + (i + 1) + ": ");
numbers[i] = keyBoard.nextDouble();
sum += numbers[i];
}
double average = sum / (double) size;
System.out.printf("The average of " + size + " element is %.2f", average);
System.out.print("\nReverse order of " + size + " elements: ");
for (int i = size - 1; i > 0; i--) {
System.out.print(numbers[i] + ",");
}
System.out.print(numbers[0]);
keyBoard.close();
}
}
Comments
Leave a comment