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
- Variable r must be of double type as Math.sqrt method returns a double value.
- The line
r=sqrt(n);
should ber=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
Predict the Output of the given snippet, when executed:
switch (opn) { case 'a': System.out.println("Platform Independent"); break; case 'b': System.out.println("Object Oriented"); case 'c': System.out.println("Robust and Secure"); break; default: System.out.println("Wrong Input"); }
When (i) opn = 'b' (ii) opn = 'x' (iii) opn = 'a'
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); } }
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); } }
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); } } }