4 Replies Latest reply: May 6, 2013 12:05 PM by 939520 RSS

    Trying to generate pattern

    967983
      Hi All,

      I am trying to generate a pattern whose partial sample output is shown below.

      i=1 j=1 k=1
      i=1 j=1 k=2
      i=1 j=1 k=3
      i=1 j=1 k=4
      i=1 j=1 k=5
      i=1 j=1 k=6
      i=1 j=1 k=7
      i=1 j=1 k=8
      i=1 j=1 k=9
      i=1 j=1 k=10
      i=1 j=2 k=1
      i=1 j=2 k=3
      i=1 j=2 k=5
      i=1 j=2 k=7
      i=1 j=2 k=9
      i=1 j=2 k=2
      i=1 j=2 k=4
      i=1 j=2 k=6
      i=1 j=2 k=8
      i=1 j=2 k=10
      i=1 j=3 k=1
      i=1 j=3 k=4
      i=1 j=3 k=7
      i=1 j=3 k=10
      i=1 j=3 k=2
      i=1 j=3 k=5
      i=1 j=3 k=8
      i=1 j=3 k=3
      i=1 j=3 k=6
      i=1 j=3 k=9
      i=1 j=4 k=1
      i=1 j=4 k=5
      i=1 j=4 k=9
      i=1 j=4 k=2
      i=1 j=4 k=6
      i=1 j=4 k=10
      i=1 j=4 k=3
      i=1 j=4 k=7
      i=1 j=4 k=4
      i=1 j=4 k=8
      ...

      The value of k is incremented by the value of j at that particular time. Your help is appreciated. This is just the pattern I am trying to generate.

      My attempt to generate the output is:
      public class One {
           public static void main(String args[])
           {
                Calculate();
           }
           static void Calculate()
           {
                for(int i=1;i<=10;i++)
                {
                     for(int j=i;j<=10;j=j+i)
                     {
                          for(int k = 1;k<=10;k=k+j)
                          {
                               System.out.println("i="+i+" j="+j+" k="+k);
      
                          }                                        
                     }
                }
           }
      
      }
      Thanks
        • 1. Re: Trying to generate pattern
          gimbal2
          You can't use k to do the counting for the most inner loop. The most inner loop needs to run 10 times and ALSO needs to calculate a value to display as 'k'. This value starts at 1 and then increases 'j' with each iteration, never going over 10.

          You're almost there, now think about it a little more and finish this sucker yourself. Don't rob yourself of the eureka moment where you figure it out all on your own - that feeling (plus the satisfaction of actually finishing something) is what makes programming fun.
          • 2. Re: Trying to generate pattern
            967983
            I already used three loops and I think the loop using the variable i is unnecessary. I have sought for help to this pattern after trying for myself many times, but no use. I request anyone who knows the answer to post the solution.
            • 3. Re: Trying to generate pattern
              aksarben
              I think the problem would much more clear if you would describe in English (not code) what rules the output should follow. I can't tell what the desired outcome is based on the code & your vague description. The sample output was not helpful to me.
              • 4. Re: Trying to generate pattern
                939520
                Assuming your data has no defined algorithm and is finite in length, how about sticking all the numbers in a (static) three dimensional array, then reading the array? There is no algorithm involved.