How can I get this output in Java using scanner ?
Enter the number
6
—————————
6
12
18
24
30
36
42
48
54
60
66
72
78
84
90
96
BUILD SUCCESSFUL.
Answers
Explanation:
import Java.util.scanner;
public class Build
{
public static void main( )
{
Scanner sc = new Scanner (System.in);
System.out.println("enter the number");
int num = sc.nextInt( );
int j = 0;
for(int i = 1; i<=16; i++)
{
j = num * i ;
System.out.println( j );
if( i == 16 )
{
System.out.println("BUILD SUCCESSFUL");
}
}
}
}
Solution:
The given problem is solved in Java.
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int i,n;
System.out.println("Enter the number");
n = sc.nextInt();
sc.close();
System.out.println("—————————");
for(i = 1;i < 17;i++)
System.out.println(n*i);
System.out.println("BUILD SUCCESSFUL.");
}
}
Explanation:
From the output, we can guess that we have to display the first 16 multiples of the number entered by the user.
To do that,
(1) Accept the number from the user (say n) by using nextInt() method of scanner class.
(2) Create a for loop which can iterate 16 times. Here 'i' is the control variable.
(3) Display the product of the variables i and n. By doing this, you can calculate the multiples of the number.
(4) After the termination of the for loop, display BUILD SUCCESSFUL. using println() statement.
See attachment for output.