Computer Applications
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.
Java
Java Iterative Stmts
1 Like
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 = 0;
double sum = 0;
while (true) {
int n = in.nextInt();
if (n < 0) {
break;
}
int ld = n % 10;
if (ld == 3) {
count++;
sum += n;
}
}
double avg = sum / count;
System.out.println("Count = " + count);
System.out.println("Average = " + avg);
}
}
Output

Answered By
2 Likes
Related Questions
How many times will the following loop execute? Write the output of the code:
int a = 5; while (a > 0) { System.out.println(a-- + 2); if (a % 3 == 0) break; }
Write a program to input a number and find whether the number is an emirp number or not. A number is said to be emirp if the original number and the reversed number both are prime numbers.
For example, 17 is an emirp number as 17 and its reverse 71 are both prime numbers.
Convert the following for loop segment to an exit-controlled loop.
for (int x = 1, y = 2; x < 11; x += 2, y += 2) { System.out.println(x + "\t" + y); }
Give the output of the following program segment. How many times is the loop executed?
for(x=10; x>20;x++) System.out.println(x); System.out.println(x*2);