KnowledgeBoat Logo

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

BlueJ output of 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.

Answered By

3 Likes


Related Questions