Computer Applications
Correct the errors of 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);
}
}
}
Answer
Errors in the snippet
if(n=25)should beif(n==25)- Correct way to call method pow is Math.pow(p,2);
- Return type of Math.pow is double. Variable k is of float type so return value should be explicitly casted to float.
Math.square root(n)should beMath.sqrt(n)- Return type of Math.sqrt is double. Variable r is of float type so return value should be explicitly casted to float.
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 & 3rd corrections
System.out.println("The value of"+p+ "= "+k);
}
else
{
r=(float)Math.sqrt(n); //4th & 5th corrections
System.out.println("The value of"+n+ "= "+r);
}
}
}
Related Questions
Write a program to input two unequal numbers. Display the numbers after swapping their values in the variables without using a third variable.
Sample Input: a=23, b= 56
Sample Output: a=56, b=23Write a program to input the temperature in Celsius and convert it into Fahrenheit. If the temperature is more than 98.6 °F then display "Fever" otherwise "Normal".
Write a program to accept marks of a student obtained in 5 different subjects (English, Phy., Chem., Biology, Maths.) and find the average. If the average is 80% or more then he/she is eligible to get "Computer Science" otherwise "Biology".
'Mega Market' has announced festival discounts on the purchase of items, based on the total cost of the items purchased:
Total cost Discount Up to ₹2,000 5% ₹2,001 to ₹5,000 10% ₹5,001 to ₹10,000 15% Above ₹10,000 20% Write a program to input the total cost. Display name of the customer, discount and the amount to be paid after discount.