This output using for loop?

Discussion in 'Programming' started by Codd, Nov 28, 2010.

  1. #1
    So I'm trying to get this output using a for loop.

    1 1
    2 1

    I just can't get a logic good enough.
    I need help. Anyone has any idea?
     
    Last edited: Nov 28, 2010
    Codd, Nov 28, 2010 IP
  2. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #2
    for ($i = 1; $i <= 2; $i++)
    {
        echo "$i 1<br>";
    }
    PHP:
     
    ActiveFrost, Nov 28, 2010 IP
  3. Codd

    Codd Member

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #3
    Thats good, but the 1 shouldn't be printed directly.
    It should be printed by some kind of logic.
     
    Codd, Nov 28, 2010 IP
  4. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #4
    Care to elaborate ? The given snippet produces exactly what you were asking for. If there are any other hidden rules, tell us about them first.
     
    ActiveFrost, Nov 28, 2010 IP
  5. Codd

    Codd Member

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #5
    The 1 shouldn't be printed directly through quotes.
    It should be printed by the logic of the for loop.

    For eg, you don't print the Fibonacci series by directly putting it into the quotes, do you? It is printed using a logic.
     
    Codd, Nov 28, 2010 IP
  6. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #6
    Your example has no logic at all so the question is .. what are you talking about ?
     
    ActiveFrost, Nov 28, 2010 IP
  7. Codd

    Codd Member

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #7
    I was asked this question to test my logic. But I couldn't answer it. :p

    The best I could get is this:
    for($i=1; $i<=2; $i++) {
       for($j=1; $j<=2; $j++) {
          echo ceil($i/$j)." ";
       }
       echo "\n";
    }
    PHP:
     
    Codd, Nov 28, 2010 IP