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.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  1. Practice
  2. Java
  3. Introduction
  4. Java Loops I
  5. Discussions

Java Loops I

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

Sort 377 Discussions, By:

votes
  • recency
  • votes

Please Login in order to post a comment

  • [deleted] 2 years ago+ 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);
            }
        }
    }
    
    28|
    Permalink
    • LuQzz 2 years ago+ 1 comment

      This is clean! Awesome!

      0|
      ParentPermalink
      • VIPERCODER1 1 year ago+ 4 comments

        may i ask that where is the declaration of "i"?

        -4|
        ParentPermalink
        • Jordeatsu 1 year ago+ 1 comment

          within the for loop (int i = 1; i<=10; i++)

          0|
          ParentPermalink
          • VIPERCODER1 1 year ago+ 0 comments

            oh okay....i got it,thanks

            0|
            ParentPermalink
        • TARUNMATTA1994 12 months ago+ 0 comments

          int i is the declaration of i

          0|
          ParentPermalink
        • prem_vishwakarm1 10 months ago+ 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

          0|
          ParentPermalink
          • maniidamakanti_1 5 months ago+ 1 comment

            Why we use printf instead of println?

            -2|
            ParentPermalink
            • bhavnaharitsa 3 months ago+ 0 comments

              println ensures that the statement is printed in a fresh line

              0|
              ParentPermalink
        • Pathipati_arun 3 months ago+ 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.

          0|
          ParentPermalink
    • brendanford24 2 years ago+ 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^^?

      -8|
      ParentPermalink
      • brendanford24 2 years ago+ 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 ^^

        5|
        ParentPermalink
        • chigurupatichai1 2 years ago+ 0 comments

          is it working

          0|
          ParentPermalink
        • rmyoung04 2 years ago+ 0 comments

          Good call on using printf. However, you need to close scanner.

          0|
          ParentPermalink
        • himaanshurocks 1 year ago+ 3 comments

          you can simply write:

          System.out.println(input+" x "+i+" = "+(2*i));

          1|
          ParentPermalink
          • celik_zeki 1 year ago+ 0 comments

            i did this but after i saw formatted print i am not contented with my code :(

            0|
            ParentPermalink
          • nilupulee_hansa1 12 months ago+ 2 comments

            Here "2" should not be a constant. right? It is the input value. Isn't it?

            0|
            ParentPermalink
            • zee13_khan 11 months ago+ 0 comments

              Then it will be the output for just 2, here we are taking input number to generalize the code

              0|
              ParentPermalink
            • asaxenarec123 5 months ago+ 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...

              0|
              ParentPermalink
          • aakashkumarjee 8 months ago+ 0 comments

            at the end you should write System.out.println(input+" x "+i+" = "+(input*i));

            0|
            ParentPermalink
        • amittomar87 6 months ago+ 0 comments

          Could someone plz elaborate the use of "%d x %d = %d\n"? How did it manage to come as n * i?

          0|
          ParentPermalink
        • kumar_santosh959 6 months ago+ 0 comments

          System.out.printf("%d x %d = %d%n", n, i, n*i); try this it work

          -3|
          ParentPermalink
      • Venkat30119 1 year ago+ 2 comments

        input.hasNextInt() purpose of using it??

        1|
        ParentPermalink
        • himaanshurocks 1 year ago+ 0 comments

          not sure . but it may be useful to check whether there exists a new int or not.

          1|
          ParentPermalink
        • aakashkumarjee 8 months ago+ 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

          0|
          ParentPermalink
          • ghazi261 7 months ago+ 1 comment

            what does it mean by token in this case? Can you please explain or refer to an explaination? Thanks

            0|
            ParentPermalink
            • aakashkumarjee 4 months ago+ 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.

              0|
              ParentPermalink
      • ganeshethiraj185 5 months ago+ 0 comments

        util package is enough

        0|
        ParentPermalink
    • ravi_bhatt_75491 2 years ago+ 1 comment

      it should be \n instead of %n

      -1|
      ParentPermalink
      • [deleted] 2 years ago+ 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

        3|
        ParentPermalink
        • deekshithsagar73 1 year ago+ 0 comments

          yes it should

          -6|
          ParentPermalink
    • vjitesh 2 years ago+ 3 comments

      Will you explain me: System.out.printf("%d x %d = %d%n", N, i, N*i);

      -7|
      ParentPermalink
      • sara99 1 year ago+ 0 comments
        [deleted]
        0|
        ParentPermalink
      • deekshithsagar73 1 year ago+ 0 comments

        NOO

        -11|
        ParentPermalink
      • karthikshenoy93 1 year ago+ 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

        2|
        ParentPermalink
        • tasfiahm 1 year ago+ 0 comments

          Thanks a lot. Today I have lerned something new and simple but very inportant in fromating lines.

          0|
          ParentPermalink
    • rgdeekshith 1 year ago+ 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?

      0|
      ParentPermalink
      • [deleted] 1 year ago+ 0 comments
        [deleted]
        -1|
        ParentPermalink
      • deekshithsagar73 1 year ago+ 0 comments

        NOO

        -15|
        ParentPermalink
      • fodorelli 1 year ago+ 1 comment

        System.out.println(); is efficient for simply printing a line of text. If the line of text needs to be formatted (ex: alignment (left-justified, etc.), etc.), then System.out.printf(); would be used

        1|
        ParentPermalink
        • abhi7625 12 months ago+ 0 comments
          [deleted]
          1|
          ParentPermalink
      • kacha_nimbu 9 months ago+ 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
        
        0|
        ParentPermalink
    • Pricks_123 1 year ago+ 1 comment

      Exactly Correct.Thanks for uploading solution

      0|
      ParentPermalink
      • deekshithsagar73 1 year ago+ 0 comments

        thanks for uploading comment

        -5|
        ParentPermalink
    • abhijitaj123 1 year ago+ 3 comments

      i m facing this issue, can u pls help me?

      error: no suitable method found for println(String,int,int,int)

      0|
      ParentPermalink
      • Pricks_123 1 year ago+ 0 comments

        can u share ur entire code, so that we could analize ur issue

        0|
        ParentPermalink
      • deekshithsagar73 1 year ago+ 0 comments

        noo

        -9|
        ParentPermalink
      • kumchandan07 11 months ago+ 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)

        0|
        ParentPermalink
    • sownderya29 1 year ago+ 0 comments

      can u explain this!!without getting a value how this get printed and ya also you are using printf here

      0|
      ParentPermalink
    • imvikash007k 1 year ago+ 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.

      0|
      ParentPermalink
    • 3333ayannkhan 1 year ago+ 1 comment

      can you explain this : %d x %d

      1|
      ParentPermalink
      • VIPERCODER1 1 year ago+ 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 :)

        0|
        ParentPermalink
    • manojred221 12 months ago+ 0 comments

      will this pass the test case with N=45

      0|
      ParentPermalink
    • singhsaurabh_ki1 11 months ago+ 0 comments

      how i print x ??? shift +8=*.. but its wrong????why?

      0|
      ParentPermalink
    • umarbasha007 9 months ago+ 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();
      }
      
      0|
      ParentPermalink
      • Saitama_sensei 9 months ago+ 1 comment

        what does scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); do???

        1|
        ParentPermalink
        • kacha_nimbu 9 months ago+ 0 comments

          Couldn't find much about it. My code is working fine with and without

          0|
          ParentPermalink
    • ishraqisc 9 months ago+ 0 comments

      Why aren't you setting up the constraint for N, i.e. 2<=N<=20?

      0|
      ParentPermalink
    • shivayush143 8 months ago+ 0 comments

      in the for loop instead of using only i y do u use integer type i. u cn show it in above.

      0|
      ParentPermalink
    • shivayush143 8 months ago+ 0 comments

      does it take the value of N throgh user

      0|
      ParentPermalink
    • ankushchandole 7 months ago+ 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);

      0|
      ParentPermalink
      • whawkins90 4 months ago+ 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.

        0|
        ParentPermalink
    • momsteachcode 7 months ago+ 0 comments

      Why can't we use println here instead of printf, why is it showing an error

      0|
      ParentPermalink
    • krishnadulaldal1 5 months ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • tigerO 4 months ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • bhavnaharitsa 3 months ago+ 0 comments

      But,where is the cross symbol in keyboard?

      0|
      ParentPermalink
    • ngveerendran 2 weeks ago+ 0 comments

      why println is not working for this code

      0|
      ParentPermalink
  • spacetaco94 2 years ago+ 0 comments

    for(int i=0; i<10; i++){

    System.out.printf("%d x %d = %d\n", N, (i+1), (N*(i+1)));
    

    }

    5|
    Permalink
  • Emmanuelmireku15 1 year ago+ 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);
            }
        }
    }
    
    2|
    Permalink
    • lochek_alon 1 year ago+ 1 comment

      this is exactly how i solved it, but need to keep using printf, i think, since everyone uses it here.

      0|
      ParentPermalink
      • todd37 6 months ago+ 0 comments

        YES, the directions indicate you must use printf (format printing).

        0|
        ParentPermalink
    • contactredch 4 months ago+ 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(); }

      0|
      ParentPermalink
      • whawkins90 4 months ago+ 0 comments

        Looks like your call to println() is outside your loop.

        0|
        ParentPermalink
  • pnssoftwares7 2 months ago+ 1 comment

    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); what is the mean of given statement?

    1|
    Permalink
    • tejasparmar12111 1 month ago+ 0 comments

      remove this"scanner.skip......" and simply write your own code.

      0|
      ParentPermalink
  • uvkrishnasai 8 months ago+ 0 comments

    IntStream.rangeClosed(1,10).forEachOrdered(i -> System.out.println(String.format("%d x %d = %d", N, i, i*N)));

    1|
    Permalink
  • vepalisantosh 2 days ago+ 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?

    0|
    Permalink
  • ramandwivedikan1 2 days ago+ 0 comments

    how to insert cross symbol.

    0|
    Permalink
  • alfa_voland 6 days ago+ 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);
            }
    
    0|
    Permalink
  • prashanthoods 1 week ago+ 0 comments

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

        }
    
    0|
    Permalink
  • Ghulam_Nadeem 1 week ago+ 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])?");
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature