I had an interview today. This question was to optimize the code below. if we will see the code below after for loop there are four steps of "if-else" follows. So, interviewer asked me optimize it to 3 if-else line. I have tried a lot. But could not able to find the solution. Even he told me if you know scripting language then, you can use them also. Please help me in optimizing the same.
int main()
{
int i = 1;
for(i; i <= 100; i++)
{
if((i % 3 == 0 && i % 5 == 0))
{cout << "PR\n";}
else if(i % 3 == 0)
{cout << "P\n";}
else if(i % 5 == 0)
{cout << "R\n";}
else
{cout << i <<"\n";}
}
system("pause");
return 0;
}
This is a well known question... the "FizzBuzz".
You can even solve it without any explicit IFs