This is not efficient, simplify this. if (n == 1) result = n + 2; if (n == 2) result = n + n; if (n == 3) result = 3 * n; if (n <= 0) result = 0; if (n > 3) result = 1;
We will use a simple if and else statement.
The if statement will control n>3, and the else part will use the switch statement.
We will use a switch statement for the case n == 1 , n == 2, n == 3, and the default case for n <=0.
Here is the simplification
if( n > 3)
{
result = 1;
}
else
{
switch(n)
{
case 1: result = n + 2;
break ;
case 2: result = n + n;
break ;
case 3: result = 3*n;
break ;
default: result = 0;
break;
}
}
Please don't forget to give a positive rating to the answer. Your rating motivates experts to help other students also. Thank you :)
Get Answers For Free
Most questions answered within 1 hours.