159k views
3 votes
Write a Java program that does the following: Prompts the user to input five decimal numbers Prints the five decimal numbers Converts each decimal number to the nearest integer Adds the five integers Prints the sum and average of the five integers

1 Answer

0 votes

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner myObj = new Scanner(System.in);

float arr[] = {0,1,2,3,4};

String txt[] = {"first", "second", "third", "fourth", "fifth"};

for (int i=0;i<arr.length; i++){

System.out.println("Enter the " + txt[i] + " number.");

float num1 = myObj.nextFloat();

arr[i] = num1;

}

for (int w = 0; w< arr.length; w++){

System.out.println("The " + txt[w] + " number is " + arr[w]);

arr[w] = Math.round(arr[w]);

}

System.out.println(" ");

for (int w = 0; w < arr.length; w++){

System.out.println("The " + txt[w] + " number is " + arr[w]);

}

System.out.println(" ");

float total = 0;

for (float w : arr){

total += w;

}

System.out.println("The sum of the numbers is " + total + " and the average is " + (total / 5));

}

}

I hope this helps!

User Joe Strout
by
6.0k points