Question

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.

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

import java.util.*;

class Solution

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

       

        // store the number of elements entered

        int count = 0;

       

        // store the total of elements entered

        int total = 0;

       

        // infinite loop

        while(true)

        {

            // get user input

            int x = sc.nextInt();

           

            // if the number eneterd is odd

            if( x % 2 != 0 )

                break;

           

            total += x;

            count++;

        }

       

        System.out.println("Total of all elements : " + total);

        System.out.println("Total number of elements : " + count);

    }

}


Sample Output

Add a comment
Know the answer?
Add Answer to:
Write a while loop in java that will let the user enter a series of 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
  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • 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...

  • (java) Write a While loop that prompts the user for only even numbers. Keep prompting the...

    (java) Write a While loop that prompts the user for only even numbers. Keep prompting the user to enter even numbers until the user enters an odd number. After the loop ends, print the sum of the numbers. Do not include that last odd number. You should not store the numbers in an array, just keep track of the sum. Make sure to use a break statement in your code. You may assume that a java.util.Scanner object named input has...

  • Write Java code that does the following Ask the user to enter a file name. Ask...

    Write Java code that does the following Ask the user to enter a file name. Ask the user for two integers as the lower bound and the upper bound. Use a loop to write all the odd numbers between the lower bound and the upper bound (inclusive) to the file, and then close the file. Use either while loop, do while loop, or for loop to complete the program. PrintWriter class is used. The file is closed before the program...

  • Write a java program that asks the user to enter an integer. The program should then...

    Write a java program that asks the user to enter an integer. The program should then print all squares less than the number entered by the user. For example, if the user enters 120, the program should display 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

  • (C++ Microsoft Visual Studio) [15 Points] Write a while loop to calculate the average of numbers...

    (C++ Microsoft Visual Studio) [15 Points] Write a while loop to calculate the average of numbers entered by the user:  Ask the user to enter an integer number or -1 to stop  Add the entered numbers  Compute the average of the numbers outside the loop after termination  Print the average

  • Java Programming Write a method named isEven that accepts an integer argument. The method should return...

    Java Programming Write a method named isEven that accepts an integer argument. The method should return true if the argument is even, or false if not. The program’s main method should use a loop to generate a random integer. The number of random integers is provided by a user, who must enter a number greater than 10. The loop should then invoke the isEven method to determine whether each integer is even or odd, display the random number and the...

  • JAVA Ask the user for integers. If the user did not enter an integer, ask again....

    JAVA Ask the user for integers. If the user did not enter an integer, ask again. (Type Safe Input) Keep a running total of the intergers entered (sum). Once the user enters 0, stop looping. Print the sum of all the numbers. Do not use try-catch. EXAMPLE OUTPUT: Enter an integer, 0 to stop> [fasdfsa] Invalid input. Enter an integer, 0 to stop> [231.342] Invalid input. Enter an integer, 0 to stop> [1] Enter an integer, 0 to stop> [2]...

  • 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 C++ program that uses a do or a while loop that has the user...

    Write a C++ program that uses a do or a while loop that has the user enter golf scores, terminated by a zero. After the zero has been entered, display the number of scores entered and their average.

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