We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/*This is the simple and easy method to solve this question, i just simply run loop with the 2 number given in problem set and do arithematic calculation and then print as per the guideline, I use for loop cause i know how many time loop is going to run then this section is more easy to understand which i given below:
Syntax of for loops:
** for (initialisation; condition; increment/decrement)**
System.out.println(n + " x " + i + " = " + (n * i));
n + " x "
When you use + with a number and a string, Java converts the number to text and concatenates.
If n is 5, this produces "5 x ".
+ i + " = "
Next, Java takes that result and appends the current loop index i, then the literal " = ".
If i is 3, you now have "5 x 3 = ".
+ (n * i)
The expression inside parentheses (n * i) is evaluated first (so you multiply two integers).
If n = 5 and i = 3, that gives 15, which is then converted to the string "15" and appended.
System.out.println(...)
Finally, println prints that full string and moves to a new line.
Putting it all together, when n = 5 and i = 3, you get:
Copy code
5 x 3 = 15
.................................................................If you like my solution,approach hit the one like for appreciation.
*/
import java.util.Scanner;
public class hackerrankloop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
for (int i = 1; i <= 10; i++)
System.out.println
(N + " x " + i + " = " + ( N * i ));
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Loops I
You are viewing a single comment's thread. Return to all comments →
/*This is the simple and easy method to solve this question, i just simply run loop with the 2 number given in problem set and do arithematic calculation and then print as per the guideline, I use for loop cause i know how many time loop is going to run then this section is more easy to understand which i given below: Syntax of for loops:
System.out.println(n + " x " + i + " = " + (n * i)); n + " x " When you use + with a number and a string, Java converts the number to text and concatenates. If n is 5, this produces "5 x ". + i + " = " Next, Java takes that result and appends the current loop index i, then the literal " = ". If i is 3, you now have "5 x 3 = ". + (n * i) The expression inside parentheses (n * i) is evaluated first (so you multiply two integers). If n = 5 and i = 3, that gives 15, which is then converted to the string "15" and appended. System.out.println(...) Finally, println prints that full string and moves to a new line. Putting it all together, when n = 5 and i = 3, you get: Copy code 5 x 3 = 15 .................................................................If you like my solution,approach hit the one like for appreciation. */
import java.util.Scanner;
public class hackerrankloop { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
}