Write a program that reads a collection of positive and nega
Write a program that reads a collection of positive and negative numbers and multiplies only the positive integers. Loop exit should occur when three consecutive negative values are read.
Solution
package org.students;
import java.util.Scanner;
public class MultiplyPositiveNos {
public static void main(String[] args) {
//Declaring variables
int number,result=1,count=0;
//Scanner class Object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
/* This loop continues to executes until the user
* enters consecutively 3 times negative number
*/
while(true)
{
//Getting the Integer entered by the user
System.out.print(\"\ Enter a number (Positive /Negative) :\");
number=sc.nextInt();
/* Checking whether the entered number is positive or not
* if yes,Then it performs multiplication.
* and set count variable to zero
* If not,Incrementing the count variable by 1
*/
if(number>0)
{
//Performing multiplication
result*=number;
//Setting count variable to zero
count=0;
}
else
count++;
/* Checking if the count value is 3
* If yes,Then this while loop will get break
* and control will comes out of the loop
*/
if(count==3)
break;
}
//Displaying the final Result after multiplying alla positive numbers
System.out.println(\"The Multiplicatin of Positive Integers is :\"+result);
}
}
____________________________________
Output:
Enter a number (Positive /Negative) :5
Enter a number (Positive /Negative) :-2
Enter a number (Positive /Negative) :3
Enter a number (Positive /Negative) :-1
Enter a number (Positive /Negative) :-2
Enter a number (Positive /Negative) :2
Enter a number (Positive /Negative) :1
Enter a number (Positive /Negative) :-1
Enter a number (Positive /Negative) :-2
Enter a number (Positive /Negative) :-3
The Multiplicatin of Positive Integers is :30
_____________Thank You

