how to write a program to get next odd number memory (limit 400 bytes)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aaasss
    New Member
    • Aug 2010
    • 4

    #1

    how to write a program to get next odd number memory (limit 400 bytes)

    i tried this code but it is more than 400 bytes

    #include<stdio. h>
    int main()
    {
    int x[4],i;
    for(i=0;i<4;i++ )
    {
    scanf("%d",&x[i]);//to get all numbers from user
    }
    for(i=0;i<4;i++ )
    {
    if(x[i]%2==0)//to get the next odd number
    x[i]++;
    }
    for(i=0;i<4;i++ )
    {
    printf("%d\n",x[i]);//to print odd numbers
    }
    return 0;
    }
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    It's 280 bytes even with comments.

    Comment

    • aaasss
      New Member
      • Aug 2010
      • 4

      #3
      and if the memory limit (100 bytes) how can i solve this problem???
      please help me!!!

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        Can you describe the problem somewhat more detailed than "get next odd number memory" ? At least what user is expected to enter and what is supposed to be output.

        Comment

        • aaasss
          New Member
          • Aug 2010
          • 4

          #5
          Given an integer 1 <= N <= 10000,you have to print the next odd number.



          Input
          The input file consists of series of numbers one in each line.The input is terminated by EOF.

          Output
          The next odd number.

          Example
          Input:
          5
          2
          6
          17

          Output:
          7
          3
          7
          19



          Take care about your fingers, do not use more than 100 bytes of code

          Comment

          • aaasss
            New Member
            • Aug 2010
            • 4

            #6
            this is the problem and i hope that you help me solve it

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              OK, well here are some tips about saving space:
              1. The 3 loops could be merged, doing all of the task in just one loop.
              2. Instead of that if-clause, you could use shorter syntax. Let me give you an example:
              Code:
              int a = 3;
              int b = a<10 ? 1 : 2; // this means: if a<10 is true, then the expression in brackets will result in 1, otherwise in 2
              So, if you adapt that, you can save some space.

              Oh, and the tasks tells you that the series of numbers is terminated by EOF, not that you have exactly 4 numbers. So read them in one by one and operate on them or take them as arguments for the program.

              One more thing: If I enter 3, should it give me 3 (which is an odd number) or 5 (which is the next highest odd number)?

              Greetings,
              Nepomuk

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Maybe I'm missing something, but printing out the odd numbers is:

                Code:
                printf("%d", x[i]);
                where (x[i] % 2) != 0.

                Put this in a loop controlled by i and you are done.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Are negative numbers allowed in the input file?
                  What is the next odd number for -5 (is it -3 or -7)?

                  Comment

                  • johny10151981
                    Top Contributor
                    • Jan 2010
                    • 1059

                    #10
                    try this
                    Code:
                    next_odd=current_numb+(current_numb%2)+1;
                    It will always give you the next odd number.....

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #11
                      Prior to C99, the behavior of the "%" operator was implementation-dependent when either operand was negative. A portable solution ought to work around this limitation (or negative values ought to be trapped).

                      Comment

                      Working...