Question

Write a program that prompts the user for positive integers, only stopping when a negative integer...

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 positive integers were odd.

Example:
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.

java languageee!!!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Main2.java
import java.util.Scanner;
public class Main2{
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    int n, count = 0;
    while (true) {
      System.out.print("Enter a positive integer (0 or negative to stop): ");
      n = scanner.nextInt();
      if(n<=0){
        break;
      }
      if(n%2 == 1){
        count += 1;
      }
    }
    System.out.println("You entered "+count+" odd integers.");
  }
}


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.

Add a comment
Know the answer?
Add Answer to:
Write a program that prompts the user for positive integers, only stopping when a negative integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Write a C++ program that repeatedly collects positive integers from the user, stopping when the user...

    Write a C++ program that repeatedly collects positive integers from the user, stopping when the user enters a negative number or zero. After that, output the largest positive number entered. A sample run should appear on the screen like the text below. Enter a number: 3 Enter a number: 10 Enter a number: 2 Enter a number: -213 Output: The largest positive number you entered was 10.

  • A java program Write a program that prompts for and reads in a positive    integer...

    A java program Write a program that prompts for and reads in a positive    integer into a variable n. Your program should then sum    the first n ODD integers and display the sum. For    example, if 3 is entered for n, your program should display    the the sum 1 + 3 + 5. If 5 is entered, your program should    display the sum 1 + 3 + 5 + 7 + 9. What is the...

  • In Java Write a program that will ask the user for integers and print if the...

    In Java Write a program that will ask the user for integers and print if the integer is positive, negative or zero. The program should continue asking for integer until the user enters a negative value.

  • Java: Write a program that prompts the user to enter integers in the range 1 to...

    Java: Write a program that prompts the user to enter integers in the range 1 to 50 and counts the occurrences of each integer. The program should also prompt the user for the number of integers that will be entered. As an example, if the user enters 10 integers (10, 20, 10, 30, 40, 49, 20, 10, 25, 10), the program output would be: 10 occurs 4 times 20 occurs 2 times 25 occurs 1 time 30 occurs 1 time...

  • Write a program that prompts the user for a positive integer, n, and then prints a...

    Write a program that prompts the user for a positive integer, n, and then prints a following shape of stars using nested for loop, System.out.print(“*”);, and System.out.println();. Example1: Enter a positive integer: 3 * * * * * * * * * Example2: Enter a positive integer: 5 * * * * * * * * * * * * * * * * * * * * * * * * * JAVA LANGUAGE!!

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

  • C++ Write a program that prompts the user to enter two positive integers: num1 and num2....

    C++ Write a program that prompts the user to enter two positive integers: num1 and num2. - Validate that num1 is less than num2 and that both numbers are positive. If any of these conditions are not met, allow the user to re-enter num1 and num2 until the input is determined valid. - For all integers from num1 through num2, print the word keyboard if the current integer is divisible by 2 and print the word mouse if the current...

  • JAVA. Write a program that reads in a series of positive integers and prints out the...

    JAVA. Write a program that reads in a series of positive integers and prints out the maximum value entered. The user will indicate they are finished entering numbers by entering zero or a negative integer.

  • 1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The...

    1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The program should use the for loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. If the user enters a zero or negative number, a message should be given and the program should not continue (see Sample Run...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT