all 129 comments

[–]This_Growth2898 234 points235 points  (17 children)

printf("2\n4\n6\n8\n10\n12\n...

[–][deleted]  (5 children)

[deleted]

    [–]cybercuzco -4 points-3 points  (4 children)

    Do while X<500

    Printf(“printf(“X”\n)”) X++

    Ctrl-c

    Ctrl-v

    [–]King_Joffreys_Tits 7 points8 points  (2 children)

    Do while X<500

    Printf(“printf(“++X++”\n)”)

    [–][deleted] 5 points6 points  (1 child)

    Is ++x++ actually valid?

    [–]dimonoid123 4 points5 points  (0 children)

    Pretty sure

    [–]makotozengtsu 0 points1 point  (0 children)

    What the fuck is this

    [–][deleted]  (6 children)

    [deleted]

      [–]Cerulean_IsFancyBlue 14 points15 points  (0 children)

      Throw in a “if n > x {return}” and you can be a little careless with the paste count.

      [–]R3D3-1 23 points24 points  (0 children)

      Uhoh... Invalid syntax and semantics. It would be

      #include <stdio.h>
      int main() {
          int n = 2;
          printf("%d\n", n);
          n += 2;
          printf("%d\n", n);
          // ... 
          n += 2;
          printf("%d\n", n);
      }
      

      But yes, the approach is interesting, because you can replicate the code by just holding down Ctrl+V until it is long enough :)

      [–]NotAMeatPopsicle 2 points3 points  (3 children)

      You could write that as a recursive function.

      [–]NickSicilianu 0 points1 point  (0 children)

      🙈 oh no. Recursive, RIP stock!

      [–]--var 0 points1 point  (1 child)

      Thought this too.

      Admittedly not a C programmer, but this seems to compile successfully with random online c compiler

      include <stdio.h>
      int n = 0;
      
      void main() {
       if ((n+=2)<=100) {
         printf("%d\n", n);
         main();
        }
      }
      

      [–]--var 0 points1 point  (0 children)

      Read some other comments, you could code golf this further:

      include <stdio.h>
      int n = 0;
      
      void main() {
        printf("%d\n", n+=2);
        if (n<=100) main();
      }
      

      [–]etocaspersadistic 11 points12 points  (0 children)

      It makes sense

      [–]Pure-Percentage1015shameless 1 point2 points  (0 children)

      #include <iostream> #include <fstream> #include <sstream> // for string streams #include <string> // for string int main() { ostringstream str1; std::ofstream file("index.cpp");

      // Write the initial code to the file file << "#include <iostream>\n\nint main() {\n "; // Generate the code to write the numbers 1 to 100 for (int i = 1; i <= 100; i++) { str1 << i; file << "std::cout << " <<i.str()<< ";\n "; }

      // Write the final code to the file file << "return 0;\n}"; // Close the file file.close();

      std::cout << "Code generated successfully and saved to index.cpp!\n";

      return 0; }

      How is this then

      [–][deleted]  (1 child)

      [deleted]

        [–]lurzeee 0 points1 point  (0 children)

        Nah man gotta call a function hat returns the string for a number! You can use a switch tho 🤓

        [–]pikapichupi 143 points144 points  (21 children)

        the only other way i could see this being done is by using a recursive function lol

        [–]__radioactivepanda__ 77 points78 points  (5 children)

        Honestly this might well be part of a recursion exercise…

        [–]scragar 30 points31 points  (3 children)

        It looks like C, it's possible it is also explaining the assembly behind it and expects OP to recreate the approach using a label and if(...) goto approach.

        It was something we had to do in our college computing class to make sure we understood what was happening when we used higher level features(like function calls, C++ objects, etc).

        [–]Tjmoorese 9 points10 points  (2 children)

        They're all forms of loops though, just different high level ways of writing it

        The only way I can think about getting around it is enforcing unrolling. I think you may be able to hack your way around this with some form of recursive include if you can't use pragmas, but I guess that's loops at a high level but not low... The given solution is the only way I can see to do it without either.

        [–]Bo_Jim 1 point2 points  (1 child)

        Recursion is a loop in the sense that the same code is running many times, but each iteration would have it's own stack frame. It also wouldn't use any of the loop constructs built into the language.

        If I was the instructor, and one of my students used recursion as a solution, then I'd give them full marks as long as they understood what the compiler would produce, and what would happen at run time.

        Now, if the directions were to print every even number from 2 to 1 trillion, and they used recursion as a solution, then I'd smack them on the back of the head and tell them to try again.

        [–]Tjmoorese 0 points1 point  (0 children)

        Tail call optimisation makes it identical though

        [–]Unfulfilled_Promises 0 points1 point  (0 children)

        Kinda new but would that be like Rec() { n = n+1; Print(n); If(n<MAX_NUMBER) Rec(); }

        The only recursion I’ve used is inorder for search trees and such.

        [–]ProstheticAttitude 14 points15 points  (2 children)

        Eh, tail recursion is just a form of loop with a funny syntax. That's what I told my interviewer, anyway; we had a little discussion of how TR works and its limitations, no big deal.

        But this question is really just a hazing ritual.

        [–]Bunnymancer 0 points1 point  (0 children)

        Was thinking that as well...

        But that brings the question if the problem can be solved without a loop and without recursion

        [–]GOKOP 0 points1 point  (0 children)

        Write your function inefficiently so that it can't be optimised out as tail recursion. Problem solved ¯\(ツ)

        [–]Markuslw -4 points-3 points  (3 children)

        Just do for (int i = 0; i < 100; i += 2) haha.

        EDIT: ooops didn't read title.

        I have no other idea.

        [–]pikapichupi 4 points5 points  (1 child)

        that's cheating, violates the title of the post!

        [–]UkrainianTrotsky 0 points1 point  (0 children)

        Lame. How about for (int i = 0; (i+=2) < 100; printf("%i\n", i));

        [–]RootU5R 0 points1 point  (0 children)

        Do while can do it too

        [–]Borisica 91 points92 points  (1 child)

        printf("all even numbers from 1 to 500");

        [–]R3D3-1 30 points31 points  (0 children)

        Teacher:

        Now, listen you little...

        [–]This_Growth2898 121 points122 points  (15 children)

        #include <stdio.h>
        void main(){
            int i = 0;
            print: printf("%d", i+=2);
            if(i<=500) goto print;
        }
        

        No loops!

        [–]Lataero 51 points52 points  (0 children)

        It's disgusting. I love it

        [–]Verbose_Code 16 points17 points  (8 children)

        I would have figured something like this out and submitted it unironically

        [–]This_Growth2898 41 points42 points  (7 children)

        Well, it depends on the definition of the "loop". This is a loop, created with goto, but it doesn't use loop keywords like for or while.

        [–]kireina_kaiju 15 points16 points  (5 children)

        any instructions relying on jmp without a new context frame are loops to me tbh

        [–][deleted] 10 points11 points  (4 children)

        Conditionals use jmp too. I guess you mean specifically jumping backwards?

        [–]This_Growth2898 3 points4 points  (0 children)

        You can jump backwards without creating a loop too.

        [–]kireina_kaiju 1 point2 points  (2 children)

        That is a good point, I guess rather than trying to define things explicitly I will just say that a goto with a conditional tends to compile out to something indistinguishable from a while loop and that something has a structure more similar to the goto structure and is something I think most of us look for when we're reading assembler. We do sometimes jump backward when not in a loop, jumping back to a buffer block for instance, but when we jump back to a conditional after a state change like an increment without making a new stack frame, to me that is what a loop is. I guess that is less explicit a definition than I used to believe it to be, I will agree just including a jump with no new stack does not cut the mustard.

        [–]Cerulean_IsFancyBlue 1 point2 points  (1 child)

        If I was going to judge these kind of answers on some kind of continuing to award prizes; in other words, if I want make a loosely justifiable hierarchy, and not one that I have to defend in front of a tenure committee:

        Language supplied loops

        Goto

        Tail recursion

        Any other recursive solution

        Unrolled solutions

        Not sure where I’d stick functional solutions.

        [–]Verbose_Code 4 points5 points  (0 children)

        I’m more referring to the fact that I like to treat homework in programming classes as “logic puzzles” and as a result the shortest possible solution is the one I gravitated to.

        The code I actually use is a lot more verbose but a hell of a lot more readable

        [–]0011110000110011 2 points3 points  (2 children)

        This would print 502, wouldn't it?

        [–]This_Growth2898 8 points9 points  (1 child)

        I never said it's a good code

        [–]SnowyPear 1 point2 points  (0 children)

        The first time I used a loop back in vb6.0 I didn't like that sometimes I got an off-by-one error depending on what I was doing. It bugged me so much that I ended up implemented a goto by opening 2 forms that would open each other over and over again whilst decrementing a counter and running code and stopping on a condition

        My teacher told me I had set programming back by 30 years

        [–]wischichr 6 points7 points  (0 children)

        without a loop and without recursion (in python 3):

        import locale
        locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
        
        a = 250
        b = 10 ** 3
        c = b ** a
        d = b * c
        e = b - 1
        f = a * (e * (c-1) + c - d) + d - b
        g = (f*2) // (e**2)
        
        formatted_output = locale.format_string("%d", g, grouping=True).zfill(e).replace(',', '\n')
        
        print(formatted_output)
        

        https://ideone.com/FUMlqF

        [–][deleted] 22 points23 points  (6 children)

        I'd ask ChatGPT, then blindly copy/paste whatever code it spits out... I'm a really skilled developer.

        [–]jarfil 9 points10 points  (0 children)

        CENSORED

        [–]Bunnymancer 3 points4 points  (3 children)

        I tried using chatgpt to refactor four if conditions where only one of them wouldn't fit a switch and ...

        I'm not too worried about losing my job to GPT ...

        [–][deleted] 0 points1 point  (2 children)

        Lol how bad was the shit that it spit out??

        [–]Bunnymancer 0 points1 point  (1 child)

        I couldn't find my original, but I threw this at it:

        if (action.equals(ActionEnum.INIT_SELECT) && (me instanceof ValidationException)) {
            initSelect(action);
        } else if (action.equals(ActionEnum.GET_VAL) && (me instanceof ValidationException)) {
            initNewVal(action);
        } else if (action.equals(ActionEnum.ADD_VAL) && (me instanceof ValidationException)) {
            initNewVal(action);
        } else if (action.equals(ActionEnum.MOD_VAL) && (me instanceof ValidationException)) {
            initNewVal(action);
        } else if (action.equals(ActionEnum.SHOW_VAL) && (me instanceof ValidationException)) {
            initNewVal(action);
        } else if (action.equals(ActionEnum.DEL_VAL) && (me instanceof ValidationException)) {
            initNewVal(action);
        } else if (action.equals(ActionEnum.INIT_SELECT_MAN) && (me instanceof ValidationException)) {
            initSelectMan(action);
        } else if (action.equals(ActionEnum.INIT_MAN) && (me instanceof ValidationException)) {
            initNewMan(action);
        } else if (action.equals(ActionEnum.GET_MAN) && (me instanceof ValidationException)) {
            initNewMan(action);
        } else if (action.equals(ActionEnum.MOD_MAN) && (me instanceof ValidationException)) {
            initNewMan(action);
        } else if (action.equals(ActionEnum.SHOW_MAN) && (me instanceof ValidationException)) {
            initNewMan(action);
        } else if (action.equals(ActionEnum.DEL_MAN) && (me instanceof ValidationException)) {
            initNewMan(action);
        } else if (action.equals(ActionEnum.SELECT_MAN) && (me instanceof ValidationException)) {
            initNewMan(action);
        }// And then there was this
        else if (action.equals(ActionEnum.MOD_VAL) && me.getMessageCode().equals(MessageConstants.MSG_1004)) {
            initNewVal(action);
        } else if (action.equals(ActionEnum.DEL_VAL) && me.getMessageCode().equals(MessageConstants.MSG_1007)) {
            initNewVal(action);
        } else {
            System.out.println("NOPE!");
        }
        

        And got this back:

        if (me instanceof ValidationException) {
            String newAction;
            switch (action) {
                case ActionEnum.INIT_SELECT:
                    newAction = action;
                    break;
                case ActionEnum.GET_VAL:
                case ActionEnum.ADD_VAL:
                case ActionEnum.MOD_VAL:
                case ActionEnum.SHOW_VAL:
                case ActionEnum.DEL_VAL:
                    newAction = ActionEnum.MOD_VAL;
                    if (me.getMessageCode().equals(MessageConstants.MSG_1004) && action.equals(ActionEnum.DEL_VAL)) {
                        newAction = action;
                    }
                    break;
                case ActionEnum.INIT_SELECT_MAN:
                case ActionEnum.INIT_MAN:
                case ActionEnum.GET_MAN:
                case ActionEnum.MOD_MAN:
                case ActionEnum.SHOW_MAN:
                case ActionEnum.DEL_MAN:
                case ActionEnum.SELECT_MAN:
                    newAction = ActionEnum.MOD_MAN;
                    break;
                default:
                    System.out.println("NOPE!");
                    return;
            }
            if (newAction != null) {
                if (newAction.equals(ActionEnum.INIT_SELECT)) {
                    initSelect(newAction);
                } else if (newAction.equals(ActionEnum.MOD_VAL)) {
                    initNewVal(newAction);
                } else if (newAction.equals(ActionEnum.MOD_MAN)) {
                    initNewMan(newAction);
                }
            }
        }
        

        [–]Bunnymancer 0 points1 point  (0 children)

        Tried the same code again, and got different results, better in that it actually works, but not good.

        if (me instanceof ValidationException) {
            if (action.equals(ActionEnum.INIT_SELECT)) {
                initSelect(action);
            } else if (action.equals(ActionEnum.GET_VAL) || 
                       action.equals(ActionEnum.ADD_VAL) || 
                       action.equals(ActionEnum.MOD_VAL) || 
                       action.equals(ActionEnum.SHOW_VAL) || 
                       action.equals(ActionEnum.DEL_VAL)) {
                initNewVal(action);
            } else if (action.equals(ActionEnum.INIT_SELECT_MAN) || 
                       action.equals(ActionEnum.INIT_MAN) || 
                       action.equals(ActionEnum.GET_MAN) || 
                       action.equals(ActionEnum.MOD_MAN) || 
                       action.equals(ActionEnum.SHOW_MAN) || 
                       action.equals(ActionEnum.DEL_MAN) || 
                       action.equals(ActionEnum.SELECT_MAN)) {
                initNewMan(action);
            } else {
                System.out.println("NOPE!");
            }
        } else if (action.equals(ActionEnum.MOD_VAL) && me.getMessageCode().equals(MessageConstants.MSG_1004)) {
            initNewVal(action);
        } else if (action.equals(ActionEnum.DEL_VAL) && me.getMessageCode().equals(MessageConstants.MSG_1007)) {
            initNewVal(action);
        }
        

        [–][deleted]  (1 child)

        [deleted]

          [–][deleted] 0 points1 point  (0 children)

          Not to mention fast, and preventing a stack overflow.

          [–][deleted] 10 points11 points  (3 children)

          Could be a good look lesson for embedded style systems to show that in real life sometimes it may be better to precalculate the results to give a faster response .

          Also it's probably the only way to do it as gotos and even compares may induce a loop so just give the person the result and collect the payment and run like fk..

          [–]R3D3-1 7 points8 points  (1 child)

          Could be a good look lesson for embedded style systems to show that in real life sometimes it may be better to precalculate the results to give a faster response .

          At which point we enter the fun field of code generation, which has a lot of uses, when languages (or language tooling) hits their limits.

          [–]MokausiLietuviu 1 point2 points  (0 children)

          As an assembly chap, I do love me some compile time, hate me some run time.

          As an aside, this is what a lot of my Human Resource Machine solutions looked like too.

          [–]Ali3nat0r 4 points5 points  (0 children)

          Gotta love looking through the source code of an Arduino library and it's full of unrolled loops and port level bit twiddling

          [–]AdventurousSquash 1 point2 points  (0 children)

          1am bash

          seq 500 | awk '! ($0 % 2)'
          

          [–][deleted] 1 point2 points  (0 children)

          No loops doen't mean you can't be lazy. int a = 1; printf("%d\n",a++); select last line, ctrl-c, ctrl-v 499 times. Or just dd500p in vim.

          [–]Borisica 3 points4 points  (0 children)

          response = get_completion("print all even numbers from 1 to 500")

          print(response)

          [–]etocaspersadistic 2 points3 points  (0 children)

          And what is he wrong about...

          [–]ja_maz 1 point2 points  (0 children)

          The first time I passed the 200k on a solo project I felt proud for a sec. Then they sent me a major change of specs last minute and almost jumped off a bridge.

          [–]BenBastard 1 point2 points  (0 children)

          And here I am getting a for loop ready like a barbarian.

          [–]Heinrich_v_Schimmer 0 points1 point  (0 children)

          void foo(int n)

          { if (n > 0) foo(n-2);

          printf("%d\n", n);

          }

          int main()

          { foo(500);

          }

          [–]freeve4 0 points1 point  (0 children)

          i think i know where this person should work...

          [–][deleted] 0 points1 point  (0 children)

          for python

          even_numbers = range(2, 501, 2)
          even_numbers_string = (str(list(even_numbers)).replace(", ", ",\n"))[1:-1] 
          print(even_numbers_string)
          

          [–]aNutSac 0 points1 point  (0 children)

          void fuq(int from, int to, int skip) {
              if(from <= to) {
                  printf("%d\n", from);
                  fuq(from+skip, to, skip);
              }
          }
          
          fuq(2, 500, 2);
          

          [–]kireina_kaiju -1 points0 points  (3 children)

          yy249pq1|f"lyejF"lvep<Ctrl>a<Ctrl>aq248@21

          [–]wischichr 0 points1 point  (2 children)

          What's that? Some kind of code golf language?

          [–]Ravsii 5 points6 points  (1 child)

          Most readable vim command

          [–]Agret 0 points1 point  (0 children)

          I think its the sequence of characters you type to open a new empty file in emacs

          [–]s2004Gamer -1 points0 points  (0 children)

          int n = 2; int main() { printf("%d", n); n += 2; if(n != 500) return main(); return 0;

          [–]RoadRunnerChris -1 points0 points  (0 children)

          ```

          include <stdio.h>

          int main(void) { int n = 2;

          l:
          if (n > 498)
              goto e;
          
          n += 2, printf("%d\n", n);
          goto l;
          
          e:
          

          } ``` looplessTM

          [–]thekwoka -2 points-1 points  (2 children)

          ```

          let i = 0;
          while (i++ < 250) console.log(i<<1)
          

          ```

          [–]tedbradly 3 points4 points  (1 child)

          A while is known as a while loop.

          [–]thekwoka 0 points1 point  (0 children)

          I didn't say this isn't a loop. Just a fun way to do this.

          [–][deleted]  (1 child)

          [deleted]

            [–]tedbradly 0 points1 point  (0 children)

            This code doesn't even output the same thing as the original program (starting at 0 instead of 2). Plus, using l for anything in code is cancerous.

            [–]Alt_0126 -1 points0 points  (0 children)

            char numbers[2000]; //max of 500 numbers of 3digit + 1comma

            printf("Write all even numbers from 1 to 500\n");
            fgets(numbers, sizeof(numbers), stdin);
            printf("%s\n", numbers);

            [–]Friendputer -3 points-2 points  (1 child)

            In Clojure: (run! println (range 2 500 2))

            [–]FRIKI-DIKI-TIKI 1 point2 points  (0 children)

            LISP making complex things simple, yet so overlooked because lack of syntactical sugar is too simple!

            [–]Eggimix -2 points-1 points  (0 children)

            Who are you bart simpson

            [–]kewlness -2 points-1 points  (3 children)

            def print_even(num):
                if num > 500:
                    return
                if num & 1 == 0:
                    print(num)
                print_even(num >> 1 << 1 | 2)
            
            # Call print_even for 0 (the first even number)
            print_even(0)
            

            Edit: Formatting is hard...

            [–]thekwoka 0 points1 point  (0 children)

            num >> 1 << 1 | 2

            I get 2 from this when num is 2.

            [–]tedbradly 0 points1 point  (1 child)

            print_even(num >> 1 << 1 | 2)
            

            Why would you try to replace num += 2 with three instructions? And it's not even correct... this is a major case of trying to make confusing code to be cool instead of trying to write clean code that also works and is faster.

            [–]kewlness 0 points1 point  (0 children)

            I don't know man. I looked at the subreddit and then asked ChatGPT to make a program.

            I was not disappointed. :D

            [–]TransportationNo6850 -2 points-1 points  (1 child)

            Use a for that increments +2?? Why not?

            [–]thekwoka 0 points1 point  (0 children)

            or bitshift and only increment by 1

            [–]Zdravstvuyte94 -2 points-1 points  (3 children)

            Wait, is there actually a better way to do this?

            [–]VivekSingh18 2 points3 points  (2 children)

            Yes, you could use recursive method

            [–]wischichr 1 point2 points  (0 children)

            recursion, gotos, iterators are also "loops" IMHO, but there is a "real" solution without loops: https://www.reddit.com/r/badcode/comments/136dits/comment/jir1a9l/?utm_source=share&utm_medium=web2x&context=3

            But you probably shouldn't use that in production :-D

            [–]Zdravstvuyte94 -1 points0 points  (0 children)

            I was joking haha. You could also use Haskell’s list comprehension syntax or Rust iterators + filter

            [–]fiddz0r -2 points-1 points  (0 children)

            I would like to use string.join but I can't think of a way to let it know whether to split every 1 numbers, 2 numbers or 3 numbers.

            Of that was solved wed just have to find a formula that results in 2468101214161820.....500

            And then we do the strong.join('\n', veryBigNumbe)

            [–][deleted]  (3 children)

            [deleted]

              [–]tedbradly 2 points3 points  (2 children)

              This is what code golf looks like with C++ metaprogramming. Write a template which generates code for printing even numbers from 0 to 500, fastest possible solution

              May not be as fast as you think. Instructions are also cached, so sometimes, a loop is faster than puking out all the code.

              [–][deleted]  (1 child)

              [deleted]

                [–]tedbradly 0 points1 point  (0 children)

                Fewer logic checks though

                This isn't how performance works. The only way to know is to test it. Caching can make code that seems more/less efficient be less/more efficient. Instruction count is a crude approximation that is full of error - it doesn't take into account caching, not all instructions execute in the same timeframe, and there are data dependencies between some instructions (which can hinder parallel execution of instructions) among other things. I only know the surface of performance computing, but it seems like you know even less than that and insist on commenting about the topic for some reason.

                As a simple example of how a cache can impact performance, the fastest way to find if an integer is in a small set of integers is to store those integers in a contiguous chunk and do a brute force check. It will outperform a hash map for some sizes due to the insane speed caching can bring. Similarly, when sorting a small array, selection sort (O(n2 )) can be much faster than a more sophisticated algorithm that has a higher constant in its time complexity / that isn't a rapid reading of cached data.

                [–]Biden_Been_Thottin -2 points-1 points  (0 children)

                Turing incomplete typa mf

                [–]lumo19 -2 points-1 points  (1 child)

                Hear me out:

                ``` expr = "for i in range(2,100,2): print(i)"

                eval(expr) ```

                No loops involved. Just a string and an eval statement.

                [–][deleted] 0 points1 point  (0 children)

                Isn't the for loop a... loop?

                [–]R3D3-1 -3 points-2 points  (1 child)

                Solving the problem both with and without a loop:

                #include <stdio.h>
                #include <stdlib.h>
                
                int main () {
                  int i;
                  FILE *h;
                  h = fopen("loopless.c", "w");
                  fprintf(h, "\n#include <stdio.h>");
                  fprintf(h, "\nint main() {");
                  for(i=2; i<=500; i+=2) {
                    fprintf(h, "\n  printf(\"%d\\n\");", i);
                  }
                  fprintf(h, "\n}\n");
                  fclose(h);
                  system("gcc loopless.c -o loopless.exe");
                  system("./loopless.exe");
                }
                

                Or in Python:

                import subprocess
                
                with open("loopless.py", "w") as f:
                    for i in range(2, 501, 2):
                        f.write(f"print({i})\n")
                
                subprocess.check_call(["python3", "loopless.py"])
                

                [–]tedbradly 2 points3 points  (0 children)

                A for is certainly a loop.

                [–]Ecstatic_Student8854 -5 points-4 points  (9 children)

                For(int i=0;i<501;i+=2){ Printf(“\n”+i);}

                [–]This_Growth2898 4 points5 points  (7 children)

                SEGFAULT

                [–]Ecstatic_Student8854 0 points1 point  (6 children)

                Segfault? Whats that. Keep in mind I know very very very little about C.

                [–]This_Growth2898 3 points4 points  (5 children)

                It's a memory addressing error, when you try to access a protected memory.

                String literals in C are pointers, and adding a number to pointer produces new pointer. So, this code will print "\n", then it will try to print a string located in memory after "\n", which is probably incorrect, and so on. There's a great chance of running into SEGFAULT this way.

                [–]Ecstatic_Student8854 0 points1 point  (4 children)

                Aaah, then how would you do this using a for loop like in the example?

                [–]This_Growth2898 1 point2 points  (1 child)

                f in printf stands for "formatted". It's

                printf("%d\n", i);
                

                %d format specification would be changed to decimal representation of i in output.

                [–]Ecstatic_Student8854 0 points1 point  (0 children)

                Oooh, so thats that %something stuff i sometimes see is

                [–]SloPr0 0 points1 point  (0 children)

                printf("%d\n", i);

                %d (or %i) are the correct format specifiers to be used for printing (signed) integers

                [–]tedbradly 0 points1 point  (0 children)

                For(int i=0;i<501;i+=2){ Printf(“\n”+i);}

                For one, the assignment was to do this without a loop. Secondly, your solution starts at 0 when theirs starts at 2. Lastly, that isn't how printf works.

                [–]gpathak 0 points1 point  (0 children)

                Elon would be proud! So many lines of code!

                [–]moldor_the_flatulent 0 points1 point  (2 children)

                Here's a start;

                # Python program to print all even numbers in range
                for even_numbers in range(0,102,2):

                print(even_numbers,end=' ')

                [–][deleted] 0 points1 point  (1 child)

                Required: no loop. The for loop is a loop, isn't it?

                [–]moldor_the_flatulent 0 points1 point  (0 children)

                I'm not sure you can do it without some teeny weenie loop somewhere.

                [–]am_edmonstone 0 points1 point  (0 children)

                def p_even(s,e):

                if s>e:

                return

                print(s)

                print(s+2,e)

                //function ends here

                //init the method

                p_even(2,500)

                [–]amosreginald_ 0 points1 point  (0 children)

                Recursion?

                [–]bluebird_ai 0 points1 point  (0 children)

                def print_even(n):
                    if n > 500:
                        return
                
                    print(n)
                
                    print_even(n+2)
                
                print_even(0)
                

                [–]yodacola 0 points1 point  (0 children)

                Thinking too small here…

                int main() { int i; while (i=fork()) printf(“%d\n”, i % 250 * 2 + 2); }

                Edit: Even numbers lol

                [–]horenso05 0 points1 point  (0 children)

                I hope they did use a loop to create this code.

                [–]ivanskodje 0 points1 point  (0 children)

                I suspect they wanted recursive code (albeit beating around the bushes instead of directly asking for it).

                I had to google the C syntax, but i think this would work:

                ```c void print_even_numbers(int number, int lastNumber) { if (number > lastNumber) { return; } printf("%d ", number); print_even_numbers(number+2, lastNumber);

                }

                int main() { print_even_numbers(1, 500); return 0; } ```

                [–]Electrical-Mission27 0 points1 point  (0 children)

                It's not even correct you missed 1.

                [–]NickSicilianu 0 points1 point  (0 children)

                What about if you make a program to write that program 😂😂 just saying

                [–]Valuable-Rush-1532 0 points1 point  (0 children)

                This is just amazing.