435,347 Members | 2,120 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,347 IT Pros & Developers. It's quick & easy.

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

 P: 4 i tried this code but it is more than 400 bytes #include int main() { int x,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; } Aug 26 '10 #1
10 Replies

 100+ P: 687 It's 280 bytes even with comments. Aug 26 '10 #2

 P: 4 and if the memory limit (100 bytes) how can i solve this problem??? please help me!!! Aug 26 '10 #3

 100+ P: 687 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. Aug 27 '10 #4

 P: 4 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 Aug 27 '10 #5

 P: 4 this is the problem and i hope that you help me solve it Aug 27 '10 #6

 Expert 2.5K+ P: 3,112 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: Expand|Select|Wrap|Line Numbers 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 Aug 27 '10 #7

 Expert Mod 5K+ P: 9,197 Maybe I'm missing something, but printing out the odd numbers is: Expand|Select|Wrap|Line Numbers printf("%d", x[i]); where (x[i] % 2) != 0. Put this in a loop controlled by i and you are done. Aug 27 '10 #8

 Expert 100+ P: 2,400 Are negative numbers allowed in the input file? What is the next odd number for -5 (is it -3 or -7)? Aug 30 '10 #9

 100+ P: 1,059 try this Expand|Select|Wrap|Line Numbers next_odd=current_numb+(current_numb%2)+1;   It will always give you the next odd number..... Aug 30 '10 #10

 Expert 100+ P: 2,400 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). Aug 30 '10 #11 