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. The Lounge
  3. Yet another fun C-puzzle

Yet another fun C-puzzle

Scheduled Pinned Locked Moved The Lounge
questionc++
32 Posts 14 Posters 0 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.
  • N Nish Nishant

    This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-

    static int i=0;

    class ad
    {
    ad()
    {
    i++;
    cout << i;
    }
    }

    void main()
    {
    ad objad[100];
    }

    I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)

    I Offline
    I Offline
    Imtiaz Murtaza
    wrote on last edited by
    #6

    Dunno whether this is acceptable or not ?

    static int number = -1;

    void PrintNumber()
    {
    (++number<101)?printf("%d\n",number):Exit(0);

    PrintNumber();
    }

    int main(int argc, char* argv[])
    {
    PrintNumber();

    return 0;
    

    }

    Imtiaz

    N 1 Reply Last reply
    0
    • N Nish Nishant

      This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-

      static int i=0;

      class ad
      {
      ad()
      {
      i++;
      cout << i;
      }
      }

      void main()
      {
      ad objad[100];
      }

      I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)

      R Offline
      R Offline
      Russell Morris
      wrote on last edited by
      #7

      Hmm...

      static int i=1;
      typedef void (*fn) ();
      fn pfn[2];

      void print_i(void)
      {
      printf("%d\n",i);
      // Not a conditional! :)
      // i++ / 100 will evaluate to 0 until i has reached 100
      (pfn[i++ / 100])();
      }

      void go_bye_bye()
      {
      exit(0);
      }

      void main(void)
      {
      pfn[0] = &print_i;
      pfn[1] = &go_bye_bye;

      (pfn\[0\])();
      

      }

      -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy

      N D R 3 Replies Last reply
      0
      • I Imtiaz Murtaza

        Dunno whether this is acceptable or not ?

        static int number = -1;

        void PrintNumber()
        {
        (++number<101)?printf("%d\n",number):Exit(0);

        PrintNumber();
        }

        int main(int argc, char* argv[])
        {
        PrintNumber();

        return 0;
        

        }

        Imtiaz

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #8

        Isn't the ? : block same as an if-else? Nish

        1 Reply Last reply
        0
        • R Russell Morris

          Hmm...

          static int i=1;
          typedef void (*fn) ();
          fn pfn[2];

          void print_i(void)
          {
          printf("%d\n",i);
          // Not a conditional! :)
          // i++ / 100 will evaluate to 0 until i has reached 100
          (pfn[i++ / 100])();
          }

          void go_bye_bye()
          {
          exit(0);
          }

          void main(void)
          {
          pfn[0] = &print_i;
          pfn[1] = &go_bye_bye;

          (pfn\[0\])();
          

          }

          -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #9

          Perfect Mr Russel Morris is the winner :-) BTW for the record, here's my code (very similar to Russel's as the basic concept of using func pointers is same)

          void Dummy(int)
          {
          }

          void Show(int i);

          typedef void (*FUNC)(int);
          FUNC pFunc[2];

          void Show(int i)
          {
          printf("%d\t",i);
          pFunc[i<100](i+1);
          }

          int main()
          {
          pFunc[0] = &Dummy;
          pFunc[1] = &Show;
          Show(1);
          }

          N 1 Reply Last reply
          0
          • N Nish Nishant

            Perfect Mr Russel Morris is the winner :-) BTW for the record, here's my code (very similar to Russel's as the basic concept of using func pointers is same)

            void Dummy(int)
            {
            }

            void Show(int i);

            typedef void (*FUNC)(int);
            FUNC pFunc[2];

            void Show(int i)
            {
            printf("%d\t",i);
            pFunc[i<100](i+1);
            }

            int main()
            {
            pFunc[0] = &Dummy;
            pFunc[1] = &Show;
            Show(1);
            }

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #10

            In fact Russel's solution is better than mine. I used the < operator whoich is a conditional expression (it's valid usage as the question only disallows conditional statements) but Russel uses division which is perfect :-) Congrats again Russel. I raise my hat to you :-)

            1 Reply Last reply
            0
            • R Russell Morris

              Hmm...

              static int i=1;
              typedef void (*fn) ();
              fn pfn[2];

              void print_i(void)
              {
              printf("%d\n",i);
              // Not a conditional! :)
              // i++ / 100 will evaluate to 0 until i has reached 100
              (pfn[i++ / 100])();
              }

              void go_bye_bye()
              {
              exit(0);
              }

              void main(void)
              {
              pfn[0] = &print_i;
              pfn[1] = &go_bye_bye;

              (pfn\[0\])();
              

              }

              -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy

              D Offline
              D Offline
              David Stone
              wrote on last edited by
              #11

              Very nice. :) I hadn't even considered using function pointers. I was talking to Nish and suggested one printf with 100 digits and 100 linebreaks. ;)


              [Cheshire] I can't afford those plastic things to cover the electric sockets so I just draw bunny faces on the electric outlets to scare the kids away from them... [RLtim] Newsflash! Kids aren't afraid of bunnies. [Cheshire] Oh they will be... -Bash.org

              N 1 Reply Last reply
              0
              • D David Stone

                Very nice. :) I hadn't even considered using function pointers. I was talking to Nish and suggested one printf with 100 digits and 100 linebreaks. ;)


                [Cheshire] I can't afford those plastic things to cover the electric sockets so I just draw bunny faces on the electric outlets to scare the kids away from them... [RLtim] Newsflash! Kids aren't afraid of bunnies. [Cheshire] Oh they will be... -Bash.org

                N Offline
                N Offline
                Nish Nishant
                wrote on last edited by
                #12

                David Stone wrote: I was talking to Nish and suggested one printf with 100 digits and 100 linebreaks. Guys, it's okay, David is from Southern California - so as I said, it's quite okay :rolleyes:

                1 Reply Last reply
                0
                • R Russell Morris

                  Hmm...

                  static int i=1;
                  typedef void (*fn) ();
                  fn pfn[2];

                  void print_i(void)
                  {
                  printf("%d\n",i);
                  // Not a conditional! :)
                  // i++ / 100 will evaluate to 0 until i has reached 100
                  (pfn[i++ / 100])();
                  }

                  void go_bye_bye()
                  {
                  exit(0);
                  }

                  void main(void)
                  {
                  pfn[0] = &print_i;
                  pfn[1] = &go_bye_bye;

                  (pfn\[0\])();
                  

                  }

                  -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy

                  R Offline
                  R Offline
                  Ryan Binns
                  wrote on last edited by
                  #13

                  Mine was pretty similar except it doesn't use globals and uses a double-not rather than / 100 :)

                  #include <stdlib.h>
                  #include <stdio.h>

                  typedef void (*funcptr)(int);

                  funcptr buf[2];

                  void count(int num)
                  {
                  printf("%d\n", 100-num);
                  (*buf[!!num])(num-1);
                  }

                  int main()
                  {
                  buf[0] = exit;
                  buf[1] = count;
                  count(99);
                  }

                  Ryan

                  "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                  N 1 Reply Last reply
                  0
                  • C Chris Maunder

                    Can we print more than 100? Does the program have to terminate elegantly? I came up with a quick solution but it ain't exactly pretty :D cheers, Chris Maunder

                    N Offline
                    N Offline
                    Nish Nishant
                    wrote on last edited by
                    #14

                    Chris Maunder wrote: Can we print more than 100? Does the program have to terminate elegantly? I came up with a quick solution but it ain't exactly pretty :-D

                    1 Reply Last reply
                    0
                    • R Ryan Binns

                      Mine was pretty similar except it doesn't use globals and uses a double-not rather than / 100 :)

                      #include <stdlib.h>
                      #include <stdio.h>

                      typedef void (*funcptr)(int);

                      funcptr buf[2];

                      void count(int num)
                      {
                      printf("%d\n", 100-num);
                      (*buf[!!num])(num-1);
                      }

                      int main()
                      {
                      buf[0] = exit;
                      buf[1] = count;
                      count(99);
                      }

                      Ryan

                      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                      N Offline
                      N Offline
                      Nish Nishant
                      wrote on last edited by
                      #15

                      *Applause* The double not was pretty cool - more elegant than Russel's division - and a lot more neater than my comparision operator :-) Nish

                      N 1 Reply Last reply
                      0
                      • N Nish Nishant

                        *Applause* The double not was pretty cool - more elegant than Russel's division - and a lot more neater than my comparision operator :-) Nish

                        N Offline
                        N Offline
                        Nish Nishant
                        wrote on last edited by
                        #16

                        Ryan Russel still wins though - 1) he posted it first 2) he uses division which while inelegant is not a conditional expression, your double not (while elegant) is still a conditional expression (valid as the question only disallows conditional statements and expressions are not statements) Nish

                        1 Reply Last reply
                        0
                        • N Nish Nishant

                          This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-

                          static int i=0;

                          class ad
                          {
                          ad()
                          {
                          i++;
                          cout << i;
                          }
                          }

                          void main()
                          {
                          ad objad[100];
                          }

                          I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)

                          V Offline
                          V Offline
                          Vivek Rajan
                          wrote on last edited by
                          #17

                          How about this int main(int ac, char ** av) { static int i=1; printf("%d ",i++); i%101&&main(0,0); }

                          N 1 Reply Last reply
                          0
                          • V Vivek Rajan

                            How about this int main(int ac, char ** av) { static int i=1; printf("%d ",i++); i%101&&main(0,0); }

                            N Offline
                            N Offline
                            Nish Nishant
                            wrote on last edited by
                            #18

                            Cool stuff, Vivek :-) Nish

                            N 1 Reply Last reply
                            0
                            • N Nish Nishant

                              Cool stuff, Vivek :-) Nish

                              N Offline
                              N Offline
                              Nish Nishant
                              wrote on last edited by
                              #19

                              Here's an improvement (LOC reduced anyway)

                              int main(int ac, char ** av)
                              {
                              printf("%d ",101-ac,(ac%100)&&main(ac+1,0));
                              }

                              Run the app without arguments (first time ac will be 1) Nish

                              A 1 Reply Last reply
                              0
                              • N Nish Nishant

                                BTW 100 printfs don't count :rolleyes:

                                G Offline
                                G Offline
                                GizzoF
                                wrote on last edited by
                                #20

                                Nishant S wrote: BTW 100 printfs don't count why not? It is a solution, isn't it?

                                1 Reply Last reply
                                0
                                • N Nish Nishant

                                  This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-

                                  static int i=0;

                                  class ad
                                  {
                                  ad()
                                  {
                                  i++;
                                  cout << i;
                                  }
                                  }

                                  void main()
                                  {
                                  ad objad[100];
                                  }

                                  I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)

                                  Z Offline
                                  Z Offline
                                  Zdeslav Vojkovic
                                  wrote on last edited by
                                  #21

                                  template <long val> void printfn() { std::cout << val << "\n"; printfn<val + 1>(); } template <> void printfn<100L>() { std::cout << "100\n"; } int main(int argc, char* argv[]) { printfn<1L>(); system("PAUSE"); return 0; } this works fine in VS.NET 2003 and gcc, but not with VC++ 6.0 which generates wrong specialization. i suppose that it also faster then previous solutions, since it is resolved in compile time.

                                  N 1 Reply Last reply
                                  0
                                  • Z Zdeslav Vojkovic

                                    template <long val> void printfn() { std::cout << val << "\n"; printfn<val + 1>(); } template <> void printfn<100L>() { std::cout << "100\n"; } int main(int argc, char* argv[]) { printfn<1L>(); system("PAUSE"); return 0; } this works fine in VS.NET 2003 and gcc, but not with VC++ 6.0 which generates wrong specialization. i suppose that it also faster then previous solutions, since it is resolved in compile time.

                                    N Offline
                                    N Offline
                                    Nish Nishant
                                    wrote on last edited by
                                    #22

                                    Good code, but C does not support templates :-) Nish

                                    Z 1 Reply Last reply
                                    0
                                    • N Nish Nishant

                                      Good code, but C does not support templates :-) Nish

                                      Z Offline
                                      Z Offline
                                      Zdeslav Vojkovic
                                      wrote on last edited by
                                      #23

                                      :-O i got carried away by this line from your original post: "An answer was later posted in C++ (and the poster said that it was not possible without C++)."

                                      1 Reply Last reply
                                      0
                                      • N Nish Nishant

                                        This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-

                                        static int i=0;

                                        class ad
                                        {
                                        ad()
                                        {
                                        i++;
                                        cout << i;
                                        }
                                        }

                                        void main()
                                        {
                                        ad objad[100];
                                        }

                                        I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)

                                        U Offline
                                        U Offline
                                        utsav_verma
                                        wrote on last edited by
                                        #24

                                        assuming that if also doesnt matter n yeah also mind that i m writing code directly here, neither compiled nor checked for output - int main(int x) { printf("%d",x); if(x<100) main(++x); } program is expected to run from command prompt as filename 0 what do u think?? UTSAV MCA final yr

                                        N 2 Replies Last reply
                                        0
                                        • N Nish Nishant

                                          This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-

                                          static int i=0;

                                          class ad
                                          {
                                          ad()
                                          {
                                          i++;
                                          cout << i;
                                          }
                                          }

                                          void main()
                                          {
                                          ad objad[100];
                                          }

                                          I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)

                                          J Offline
                                          J Offline
                                          Jason Pease
                                          wrote on last edited by
                                          #25

                                          Does this count? void main(){ char buffer[8] strcpy(buffer,"1-100"); cout << buffer << endl; } Jason

                                          N 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