Answer to Question #268034 in Java | JSP | JSF for harryy

Question #268034

4. Gathering Positivity

by CodeChum Admin

I always want to look at the positive side of things, so I decide to seriously look at positive numbers, too!


Will you code along with me?


Instructions:

  1. Using the do…while() loop, continuously scan for 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.

Instructions

  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.

Input

Multiple lines containing an integer on each.

2
3
4
-1
-5
1
0

Output

A line containing an integer.

10




1
Expert's answer
2021-11-18T15:18:18-0500


SOLUTION TO THE ABOVE QUESTION


SOLUTION CODE


package com.company;
import java.util.ArrayList;
import java.util.*;
class Main{
    public static void main(String arg[])
    {
        //propmt the user to enter a positive odd integer
        Scanner sc = new Scanner(System.in);
        //declare a variable to store the sum of inputted integers and initilaize it to 0
        int input_integer_sum = 0;
        System.out.println("\nEnter integers, to stop entering enter integer 0\n");
        int condition = 1;
        do {
            System.out.print("Enter an integer: ");
            int my_integer = sc.nextInt();
            if(my_integer==0)
            {
                condition = 0;
            }
            else
            {
                //sum the positive integers only
                if(my_integer>0)
                {
                    input_integer_sum = input_integer_sum + my_integer;
                }

            }

        }while (condition==1);
        //print the output
        System.out.println("\nOUTPUT:\n");
        System.out.println("The sum of all the inputted positive integers = "+input_integer_sum);

    }
}


SAMPLE PROGRAM OUTPUT






Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS