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.
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
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; }
Rewrite the following do while program segment using for:
x = 10; y = 20; do { x++; y++; } while (x<=20); System.out.println(x * y );
Give the output of following code and mention how many times the loop will execute?
int i; for( i=5; i>=1; i--) { if(i%2 == 1) continue; System.out.print(i+" "); }
To execute a loop 5 times, which of the following is correct?