Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually I need the fastest algorithm code for this problem:
Print numbers from 1 to 75 but if numbers are multiple of 5 print "Foo", if numbers are multiple of 7 print "Bar" and if numbers are multiple of both 5 and 7 print "FooBar"

I am thinking to use if else for checking the number is divisible by 5,7 and by both.
C#
if(n%5==0)
System.out.print("Foo")
else if(n%7==0)
System.out.print("Bar")
else if(n%5==0&&n%7==0)
System.out.print("FooBar")
else
System.out.print(n)

So please post if you have any better code which is more efficiency and fastest than this.

Thanks
Sabin
Posted
Updated 5-May-12 9:01am
v3
Comments
Sandeep Mewara 5-May-12 15:03pm    
I believe, based on problem statement, if a number is multiple of 5 & 7 both then only 'FooBar' should get print. Current code of yours would print 'Foo' and go away. Please correct me if I am wrong.

Just put the check for both n%5 == 0 && n%7 == 0 to the top into the first if. That will solve your problem.

Regards,

Manfred
 
Share this answer
 
Comments
Shahin Khorshidnia 5-May-12 17:07pm    
Good suggestion +5
Thanks Sandeep for quick respond.Actually i am new at the C# here is my full code:
C#
static void Main(string[] args)
        {
            int n = 1;
            while (n <= 75)
            {
                if (n % 5 == 0)
                    System.Console.WriteLine("Foo");
                else if(n%7==0)
                    System.Console.WriteLine("Bar");
                else if (n%5==0 && n % 7 == 0)
                    System.Console.WriteLine("FooBar");
                else
                    System.Console.WriteLine(n);
                n++;
            }

It print correctly for number divisible by 5 and 7 but it print "Foo" for number is divisible my both 5 and 7.SO can you tell me what is wrong with code.As i am new to C# and is this is the fastest algorithm for this program.
 
Share this answer
 
A bit smaller and faster:

C#
void Solution3()
        {
            for (int n = 1; n <= 75; n++)
            {
                if (n % 35 == 0)
                    Console.WriteLine("FooBar");
                else if (n % 5 == 0)
                    Console.WriteLine("Foo");
                else if (n % 7 == 0)
                    Console.WriteLine("Bar");
                else
                    Console.WriteLine(n);
            }
        }


Greetings!

Axel Kemper
 
Share this answer
 
C#
for(int i=0;i<75;i++)
{if(n%5==0)
  print"foo"
 if(n%7==0)
  print "bar"
 else print i;
}
 
Share this answer
 
Comments
[no name] 19-Aug-13 14:31pm    
How does your code handle it if "i" is divisible by 5 AND 7 that was clearly specified by the requirement?
Besides that, the question is over a year old and already answered. There is no need to answer it again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900