Programming Challenge. - Programming (2) - Nairaland
Nairaland Forum › Science/Technology › Programming › Programming Challenge. (2387 Views)
| Re: Programming Challenge. by Nobody: 9:35pm On Aug 04, 2011 |
@ Beaf Wetin dey happen? And where have you been? NL programming section has missed you too much. By the way, please do not insult Mobinga, he is one of our finest programmers here. The goal of this challenge was just to see how folks think about simple concepts, not to provide an avenue where seasoned programmers can show off their well crafted advanced skills. And if you still feel thus, you can always create the type of challenge that you think is worthy of your time; after all, you are one of the pioneers of challenges on NL, at least, since I joined NL. |
| Re: Programming Challenge. by Mobinga(op): 11:23pm On Aug 04, 2011 |
Beaf:Another surface-thinker. Don't be ignorant. Read the thread. How many people actually got what I wanted? |
| Re: Programming Challenge. by mxxpunkxx: 12:54am On Aug 05, 2011 |
Mobinga:Nailed! |
| Re: Programming Challenge. by okechukwudiei(m): 1:03pm On Aug 05, 2011 |
//recursive solution public int multiples7(int i) { if(i%7==0 && i<350) { System.out.print(i + ' '); i*=7; multiples7(i); } } |
| Re: Programming Challenge. by okechukwudiei(m): 1:38pm On Aug 05, 2011 |
okechukwu.diei:There are a few errors I made. The method should return 'void' and not 'int' and the second boolean expression in the if statement should b 'i<=350'. |
| Re: Programming Challenge. by Mobinga(op): 2:07pm On Aug 05, 2011 |
Good, but doesn't it check if every positive integer below 350 has a modulus 7 => 0? |
| Re: Programming Challenge. by njira(f): 4:00pm On Aug 05, 2011 |
Mobinga: Ladies and gentlemen i believe we have ourselves a winner ![]() Anyway for now i think i'll just stand by the side, watch the winning code and rewrite it in Python ![]() |
| Re: Programming Challenge. by okechukwudiei(m): 9:32am On Aug 06, 2011 |
Mobinga:@Mobinga it doesn't. The % operator is used to evaluate the integer remainder of a modulus division. This trick can be used to find multiples of 7 cos a multiple of 7 will evaluate to 0 if it's modulo 7. For instance 14 modulo 7 is 0 whereas 15 modulo 7 is 1. Thus 15 is not a multiple of 7 cos its modulo doesn't evaluate to 0. The second part of the conditional if statement i <= 350 ensures that the statement within the if block will be executed in 50 recursive calls. Now carefully examine my code and you'll discover that if it's implemented you'll get an infinite loop. This is because in the call to the recursive method within the if block i forgot to increment the argument i by 1. Hence, the same argument is always passed to the recursive method and i is ALWAYS less than 350. The simplest way to prevent this ugly situation is presented bellow: public void multiple7(int i) { if (i%7==0 && i<=350) { System.out.format("%d3", i); /* notice i used System.out.format() & not println(). This is Java's implementation of the printf() function in C programming language. */ multiple7(i+1); /* or pass argument i as ++i using pre-order increment operator */ } } nted you'll get an infinite loop. This is because in the call to the recursive method within the if block i forgot to increment the argument i by 1. Hence, the same argument is always passed to the recursive method and i is ALWAYS less than 350. The simplest way to prevent this ugly situation is presented bellow: public void multiple7(int i) { if (i%7==0 && i<=350) { System.out.format("%d3", i); /* notice i used System.out.format() & not println(). This is Java's implementation of the printf() function in C programming language. */ multiple7(i+1); /* or pass argument i as ++i using pre-order increment operator */ } } |
| Re: Programming Challenge. by okechukwudiei(m): 9:42am On Aug 06, 2011 |
@mobinga dnt forget to add the line of code i*=7; BEFORE calling the recursive method multiple7(i+1). Also, in the statement System.out.format(), modify the format string "%d3" to "%d3 %n \r\n". If u know C\C++ then I don't need to explain the format string. |
Programming Challenge For Beginners Competition Two N20000 -SEASON 2- • Programming Challenge For Beginners N20000 • Facebook Programming Challenge Question • 2 • 3 • 4
Are There Programmers With CS Degree But Can't Code? • Please Recommend A Good Laptop For Me To Start My Coding • Splash Screen In Vb 2008

Since it doesn't loop at all. It just prints the string.