₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,326,897 members, 8,428,544 topics. Date: Wednesday, 17 June 2026 at 03:17 PM

Toggle theme

Programming Challenge. - Programming (2) - Nairaland

Nairaland ForumScience/TechnologyProgrammingProgramming Challenge. (2387 Views)

1 2 Reply (Go Down)

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:
It is really sad and upsetting that anyone would come up with such a cheap challenge. embarassed
Please dudes, either make sure to come up with brain crackers, or just ask for coding help.
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:
Another surface-thinker, How many people actually got what I wanted?
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:
//recursive solution
public int multiples7(int i)
{
if(i%7==0 && i<350)
{
System.out.print(i + ' ');
i*=7;
multiples7(i);
}
}
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:
Nice. I think this is faster though tedious

System.out.println("0/n7/n14, "wink 



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


We should be doing code-benchmarks more often grin
grin grin Ladies and gentlemen i believe we have ourselves a winner grin grin

Anyway for now i think i'll just stand by the side, watch the winning code and rewrite it in Python grin grin
Re: Programming Challenge. by okechukwudiei(m): 9:32am On Aug 06, 2011
Mobinga:
Good, but doesn't it check if every positive integer below 350 has a modulus 7 => 0?
@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.
1 2 Reply

Programming Challenge For Beginners Competition Two N20000 -SEASON 2-Programming Challenge For Beginners N20000Facebook Programming Challenge Question234

Are There Programmers With CS Degree But Can't Code?Please Recommend A Good Laptop For Me To Start My CodingSplash Screen In Vb 2008