4. find the error from the following program make correct and givethe output: for (i=10;i>=1;i++) System.out.println("the values are" +i);
Answers
Answered by
2
Given Code:
for(i = 10; i >= 1; i++)
System.out.println("the values are"+i);
Corrected Code:
for(int i = 10; i >=1; i--)
System.out.println("the values are"+i);
Rectifications:
- In line 1, variable named 'i' is not declared before initializing it. So, error occurs while executing the program. To correct it, write int i = 10.
- Also, the value of 'i' is incremented after every time. So, the condition given in the loop never becomes false. Thus, the loop run infinite times. To correct it, decrement the value of 'i'.
The output for the given program is:
the values are10
the values are9
the values are8
the values are7
the values are6
the values are5
the values are4
the values are3
the values are2
the values are1
See the attachment for verification.
Attachments:
Similar questions
Math,
28 days ago
Math,
28 days ago
Math,
28 days ago
Math,
1 month ago
Social Sciences,
1 month ago
India Languages,
9 months ago
English,
9 months ago
Accountancy,
9 months ago