Skip to main content

HackerRank Java Practice: Java Loops I

Click here to solve it by yourself! 

Question:


Objective

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

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

Input Format

A single integer, .

Constraints

Output Format

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


Code:


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));


        int N = Integer.parseInt(bufferedReader.readLine().trim());
        int i;
        if(N>=2&&N<=20){
            for(i=1;i<=10;i=i+1){
                System.out.printf("%d x %d = %d %n",N,i,N*i);
            }
        }
        bufferedReader.close();
    }

}

Comments