Computer Science, asked by pramodsaich, 1 month ago

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 anindyaadhikari13
2

\textsf{\large{\underline{Solution}:}}

Given Co‎de:

for(i = 10; i >= 1; i++)

   System.out.println("the values are"+i);

Corrected Co‎de:

for(int i = 10; i >=1; i--)

   System.out.println("the values are"+i);

Rectifications:

  1. 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.
  2. 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'.

\textsf{\large{\underline{O{u}tput}:}}

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