Question

In: Computer Science

​​​​​​​For java program. Write a while loop that will let the user enter a series of...

​​​​​​​For java program.

  1. Write a while loop 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.

for Loop

  1. Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3.
  1. Write a for loop to display a string entered by the user backwards.

do Loop

  1. Write a do .. loop for the random number dice problem from Chap Three. Add a counter variable that is used to count the number of times this code is executed in the loop and stops after 5 times.

roll = rand.nextInt(6) + 1;

      System.out.println(" Roll = " + roll);

ctr++;

Here is a sample output display:

The rolls are:

Roll = 3

Roll = 1

Roll = 4

Roll = 6

Roll = 4

Add a loop structure using the while loop and then add a for loop. The program should include 3 loops that do the same thing. What is the “best” loop structure to use for this problem and why?

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

package c13;

import java.util.Scanner;

public class AddNumbers {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int sum=0;
       int count=0;
       int input;
       while(true) {
           System.out.print("Enter number : ");
           input = sc.nextInt();
           if(input%2!=0) {
               break;
           }
           count++;
           sum +=input;
       }
       System.out.println("Total number of iteration : "+(count+1));
       System.out.println("Total number entered : "+(count));
       System.out.println("Total of the numbers is : "+sum);
   }
}

2)

package c13;

public class Main {
   public static void main(String[] args) {
       for(int i=13;i<=93;i+=10)
           System.out.println(i);
   }
}

3)

package c13;

import java.util.Random;

public class Main {
   public static void main(String[] args) {
       int roll;
       int ctr=1;
       Random rand = new Random();
       do {
           roll = rand.nextInt(6) + 1;
           System.out.println(" Roll = " + roll);
           ctr++;
       }while(ctr<=5);
   }
}


Related Solutions

Write a while loop that will let the user enter a series of integer values and...
Write a while loop 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. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user backwards. do Loop...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers....
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat, otherwise, it should terminate. Use Continue branching statement Write a program that reads an integer and display all the positive ODD numbers from 0 to n (integer entered by the user). Use CONTINUE...
JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
Write a java program to let the user enter the path to any directory on their...
Write a java program to let the user enter the path to any directory on their computers. Then list all the files in that directory.
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of the grades entered. When computing the average, make sure that there is...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Write a Java program called Decision that includes a while loop to prompt the user to...
Write a Java program called Decision that includes a while loop to prompt the user to enter 5 marks using the JOptionPane statement and a System. Out statement to output a message inside the loop highlighting a pass mark >= 50 and <= 100. Any other mark will output a messaging stating it’s a fail.
C++ while loop Exercise Write a program that continues to ask the user to enter any...
C++ while loop Exercise Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total. #include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT