Computer Applications

Correct the errors of the given program:

class public
{                                     
public static void main(String args{})
{                                     
int a=45,b=70,c=65.45;                
sum=a+b;                              
diff=c-b;                             
System.out.println(sum,diff);         
}                                     
}                                     

Input in Java

101 Likes

Answer

Errors in the snippet

  1. Name of the class can't be public as public is a keyword.
  2. Argument of main function should be an array of Strings.
  3. We cannot assign 65.45 to c as c is an int variable and 65.45 is a floating-point literal.
  4. Variable sum is not defined.
  5. Variable diff is not defined.
  6. println method takes a single argument.

Corrected Program

class A     //1st correction                                 
{                                     
public static void main(String args[])  //2nd correction
{                                     
int a=45,b=70;
double c=65.45;     //3rd correction              
int sum=a+b;        //4th correction
double diff=c-b;    //5th correction
System.out.println(sum + " " + diff);   //6th correction
}                                     
}   

Answered By

53 Likes


Related Questions