Skip to content

Instantly share code, notes, and snippets.

@AlexanderChen0
Created February 6, 2019 23:26
Show Gist options
  • Save AlexanderChen0/dcdf9c4431fb29bcc817656503a377ea to your computer and use it in GitHub Desktop.
Save AlexanderChen0/dcdf9c4431fb29bcc817656503a377ea to your computer and use it in GitHub Desktop.
Hackerrank Java Loops
/*
Java Loops
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 x i in the form: N x i = result.
Sample Input: 2
Sample Output:
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
*/
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])?");
for(int i = 1; i <= 10; i++){
System.out.println(N + " x " + i + " = " + N*i);
}
scanner.close();
}
}
/*
Input: 2
Your Output:
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
Expected Output:
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
*/
@krishnaswgithub
Copy link

import java.io.;
import java.math.
;
import java.security.;
import java.text.
;
import java.util.;
import java.util.concurrent.
;
import java.util.function.;
import java.util.regex.
;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

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());

    //bufferedReader.close();
    for(int i = 1; i <= 10; i++){
        System.out.println(N + " x " + i + " = " + N*i);
    }
}

}

@mayank310703
Copy link

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) {
Scanner sc =new Scanner(System.in);
int N =sc.nextInt();
for (int i=1;i<11;i++){
int result = N*i;
System.out.println(N + " x " +i+ " = "+result);
}
}
}

@Sirieed
Copy link

Sirieed commented Nov 20, 2023

Can anyone explain this statement System.out.println(N + " x " + i + " = " + N*i);?

@mayank310703
Copy link

Can anyone explain this statement System.out.println(N + " x " + i + " = " + N*i);?

@Sirieed
System.out.println (N + " x " + i + " = " + Ni);
is used for printing the output on the screen
where ,
N ==input of number which we will be giving ,
"x" == is used as multiplication symbol ,
"i" == it is the loop count ,
+N
i == outputs the result in the print statement .

@ashu22121
Copy link

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) {
    int n = 2;
    /*from w ww .  ja v  a 2s  .co  m*/
    int sum;
    
    for(int i = 1; i <= 10; i++){
        sum = n * i;
        System.out.println(n + " x " + i + " = " + sum);
    }
}

}

@nethsaraPrabash
Copy link

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());
    
    

    bufferedReader.close();
    
    for(int i = 1;i <= 10;i++)
    {
        System.out.println(N+" x "+i+" = "+(N*i));
    }
}

}

@TejaswararaoNagireddy
Copy link

import java.util.Scanner;
public class Main {
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);
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment