Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   While loop (http://www.programmingforums.org/showthread.php?t=14279)

Kasit Hussain Oct 27th, 2007 10:16 AM

While loop
 
Hi guys,

I am new here and my journey with programming has just started. I need help with while loop in C#. Could anyone help with coding this using while or for loop?
it should count from 1 to 100 and prints out every number in the sequence that is divisible by 5 and tests whether that integer is even or odd.
Thanks,
Kasit

mrynit Oct 27th, 2007 10:41 AM

Re: While loop
 
read this http://en.wikipedia.org/wiki/Modulo_operation

crawforddavid2006 Oct 27th, 2007 12:28 PM

Re: While loop
 
Quote:

Originally Posted by Kasit Hussain (Post 135914)
Hi guys,
and prints out every number in the sequence that is divisible by 5


Do you mean evenly divisible by 5 since every number is technically divisible by 5

Kasit Hussain Oct 27th, 2007 12:33 PM

Re: While loop
 
Quote:

Originally Posted by crawforddavid2006 (Post 135920)
Do you mean evenly divisible by 5 since every number is technically divisible by 5

Yes I mean divisible by 5 the number has to be an integer so it would have to be 5,10, 15 etc until 100.

Thank you,

crawforddavid2006 Oct 27th, 2007 11:05 PM

Re: While loop
 
try this, and i'm not sure if this will work 100% since i havent compiled it
:

int i = 1;
while(i <= 100)
{
    if(i % 5 = 0)
    {
          Console.writeLine(i);
              if(i % 2 = 0)
                    Console.writeLine(i + "is even");
              else
                    Console.writeLine(i + "is odd");
    }

    i++;

}


again, i might be wrong about some of that since i didnt compile it, and can't compile it because i dont have an IDE on my comp right now.

DaWei Oct 27th, 2007 11:10 PM

Re: While loop
 
Mangle it a little bit and call it pseudocode. Then no one can bust your chops. ;)

crawforddavid2006 Oct 28th, 2007 12:02 AM

Re: While loop
 
Good point, o well. I'll remember that next time.

Kasit Hussain Oct 28th, 2007 6:10 AM

Re: While loop
 
Quote:

Originally Posted by crawforddavid2006 (Post 135931)
Good point, o well. I'll remember that next time.

:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

              private bool isDivisibleBy(int checkItem, int divItem)
        {
            if (checkItem % divItem == 0)
                return true;
            return false;
        }
            {
            int i = 1;
            while (i <= 100)
       

     

                if (isDivisibleBy(i, 5))
                {
                    if (isDivisibleBy(i, 2))
                        Console.WriteLine(i + " - even");
                    else
                        Console.WriteLine(i + " - odd");

                    {
                        Console.writeLine(i);
                        if (i % 2 = 0)
                            Console.writeLine(i + "is even");
                        else
                            Console.writeLine(i + "is odd");
                    }

                    i++;

                }
            }


have done it like that but still isnt working, any hints?

xavier Oct 28th, 2007 8:23 AM

Re: While loop
 
Please wrap the code in [code] My code [/code ] tags !
Oh, and do tell what the problem is with the code.

crawforddavid2006 Oct 28th, 2007 1:45 PM

Re: While loop
 
I agree with xavier, we need to know what problem your getting so we can take a look.


All times are GMT -5. The time now is 3:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC