Question

Write a java program that keeps asking the user to enter a number until the user...

Write a java program that keeps asking the user to enter a number until the user quits and prints how many even and how many odd numbers entered by the user. The loop terminates if the user enters -1 (it indicates that there will be no more number entering by the user). In other words, as long as the user doesn’t enter -1, the loop will be executed. Please add comments to the program to understand/ explain the code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

import java.util.Scanner;
public class Test{

public static void main(String []args){
int n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number or -1 to quit: ");
n=sc.nextInt();
int ct1=0,ct2=0;
while(n!=-1)
{
if(n%2==0)
ct1++;
else
ct2++;
System.out.println("Enter a number or -1 to quit: ");
n=sc.nextInt();
}

System.out.println("Number of even numbers are "+ct1);
System.out.println("Number of odd numbers are "+ct2);
  
}
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Write a java program that keeps asking the user to enter a number until the user...
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
  • Using Python, write a program that keeps asking the user for text until they type "exit"...

    Using Python, write a program that keeps asking the user for text until they type "exit" To Exit Or Not Write a program that keeps asking the user for text until they type "exit" Console 1 Enter number 1 3 Enter number 5 5 Enter number 7 Enter number 8 1 9 Enter number 10 9 11 Enter number 12 3 13 Enter number 14 4 15 Enter number 16 exit 17 Bye! 18

  • 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 Java program that: • Asks the user to enter the number of integers that...

    Write a Java program that: • Asks the user to enter the number of integers that he need to enter; • Asked him to enter integer by integer; • Print The number of Odd numbers entered and the number of Even numbers entered. Important notes: 1. You should have to copy and paste the Java as your answer for this question. DON’T take screen shot for your Java Code. It must be editable. 2. Take a screen shot for your...

  • Write a while loop in java that will let the user enter a series of integer...

    Write a while loop in java that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates.

  • C++ please no arrays! Create a program that will allow the user to enter up to...

    C++ please no arrays! Create a program that will allow the user to enter up to 50 whole values and determine the number of values entered, how many of the values were odd and how many of the values were even. Your code should contain 2 functions, a main function and a function to enter the numbers enter and count the values. Your main function should be a driver program that will call another function to count the total number...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • Write a Java program to prompt for inputting an integer N, then enter N integers (a...

    Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...

  • Write a java program that asks the user to enter 2 integers, obtains them from the...

    Write a java program that asks the user to enter 2 integers, obtains them from the user, determines if they are even or odd numbers and prints their sum, product, difference, and quotient (Division).

  • Write a c++ complete program to meet the specifications. The program should prompt the user for...

    Write a c++ complete program to meet the specifications. The program should prompt the user for a positive integer. The program should print a message whether the integer is even or odd. The looping should end when the user enters a negative number. The negative number will not be tested for even or odd. The program will print out a message of how many numbers were entered (not counting the negative number) and how many even and odd numbers were...

  • In Java, write a program using a loop that asks the user to enter a series...

    In Java, write a program using a loop that asks the user to enter a series of decimal numbers. The user must enter -88 to end the input of the decimal numbers. After the user enters all numbers, the program should display the sum of all numbers entered.

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