Results 1 to 9 of 9

Thread: quick code

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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());
                    }
                }

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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
    ...

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: quick code

    I'm doing it for multiples of 3 and 5.... could be why? give it a shot

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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
    ...

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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());
    				}
    				
    			}

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: quick code

    wait i think i got it....thanks. ill be back if i find something not right

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  9. #9
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: quick code

    Quote 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
  •  



Click Here to Expand Forum to Full Width