Write a program in Java to input a series of numbers one by one to print the count and average of those numbers which have 3 as their last digit. The process of inputting numbers should stop if the number inputted by the user is a negative number.
Answers
Answer:
Scanner scanner = new Scanner(System.in);
int sum = 0;
System.out.println("Input a number: ");
sum = sum + Integer.valueOf(scanner.nextLine());
System.out.println("Input a number: ");
sum = sum + Integer.valueOf(scanner.nextLine());
System.out.println("Input a number: ");
sum = sum + Integer.valueOf(scanner.nextLine());
System.out.println("Input a number: ");
sum = sum + Integer.valueOf(scanner.nextLine());
System.out.println("Input a number: ");
sum = sum + Integer.valueOf(scanner.nextLine());
System.out.println("The sum of the
Answer:
import java.util.Scanner;
public class KboatNumbers
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the numbers: ");
int count = ;
double sum =;
while (true) {
int n = in.nextInt();
if (n < 0) {
break;
}
int ld = n % ;
if (ld ) {
count;
sum n;
}
}
double avg sum / count;
System.out.println("Count count);
System.out.println("Average avg);
}
}
Output:
Enter the number:
Explanation:
Java input and output is a requisite concept when working on java programming. It incorporates elements as input, output, and stream. The input is the data that we give to the program. The output is the data we experience from the program in the form of results. In Java, there are four different ways of reading input from the user in the be in charge of line environment(console).
Hence, the correct answer is:
import java .util. Scanner;
public class KboatNumbers
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the numbers: ");
int count = ;
double sum =;
while (true) {
int n = in.nextInt();
if (n < 0) {
break;
}
int ld = n % ;
if (ld ) {
count;
sum n;
}
}
double avg sum / count;
System.out.println("Count count);
System.out.println("Average avg);
}
}
Output:
Enter the number:
#SPJ3