logical interview question_ print foo for time of 3, boo for time of 5 and
fooboo for both
I have been asked logical question in interview. I have solved it but just
wondering how other programmers approach to same problem of let say which
is most efficient way. I have used C# but you can use any language.
question is print number from 1 to 100. if next number is time of 3 then
print foo. If next number in queue is time of 5, print boo and if next
number in queue is time of 3 and 5 both then print fooboo else print
number
I have used bool in my logic. lets see whats other audience come back with!!!
class Program
{
static void Main(string[] args)
{
MyClass_01 obj1 = new MyClass_01();
obj1.myFunction_01();
}
}
class MyClass_01
{
public void myFunction_01()
{
for (float i = 1; i <= 100; i++)
{
bool gate1 = false;
bool gate2 = false;
if (i % 3 == 0)
{
gate1 = true;
}
if (i % 5 == 0)
{
gate2 = true;
}
if (gate1 && !gate2)
{
Console.WriteLine(i + " foo");
}
else if (gate2 && !gate1)
{
Console.WriteLine(i + " boo");
}
else if (gate1 && gate2)
{
Console.WriteLine(i + " booFoo");
}
else
{
Console.WriteLine(i);
}
}
Console.ReadLine();
}
}
No comments:
Post a Comment