KnowledgeBoat Logo

Computer Applications

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

Java Conditional Stmts

52 Likes

Answer

Explanation

  1. public is a keyword so it can't be used as an identifier for naming the class. Change the class name from public to any valid identifier, for example class Sample
  2. Argument of main method is an array of Strings. Use square brackets instead of curly brackets — String args[]
  3. c is an int variable. We cannot assign a double literal 65.45 to it.
  4. Variables sum & diff are not defined
  5. The line System.out.println(sum,diff); should be written like this System.out.println(sum + " " + diff);

Corrected Program

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

Answered By

36 Likes


Related Questions