Java Loops I

Objective
In this challenge, we’re going to use loops to help us do some simple math.

Given an integer N print its first 10 multiples. Each multiple N x i  (where 1<= i <=10 ) should be printed on a new line in the form: N x i = result.

Input Format

A single integer.

Output Format

Print 10  lines of output; each line i contains the  of in the form:
N x i = result.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i=1;i<=10;i++)
{
System.out.println(n + ” x ” +i+” = “+(n*i));
}
}
}

The another way one can do it is

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int i=1;

while(i<=10)

{
System.out.println(n + ” x ” +i+” = “+(n*i));

i+=1;
}
}
}

So basically there are 4 loops in java

1. for loop
2. while loop
3. do while loop
4. for each

In this post you’ve seen both the for loop and while loop , next post will share the remaining loop structures

Feel free to comment.