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:
Instructions
Input
Multiple lines containing an integer on each.
2
3
4
-1
-5
1
0
Output
A line containing an integer.
10
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
Comments
Leave a comment