Question

In: Computer Science

Java Program 1. Write a program that asks the user: “Please enter a number (0 to...

Java Program

1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered.

a. What is considered to be the body of the loop?

b. What is considered the control variable?

c. What is considered to be the test?

d. What is considered to be the update?

e. How can you incorporate the sentinel component into the test?

f. How can you calculate the sum of all previous numbers?

g. Which kind of loop do you think is more convenient to use? And why?

h. Construct the loop based on the information you collected

Solutions

Expert Solution

a.The statements inside the braces followed by the 'do' keyword are body of the loop

b. The user input 'n' is the control variable

c. The user input 'n' is the test

d. When we take the user input, it is the input

e. Sentinal component in the test is during the user input, when the data is updated for`

f. After each input, the value of the sum is updated by adding the value of user input

g. A do while loop is the most convenient to use, since we don't know how many times we need to repeat the process.

h. Code:

import java.util.*;

public class Main {

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        double n;
        do{
            //body of the loop
            System.out.print("Please enter a number (0 to exit) : ");
            n = sc.nextDouble();//update the value
            sum += n;//calculating the sum of the input values
        }while(n!=0);//n is the control variable

        System.out.println("The sum is : "+sum);
    }
}



Output:

Code Screenshot:


Related Solutions

Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
write a program that: 1) asks the user to enter grades, a negative number is the...
write a program that: 1) asks the user to enter grades, a negative number is the signal to stop entering. (while loop) - add each grade to a list (do not add the negative number to the list) after the while loop to enter grades is finished: 2) use a for loop on the list to calculate the average of all numbers in the list 3) use a for loop on the list to find the largest number in the...
Write a program that asks the user to enter an unsigned number and read it. Then...
Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6. COMMENT COMPLETE CODE PLEASE
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Design, plan, test, and write a computer program in Java that asks the user to enter...
Design, plan, test, and write a computer program in Java that asks the user to enter 1 number and a String. You will display the first n characters of the string where n is the number entered. For example, if the user enters 3 and java then you will print jav. If they enter 5 and Halloween then you print Hallo. If the user enters a number less than 0 then set the number to 0. Assume the user will...
Write a java program which asks the user to enter name and age and calls the...
Write a java program which asks the user to enter name and age and calls the following methods: printName(): Takes name as parameter and prints it 20 times using a while loop. printAge(): Takes age as parameter and prints all the numbers from 1 up to age. Write a java program that will print first 10 multiples of 3 in a single line.
In C#, write a console-based program that asks a user to enter a number between 1...
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you...
Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT