167k views
0 votes
What is the best way to improve the following code fragment? if ((counter % 10) == 0) { System.out.println("Counter is divisible by ten: " + counter); counter++; } else { System.out.println("Counter is not divisible by ten: " + counter); counter++; }

User Vallentin
by
4.4k points

1 Answer

2 votes

Answer:

The correct code to the given question is

if ((counter % 10) == 0) // check the condition

{

System.out.println("Counter is divisible by ten: " + counter); // display

}

else // check the condition

{

System.out.println("Counter is not divisible by ten: " +counter); // display the //value

}

counter++; // increment the value of counter

Step-by-step explanation:

Following are the description of code

  • In the given question we have to check the condition that the given number is divisible by 10 or not .
  • In the if block if the number is divisible by 10 then it print the value of number and increment the value of counter .
  • In the else block if the number is not divisible by 10 then it print the value of number and increment the value of counter .
  • It means the value of counter is increases in the if block as well as in the else block .So we have to remove the counter statement from there and place outside the if and else block .

User Colin Swelin
by
4.9k points