Question

5) Write a java program with scanner object to input first number and second number as...

5) Write a java program with scanner object to input first number and second number as variable with input statement and system out print statement.

Steps:

1) Declare scanner object

2) Ask system out print for first number and declare variable first number

3) Ask system out print for second number and declare variable second number

4) Declare first for loop where variable int i has already a value (your choice), that i is less then equal to ? and i?

5) Declare the inner for loop where variable int j has already a value (your choice), that j is less then equal to ? and j?

6) Declare the variable sum where i and j use addition (or multiplication or subtraction/ your choice)

7) Declare the if condition where sum is greater then 8 (or your choice) then break statement

8) In the end print statement where i and j are using your computation of choice and displaying the sum. Note: be careful with bracket.

Homework Answers

Answer #1

Code:

import java.util.Scanner;

class loop
{
   public static void main(String[] args)
   {
       Scanner scnr=new Scanner(System.in);
       /*Creating the scanner object*/
       System.out.print("Enter the first number: ");
       int num1=scnr.nextInt();
       /*Reading first number*/
       System.out.print("Enter the second number: ");
       int num2=scnr.nextInt();
       /*Reading second number*/
       int i=0,j=1;
       /*initializing the i ,j*/
       for( i=0;i<5;i++)
       {
           for(j=1;j<5;j++)
           {
               int sum=i+j;
               /*Decalred sum and adding i and j*/
               if(sum>=8)
               {
                   break;
                   /*Breaking the loop when sum is grater than or equals 8*/
               }
           }
       }
       System.out.print("sum="+sum+" i="+i+" j="+j);  
       /*Finally printing the sum i and j*/
   }
}

Output:

Indentation:

Know the answer?
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
Similar Questions
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
4) Create the following in a Java program. Create a Scanner Prompt the user to Receive...
4) Create the following in a Java program. Create a Scanner Prompt the user to Receive the amount and declare the variable double amount for scanner Declare variable integer remaining amount which is equal to (int) (amount*100) Find the number of one dollars bt declaring the integer variable which is equal to remaining amount divided by 100 Declare: remaining amount %=100 Find the number of quarters: Declare integer variable number of quarters equal to remaining amount divided by 25 Declare:...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
Java Programming In this lab students continue to write programs using multiple classes. By the end...
Java Programming In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to Write classes that use arrays and ArrayLists of objects as instance variables Write methods that process arrays and ArrayLists of objects Write getter and setter methods for instance variables Write methods using object parameters and primitive types Question- class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
Fibonacci Sequence (Javascript in HTML)   function fib(first, second, countdown){     console.log("I was called with " + first...
Fibonacci Sequence (Javascript in HTML)   function fib(first, second, countdown){     console.log("I was called with " + first + ", " + second + ", " + countdown);         if(countdown>0){                                 fib(first,first+second, countdown-1);         }     }     fib(1,2,3); Now you get to figure out how to use the fib function to print the number sequence without using a loop in the function (hint, the function needs to call itself). How is this exactly done? It needs to be in written so I can input it into...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it takes multiple sets of hours, minutes and seconds from the user in hh:mm:ss format and then computes the total time elapsed in hours, minutes and seconds. This can be done using the Time object already given to you. Tasks: Complete addTimeUnit() in AddTime so that it processes a string input in hh:mm:ss format and adds a new Time unit to the ArrayList member variable....