Computer Science, asked by radhajajimoggala99, 2 months ago

What is the output of the following program -
public class Compute
public static void main(string args[])
{
int result, x
x= 1;
result = 0;
while (x <= 10)
{
if (x%2 == 0)
result + = x;
+ + x
}
System.out.println(result);
}
}​

Answers

Answered by BrainlyProgrammer
5

Answer:

There are lots of errors in your códe, you have missed semicolon,brackets, Also it should be (String args[]) not (string args[])

Corrected Códe:-

public class Compute

{

public static void main(String args[])

{

int result, x;

x= 1;

result = 0;

while (x<=10)

{

if (x%2==0)

result+=x;

++x;

}

System.out.println(result);

}

}

Output:

30

Logic:

  • Loop runs from 1 to 10 adding all the even numbers

Lets solve it

2+4+6+8+10

6+6+8+10

12+8+10

20+10

30

Therefore

Correct Output:- 30

Similar questions