Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,807 members, 7,817,339 topics. Date: Saturday, 04 May 2024 at 10:33 AM

Trivial Quiz? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Trivial Quiz? (1205 Views)

Android Quiz Application With Json Parser, Php And Mysql Database / Help To How To Create A Quiz System Using VB.NET / Simple PHP Quiz: Kill The Rand() (2) (3) (4)

(1) (Reply) (Go Down)

Trivial Quiz? by bakenda(m): 11:29am On Nov 16, 2011
I was preparing a lesson example, and found
it's not as straightforward as i thought -

write a simple loop that outputs all the odd
numbers less than 50.

--use only while loop
--do not use the modulus(%) operator

Just want to see the different approaches
of fellow programmers.
Re: Trivial Quiz? by Fayimora(m): 1:25pm On Nov 16, 2011
With Java
class Bakenda
{
public static void main(String[] param)
{
int x=1;
while(x<50){
System.out.println( x );
x+=2;
}
}
}

or with Ruby

(1. .50).step(2) { |n| puts n}

Remove the space between the dots. .
Re: Trivial Quiz? by bakenda(m): 1:59pm On Nov 16, 2011
Gooood.


I should av added a 3rd condition - use only a 1-step increment.
Re: Trivial Quiz? by Fayimora(m): 3:28pm On Nov 16, 2011
I Dont understand that condition, Does that apply to me??, Do you mean it has to be +1 always?
Re: Trivial Quiz? by bakenda(m): 10:19pm On Nov 16, 2011
Fayimora:

I Dont understand that condition, Does that apply to me??, Do you mean it has to be +1 always?

Yeah. You may want to try it out with +1
Re: Trivial Quiz? by Nobody: 10:53pm On Nov 16, 2011
# Python

i = 0
while i < 25:
   print(2 * i + 1)
   i += 1
Re: Trivial Quiz? by ektbear: 7:13am On Nov 17, 2011
You can do it in closed form, I think. So no reason to do any looping.

sum of the even numbers from 0 to 50 is equal to 2 times the sum of the numbers from 0 to 25. And there is a closed form expression for the latter (don't recall it off the top of my head, but it is on wikipedia.)
Re: Trivial Quiz? by Nobody: 7:22am On Nov 17, 2011
Re: Trivial Quiz? by ektbear: 7:55am On Nov 17, 2011
Oh wow. I skimmed the post and misread it, thought he wanted a sum too. My bad. .
Re: Trivial Quiz? by ektbear: 8:08am On Nov 17, 2011
Wow, pretty bad fail on my part. Whoops :x
Re: Trivial Quiz? by Fayimora(m): 4:38am On Nov 18, 2011
bakenda:

Yeah. You may want to try it out with +1
Guess thers no point again, ma solution is more or less the same with @omo's python solution
Re: Trivial Quiz? by bakenda(m): 12:14pm On Nov 18, 2011
^^^

Yeah. My solution is similar too, but I didn't arrive at it
"instantly" like that. I was working on an example on
how to use the while loop(to output odd numbers), I
thought it was a simple thing(it is, actually), my  initial
mistake
was to write something like:

while($n<50)
  {
  echo (2*$n)+1 . "
";
$n++;
  }
Re: Trivial Quiz? by Chimanet(m): 4:45pm On Nov 18, 2011
Now i c why computer science is tied to mathematics @ omo to dun u too much

(1) (Reply)

Joomla And Wordpress Assistance / I Need Compilers For Java,c++, PASCAL And Python Available In Android Format / School Management Software For Your Growing School

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.