Looping and outputting strings

Posted on 2011-04-29
Last Modified: 2012-06-21
Here is what I need.  I need a counter that goes from 1-100 that list each number.  If a number is divisible by 3, then write "divisible by 3" as opposed to the number.  If a number is divisible by 5 then write "divisible by 5" as opposed to the number, if the number is divisible by 3 and 5 then write, "divisible by 3 and 5" as opposed to the number.  If the number is not divisible by 3 or 5 or 3 and 5 then just write the number.
Question by:VBBRett
    LVL 5

    Accepted Solution

    StringBuilder sb = new StringBuilder();
    for(int i=1;i<=100;i++)
       bool divBy3 = (i%3) == 0;
       bool divBy5 = (i%5) == 0;
       if(divBy3 && divBy5)
          sb.AppendLine(string.Format("{0} divisible by 3 and 5", i));
       else if(divBy3)
          sb.AppendLine(string.Format("{0} divisible by 3", i));
       else if(divBy5)
          sb.AppendLine(string.Format("{0} divisible by 5", i));

    Open in new window

    LVL 19

    Assisted Solution

    by:Daniel Van Der Werken
    Is this homework?  I think we're not supposed to do homework for people.

    What approach have you taken so far?

    I would see something like this:

    for( int i = 1; i <= 100; i++ )
       if ( i % 3 )
          if ( i % 5 )

    That should get you started....
    LVL 15

    Expert Comment


    Better you start writing code, then raise a  question. if required. (I bet you won't require any help :) )
    But still we are always here to help.


    Author Comment

    Nope, it's not homework at all.  It just so happens I wrote code earlier and wanted to make sure we were all on the same page.  It just so happens that we are, thanks anyways guys!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now