Question #269808

  1. Using the do…while() loop, continuously scan for random integers, but add up only all the positive integers and store the total in one variable.
  2. The loop shall only be terminated when the inputted integer is zero. Afterwards, print out the total of all inputted positive integers.

Expert's answer

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);

        int input = 0;
        int sum = 0;

        do {
            System.out.print("Enter number: ");
            input = scanner.nextInt();
            if (input > 100) {
                System.out.println("his number is already more than 100");
                break;
            }
            if (sum + input <= 100)
                sum += input;

        } while (sum < 100);

        System.out.println("Sum of Numbers : " + sum);
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS