need help with a java program. this program is in java language the problem is:
ID: 3831427 • Letter: N
Question
need help with a java program. this program is in java language the problem is:
write a program that prompts the user for positive integers, only stopping when a negative integer or zero is given. the program should then print out how many of the positvie integers were odd.
this is an example of what the program should output:
Enter a positive integer (0 or negative to stop) : 9
Enter a positive integer (0 or negative to stop) : 4
Enter a positive integer (0 or negative to stop) : 7
Enter a positive integer (0 or negative to stop) : -3
You entered 2 odd integers.
Explanation / Answer
import java.util.Scanner;
public class countAdd {
public static void main(String[] args)
{
int count=0;
while(true)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a positive integer (0 or negative to stop) : ");
int n = scan.nextInt();
if(n<=0)
{
break;
}
if(n%2==1)
{
count++;
}
}
System.out.println("You entered "+count+" odd integers");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.