KnowledgeBoat Logo

Computer Applications

Correct the errors in 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);
} 
}
    

Java Conditional Stmts

29 Likes

Answer

Explanation

  1. The line a=10,b=5,c=1,d=2; generates a compile time error. We will combine the declaration and initialization of these variables.
  2. The line c=a2+b2; is written in Java like this c = (int)(Math.pow(a, 2) + Math.pow(b, 2));
  3. The line d=(a+b)2; is written in Java like this d=(int)Math.pow((a+b), 2);
  4. Variable p is not defined

Corrected Program

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

Answered By

20 Likes


Related Questions