KnowledgeBoat Logo

Computer Applications

Correct the errors in the given program:

class Sample
{
public static void main(String args[])
{
int n,p;
float k,r;
n=25;p=12; 
if(n=25)
{
k=pow(p,2)
System.out.println("The value of"+p+ " = "+k);
}
else
{
r=Math.square root(n); 
System.out.println("The value of"+n+ " = "+r);
} 
} 
}

Java Conditional Stmts

46 Likes

Answer

Explanation

  1. The line if(n=25) should be if(n==25)
  2. The line k=pow(p,2) should be k=(float)Math.pow(p,2);
  3. The line r=Math.square root(n); should be r=(float)Math.sqrt(n);

Corrected Program

class Sample
{
public static void main(String args[])
{
int n,p;
float k,r;
n=25;p=12; 
if(n==25)               //1st correction
{
k=(float)Math.pow(p,2); //2nd correction
System.out.println("The value of"+p+ " = "+k);
}
else
{
r=(float)Math.sqrt(n);  //3rd correction
System.out.println("The value of"+n+ " = "+r);
} 
} 
}

Answered By

30 Likes


Related Questions