KnowledgeBoat Logo

Computer Applications

Correct the errors in the given program:

class Square
{
public static void main(String args[])
{
int n=289,r;
r=sqrt(n);
if(n==r)
System.out.println("Perfect Square"); 
else
System.out.println("Not a Perfect Square");
} 
}

Java Conditional Stmts

32 Likes

Answer

Explanation

  1. Variable r must be of double type as Math.sqrt method returns a double value.
  2. The line r=sqrt(n); should be r=Math.sqrt(n);

Corrected Program

class Square
{
public static void main(String args[])
{
int n=289;
double r=Math.sqrt(n); //1st & 2nd correction
if(n==r)
System.out.println("Perfect Square"); 
else
System.out.println("Not a Perfect Square");
} 
}

Answered By

18 Likes


Related Questions