KnowledgeBoat Logo
|

Computer Applications

Correct the errors of the given program:

class Simplify
{
public static void main(String args[])
{
int a,b,c,d;
a=10,b=5,c=1,d=2;
c=a2+b2;
d=(a+b)2;
p=c/d;
System.out.println(c + " "+ " "+d+ " " +p);
}
}

Input in Java

49 Likes

Answer

Errors in the snippet

  1. Multiple variable assignment statements cannot be separated by a comma. Semicolon should be used instead.
  2. Variables a2 and b2 are not defined. They should be a and b.
  3. The line d=(a+b)2 needs an operator between (a+b) and 2.
  4. Variable p is not defined.

Corrected Program

class Simplify
{
public static void main(String args[])
{
int a,b,c,d;
a=10;b=5;c=1;d=2;       //1st correction
c=a+b;                  //2nd correction
d=(a+b) / 2;            //3rd correction
int p=c/d;              //4th correction
System.out.println(c + " "+ " "+d+ " " +p);
}
}

Answered By

31 Likes


Related Questions