Java Loops I – Hacker Rank Solution
Hello Friends, How are you? Today I will solve the HackerRank Java Loops I Problem with a very easy explanation. This is the 6th problem of Java on HackerRank. In this article, you will get more than three approaches to solving this problem. So let's start-
HackerRank Java Loops I - Problem Statement
Objective
In this challenge, we're going to use loops to help us do some simple math.
Task
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, N.
Constraints
- 2 <= N <= 20
Output Format
Print 10 lines of output; each line i (where 1<= i <= 10) contains the result of N * i in the form:
N x i = result.
Sample Input:
3 (code-box)
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 (code-box)
Java Loops I – Hacker Rank Solution
Approach I:
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 { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int N = scanner.nextInt(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); int i; for(i=1;i<=10;i++) { System.out.println(+N +" x "+i+" = "+N*i); } scanner.close(); } }
import java.io.*; import java.util.*; 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); } } }
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); for (int i = 1; i <= 10; ++i) { System.out.printf("%d x %d = %d\n", N, i, N * i); } } }
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i = 1; i<=10; i++) { int result=n*i; System.out.println(n+" x "+i+" = "+result); } } }
Also Read:
Disclaimer: The above Problem ( Java Loops I ) is generated by Hackerrank but the Solution is Provided by Code Solution. This tutorial is only for Educational and Learning purposes. Authority if any queries regarding this post or website fill out the contact form.
I hope you have understood the solution to this HackerRank Problem. All these four solutions will pass all the test cases. Now visit Java Loops I HackerRank Problem and try to solve it again.
All the Best!