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
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
Math,
1 month ago
World Languages,
1 month ago
Physics,
2 months ago
Science,
2 months ago
Computer Science,
9 months ago
Math,
9 months ago