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.
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:
Get Answers For Free
Most questions answered within 1 hours.