Question

In: Computer Science

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 branching statement to skip the display of EVEN in the result.

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Question 1 :Do While Loop

Here a new java program with name "DoWhileNumbers.java" is created, which contains following code.

DoWhileNumbers.java :

import java.util.*;//import package
//Java class
public class DoWhileNumbers {
//entry point of program , main() method
   public static void main(String[] args) {
       // creating object of Scanner class
       Scanner sc=new Scanner(System.in);
       //using do while loop asking user to enter numbers
       do
       {
           //asking user to enter number 1
           System.out.print("Enter Number 1: ");
           int num1=sc.nextInt();//reading number1
           //asking user to enter number 2
           System.out.print("Enter Number 2: ");
           int num2=sc.nextInt();//reading number2
           //print result
           System.out.println("Sum of "+num1+" and "+num2+" is : "+(num1+num2));
           //asking user whether want to perform operation again
           System.out.print("Want to sum another numbers : ");
           String response=sc.next();//reading response
           //checking response
           if(response.equals("Yes"))
           {
               continue;
           }
           else
           {
               break;
           }
                      
       }while(true);

   }

}

======================================================

Output : Compile and Run DoWhileNumbers.java in the browser and will get the screen as shown below

Screen 1 :DoWhileNumbers.java

**************************************

Question 2:

OddNumbers.java :

import java.util.*;//import package
//Java class
public class OddNumbers {
   //entry point of program , main() method
   public static void main(String[] args) {
       // creating object of Scanner class
       Scanner sc=new Scanner(System.in);
       //asking user to enter number
       System.out.print("Enter Number : ");
       int num=sc.nextInt();//reading number
       if(num >0)
       {
       //using for loop checking numbers
       for(int i=1;i<=num;i++)
       {
           //checking odd numbers
           if(i%2!=0)
           {
               //display odd number
               System.out.print(i+" ");
           }
           else
           {
               continue;//continue the loop
           }
       }
       }
       else
       {
           //when negative number is entered
           System.out.print("Enter number greater than 0 ");
       }

   }

}
============================

Screen 1 :Screen when number less than 0 is entered

Screen 2 :Screen showing positive odd numbers

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

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...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. 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...
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
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
Inside a do while loop ask a user to enter two numbers (use a single scanf...
Inside a do while loop ask a user to enter two numbers (use a single scanf function). Inside the same loop ask the user whether they wish to find the min or max of the values entered. If the user enters a 0 call the minFunction; if the user enters a 1 call the maxFunction; otherwise print Not valid input. Pass the two first numbers entered as parameters to the functions. Inside minFunction and maxFunction calculate the minimun and the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
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...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT