KnowledgeBoat Logo
|

Computer Applications

The following code has some error(s). Rewrite the correct code and underlining all the corrections made.

integer counter = 0; 
i = 10; num;
for (num = i; num >= 1; num--);
{
    If i % num = 0
    {
        counter = counter + 1;
    }
}

Java Iterative Stmts

2 Likes

Answer

The correct code is as follows:

int counter = 0; //Correction 1 
int i = 10, num; //Correction 2
for (num = i; num >= 1; num--) //Correction 3
{
    if( i % num == 0) //Correction 4
    {
        counter = counter + 1;
    }
}
Explanation
CorrectionStatementError
Correction 1integer counter = 0;The datatype should be int
Correction 2i = 10; num;variable declaration cannot be done without a data type. The syntax for variable declaration is datatype variablename = value/expression;. Multiple variables should be separated by comma not semicolon.
Correction 3for (num = i; num >= 1; num--);If a semicolon is written at the end of the for loop, the block of the loop will not be executed. Thus, no semicolon should be written after for loop.
Correction 4If i % num = 0If should be written in lower case and the condition should be written within brackets. Equality operator (==) should be used instead of assignment operator (=).

Answered By

2 Likes


Related Questions