Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Loop

Loop

Scheduled Pinned Locked Moved Algorithms
comtoolshelpquestion
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MoustafaS
    wrote on last edited by
    #1

    I was asked to write the multiply table in c, output will be something like : 1 2 3 4 5 6 7 2 4 6 8 .. ....etc for 12, so I did it like this

    for (a=1;a<=12;a++)
    {
    for (b=1;b<=12;b++)
    printf("%d",a*b);
    printf("\n");
    }

    That works, but I was wondering if there exeist another solution to the same problem using only ONE loop ?


    About : Islam
    About : Me

    F 1 Reply Last reply
    0
    • M MoustafaS

      I was asked to write the multiply table in c, output will be something like : 1 2 3 4 5 6 7 2 4 6 8 .. ....etc for 12, so I did it like this

      for (a=1;a<=12;a++)
      {
      for (b=1;b<=12;b++)
      printf("%d",a*b);
      printf("\n");
      }

      That works, but I was wondering if there exeist another solution to the same problem using only ONE loop ?


      About : Islam
      About : Me

      F Offline
      F Offline
      Frank Kerrigan
      wrote on last edited by
      #2

      Quickly looking at this the closed solution formula is f(n) = 2^(n-1) so the loop would be for (a=1;a<=12;a++) { printf(2^(a-1)); }

      Grady Booch: I told Google to their face...what you need is some serious adult supervision. (2007 Turing lecture) http://www.frankkerrigan.com/[^]

      F

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups