|
-
Jul 24th, 2007, 10:29 AM
#1
Thread Starter
PowerPoster
quick code
ok this sounds easy but which is the best way of doing this?
in a for loop (lets say up to 100), for numbers where a multiple of X, print something. for a multiple of Y print something else. For a multiple of X and Y, print something else.
does that make sense? how would you find out (or the best way) if you are in multples?
would that be the mod operator? if so, then would this be right?
for (int counter = 0; counter < 100; counter++)
{
if (counter % X == 0)
{
Console.WriteLine("something 1");
}
else if (counter % Y == 0)
{
Console.WriteLine("something 2");
}
else
{
Console.WriteLine(counter.ToString());
}
}
but what about the last part "if they are multples of X and Y"?
Last edited by Techno; Jul 24th, 2007 at 10:41 AM.
-
Jul 24th, 2007, 10:53 AM
#2
Re: quick code
I was thinking % was the way to go as well. Maybe something like this?
Code:
int x = 2;
int y = 3;
for (int i = 1; i <= 100; i++)
{
if ((i % x == 0) && (i % y == 0))
{
Debug.WriteLine(i.ToString() + " is divisible by " + x.ToString() + " and " + y.ToString());
}
else if (i % x == 0)
{
Debug.WriteLine(i.ToString() + " is divisble by " + x.ToString());
}
else if (i % y == 0)
{
Debug.WriteLine(i.ToString() + " is divisible by " + y.ToString());
}
}
-
Jul 24th, 2007, 10:56 AM
#3
Thread Starter
PowerPoster
Re: quick code
yeh thats what I done but for the both multiples of X and Y it never evaluates to true but is THAT correct in itself?
-
Jul 24th, 2007, 11:00 AM
#4
Re: quick code
Really?
Here is my output from the code above:
Code:
2 is divisble by 2
3 is divisible by 3
4 is divisble by 2
6 is divisible by 2 and 3
8 is divisble by 2
9 is divisible by 3
10 is divisble by 2
12 is divisible by 2 and 3
...
-
Jul 24th, 2007, 11:03 AM
#5
Thread Starter
PowerPoster
Re: quick code
I'm doing it for multiples of 3 and 5.... could be why? give it a shot
-
Jul 24th, 2007, 11:04 AM
#6
Re: quick code
Code:
...
15 is divisible by 3 and 5
18 is divisble by 3
20 is divisible by 5
21 is divisble by 3
24 is divisble by 3
25 is divisible by 5
27 is divisble by 3
30 is divisible by 3 and 5
...
-
Jul 24th, 2007, 11:12 AM
#7
Thread Starter
PowerPoster
Re: quick code
hmm i dont get that. :-/
I figured it out (ordering of the IF statement) but still dont get your results.
15 is divisable by 5
16 is divisable by 3
17
18 is divisable by 3 and 5
19
20 is divisable by 3
21 is divisable by 5
22 is divisable by 3
23
24 is divisable by 3 and 5
25
26 is divisable by 3
27 is divisable by 5
28 is divisable by 3
29
30 is divisable by 3 and 5
code:
Code:
for (int counter = 1; counter <= 100; counter++)
{
multiple3 = counter % 2;
multiple5 = counter % 3;
if (multiple3 == 0 && multiple5 == 0)
{
//this.listBox1.Items.Add("fizz");
System.Diagnostics.Debug.WriteLine(counter.ToString() + " is divisable by 2 and 3");
}
else if (multiple3 == 0)
{
//this.listBox3.Items.Add("fizzbuzz");
System.Diagnostics.Debug.WriteLine(counter.ToString() + " is divisable by 2");
}
else if (multiple5 == 0)
{
//this.listBox2.Items.Add("buzz");
System.Diagnostics.Debug.WriteLine(counter.ToString() + " is divisable by 3");
}
else
{
//this.listBox4.Items.Add(counter.ToString());
System.Diagnostics.Debug.WriteLine(counter.ToString());
}
}
-
Jul 24th, 2007, 11:18 AM
#8
Thread Starter
PowerPoster
Re: quick code
wait i think i got it....thanks. ill be back if i find something not right
-
Jul 24th, 2007, 11:21 AM
#9
Re: quick code
 Originally Posted by Techno
code:
Code:
for (int counter = 1; counter <= 100; counter++)
{
multiple3 = counter % 2;
multiple5 = counter % 3;
if (multiple3 == 0 && multiple5 == 0)
{
//this.listBox1.Items.Add("fizz");
System.Diagnostics.Debug.WriteLine(counter.ToString() + " is divisable by 2 and 3");
}
else if (multiple3 == 0)
{
//this.listBox3.Items.Add("fizzbuzz");
System.Diagnostics.Debug.WriteLine(counter.ToString() + " is divisable by 2");
}
else if (multiple5 == 0)
{
//this.listBox2.Items.Add("buzz");
System.Diagnostics.Debug.WriteLine(counter.ToString() + " is divisable by 3");
}
else
{
//this.listBox4.Items.Add(counter.ToString());
System.Diagnostics.Debug.WriteLine(counter.ToString());
}
}
You have variables called "multiple3" and "multiple5" but are checking against the numbers 2 and 3 above. You want to check 3 and 5 right?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|