Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

Looping and outputting strings

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.
0
VBBRett
Asked:
VBBRett
2 Solutions
 
morguloCommented:
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));
   else
      sb.AppendLine(i.ToString());
}

Console.Write(sb.ToString());

Open in new window

0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
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....
0
 
NavneetCommented:
Hi!

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.

Thanks!
0
 
VBBRettAuthor Commented:
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!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now