- Practice
- Java
- Introduction
- Java Loops I
- Discussions
Java Loops I
Java Loops I
[deleted] + 22 comments As a C programmer
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.printf("%d x %d = %d%n", N, i, N*i); } } }
LuQzz + 1 comment This is clean! Awesome!
VIPERCODER1 + 4 comments may i ask that where is the declaration of "i"?
Jordeatsu + 1 comment within the for loop (int i = 1; i<=10; i++)
VIPERCODER1 + 0 comments oh okay....i got it,thanks
TARUNMATTA1994 + 0 comments int i is the declaration of i
prem_vishwakarm1 + 1 comment its up to you define it in main function or inside the for loop its up to you. just dont forgrt to initialize it to 1
maniidamakanti_1 + 1 comment Why we use printf instead of println?
bhavnaharitsa + 0 comments println ensures that the statement is printed in a fresh line
Pathipati_arun + 0 comments you can declare before for loop also ex: int i; for(i=1;i<10;i++) { code } but it is not recomended in java.
brendanford24 + 3 comments What have you imported for this code?
public class Solution { public static void main(String[] args) { Scanner input = new Scanner(System.in); for (int i = 1; i <= 10 && input.hasNextInt(); i++) { int n = input.nextInt(); System.out.printf("%d x %d = %d\n", n, i, (n*i)); } } }
Mine wont work^^?
brendanford24 + 5 comments my bad, i see where i kooked it
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); for (int i = 1; i <= 10; i++) { System.out.printf("%d x %d = %d\n", n, i, n*i); } } }
Now working ^^
chigurupatichai1 + 0 comments is it working
rmyoung04 + 0 comments Good call on using printf. However, you need to close scanner.
himaanshurocks + 3 comments you can simply write:
System.out.println(input+" x "+i+" = "+(2*i));
celik_zeki + 0 comments i did this but after i saw formatted print i am not contented with my code :(
nilupulee_hansa1 + 2 comments Here "2" should not be a constant. right? It is the input value. Isn't it?
zee13_khan + 0 comments Then it will be the output for just 2, here we are taking input number to generalize the code
asaxenarec123 + 0 comments why not ?? here 2 will be constant because here we have to only table of 2, you can see output of given instruction...
aakashkumarjee + 0 comments at the end you should write System.out.println(input+" x "+i+" = "+(input*i));
amittomar87 + 0 comments Could someone plz elaborate the use of "%d x %d = %d\n"? How did it manage to come as n * i?
kumar_santosh959 + 0 comments System.out.printf("%d x %d = %d%n", n, i, n*i); try this it work
Venkat30119 + 2 comments input.hasNextInt() purpose of using it??
himaanshurocks + 0 comments not sure . but it may be useful to check whether there exists a new int or not.
aakashkumarjee + 1 comment this method tells about the token present in te buffer. i.e it will return true if token is present and false if it is not. for example, take a look at this
public class ScannerDemo { public static void main(String[] args) {
String s = "Hello World! 3 + 3.0 = 6"; // create a new scanner with the specified String Object Scanner scanner = new Scanner(s); // check if the scanner has a token System.out.println("" + scanner.hasNext()); // print the rest of the string System.out.println("" + scanner.nextLine()); // check if the scanner has a token after printing the line System.out.println("" + scanner.hasNext()); // close the scanner scanner.close();
} }
output of this program will be
true
Hello World! 3 + 3.0 = 6
false
ghazi261 + 1 comment what does it mean by token in this case? Can you please explain or refer to an explaination? Thanks
aakashkumarjee + 0 comments basically token is the smallest building block of any program. the scanner object may or may not contain any token, hence first we confirm it.
ganeshethiraj185 + 0 comments util package is enough
ravi_bhatt_75491 + 1 comment it should be \n instead of %n
[deleted] + 1 comment no it shouldn't
"A new line character appropriate to the platform running the application. You should always use %n, rather than \n."
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
deekshithsagar73 + 0 comments yes it should
vjitesh + 3 comments Will you explain me: System.out.printf("%d x %d = %d%n", N, i, N*i);
sara99 + 0 comments [deleted]deekshithsagar73 + 0 comments NOO
karthikshenoy93 + 1 comment Printf is used to format a output as required; %d is for int, similarly we have %s for String and %f for float and so on. Also we have %n for the new line which serves the purpose of println for printf. Here the first %d is matched with the first value after the ',', if the format dosent match exception is thrown. %d --> N %d --> i %d --> N*i
tasfiahm + 0 comments Thanks a lot. Today I have lerned something new and simple but very inportant in fromating lines.
rgdeekshith + 4 comments can you please explain sir why did you use "printf" function instead of "println" function and kindly could you tell me its purpose?
[deleted] + 0 comments [deleted]deekshithsagar73 + 0 comments NOO
kacha_nimbu + 0 comments public static void main(String[] args) { int N = scanner.nextInt(); int iLoop; //scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for(iLoop=1;iLoop<11;iLoop++) { int ans= N*iLoop; System.out.println(N+" x "+iLoop+" = "+ans); } scanner.close(); you can use it like this
Pricks_123 + 1 comment Exactly Correct.Thanks for uploading solution
deekshithsagar73 + 0 comments thanks for uploading comment
abhijitaj123 + 3 comments i m facing this issue, can u pls help me?
error: no suitable method found for println(String,int,int,int)
Pricks_123 + 0 comments can u share ur entire code, so that we could analize ur issue
deekshithsagar73 + 0 comments noo
kumchandan07 + 0 comments Use printf(String,int,int,int) instead. There is no overloaded method of println having arguments as String,int,int,int. Incorrect: println(String,int,int,int)
sownderya29 + 0 comments can u explain this!!without getting a value how this get printed and ya also you are using printf here
imvikash007k + 0 comments I Need a Help Regarding N limit N>=2&&N<=20 No one Gave The range of N so for this challenge if n=30 the following answer will not work.
3333ayannkhan + 1 comment can you explain this : %d x %d
VIPERCODER1 + 0 comments Yes ofcourse my friend, the places where you see %d, you will have to presume that the value of the element that is needed to be assigned will replace it. we will just have to keep in mind its syntax that is sopf( %d * %d ", a, b); to make you more clear i will also say that in the above line the first %d will replace it with the first element after thye double quotes that is a. thank you :)
manojred221 + 0 comments will this pass the test case with N=45
singhsaurabh_ki1 + 0 comments how i print x ??? shift +8=*.. but its wrong????why?
umarbasha007 + 1 comment 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++) { // Can use \n instead of %n, both works fine System.out.printf("%d x %d = %d\n",N,i,N*i); } scanner.close(); }
Saitama_sensei + 1 comment what does scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); do???
kacha_nimbu + 0 comments Couldn't find much about it. My code is working fine with and without
ishraqisc + 0 comments Why aren't you setting up the constraint for N, i.e. 2<=N<=20?
shivayush143 + 0 comments in the for loop instead of using only i y do u use integer type i. u cn show it in above.
shivayush143 + 0 comments does it take the value of N throgh user
ankushchandole + 1 comment what is the diffrence between these two lines
System.out.println("%d x %d = %d ",N,i,N*i); &
System.out.printf("%d x %d = %d%n", N, i, N*i);whawkins90 + 0 comments The printf method expects a formatted String as its first argument and will substitute the values of the subsequent parameters into the String. The println method expects a non-formatted String; it will print exactly what you pass into it and will not perform any substitution.
momsteachcode + 0 comments Why can't we use println here instead of printf, why is it showing an error
krishnadulaldal1 + 0 comments [deleted]tigerO + 0 comments [deleted]bhavnaharitsa + 0 comments But,where is the cross symbol in keyboard?
ngveerendran + 0 comments why println is not working for this code
spacetaco94 + 0 comments for(int i=0; i<10; i++){
System.out.printf("%d x %d = %d\n", N, (i+1), (N*(i+1)));
}
Emmanuelmireku15 + 2 comments This is what I got but after reading the discussions, I a lot of people using the printf method. But this code here will do the same job. This code is in Java 8.
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); int input = sc.nextInt(); sc.close(); for(int i = 1; i <= 10; i++){ int answer = input*i; System.out.println(input + " x " + i + " = " + answer); } } }
lochek_alon + 1 comment this is exactly how i solved it, but need to keep using
printf
, i think, since everyone uses it here.todd37 + 0 comments YES, the directions indicate you must use printf (format printing).
contactredch + 1 comment can't understand why mine gives the error. 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++){ int z = N*i; } System.out.println(N + " x " + i + " = " + z); scanner.close(); }
whawkins90 + 0 comments Looks like your call to println() is outside your loop.
pnssoftwares7 + 1 comment scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); what is the mean of given statement?
tejasparmar12111 + 0 comments remove this"scanner.skip......" and simply write your own code.
uvkrishnasai + 0 comments IntStream.rangeClosed(1,10).forEachOrdered(i -> System.out.println(String.format("%d x %d = %d", N, i, i*N)));
vepalisantosh + 0 comments import java.util.*;
public class Solution {
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.close(); for(int i = 1; i <= 10; i++){ System.out.println(n + " x " + i + " = " + n * i); } }
}
can any one tell me the meaning of System.out.println(n + " x " + i + " = " + n * i); clearly?
ramandwivedikan1 + 0 comments how to insert cross symbol.
alfa_voland + 0 comments Scanner scan = new Scanner(System.in); int N = scan.nextInt(); for(int j = 1; j <= 10; j++){ System.out.println( N + " x " + j + " = " + N*j); }
prashanthoods + 0 comments for(int i = 1; i<=10; i++) { int s = N*i; System.out.println(N+" "+"x"+" "+i+" = "+s);
}
Ghulam_Nadeem + 0 comments i got the output successfully.
But i want to know what is the use of this below line in our code.
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
Sort 377 Discussions, By:
Please Login in order to post a comment