Java program for class 9
write a class to input two integer and print square root of first number
hint- use math function- math.sqrt
warning- don't spam otherwise will be reported and your answer will be deleted
Answers
Answer:
here goes your answer.
Explanation:
One of the most popular frequently asked Java Interview Question is, “Given an integer x, write a java program to find the square root of it”. There are many ways to solve this problem. In this article, let’s check out different ways to find square and square root in Java.
What is Square and Square Root?
How to Square a Number in Java
By multiplying the number by itself
Using the Math.pow function
How to Find Square Root of a Number in Java
Using java.lang.Math.sqrt() method
By using Math.pow() function
Without using any inbuilt functions
Before discussing the square root code in Java, let’s understand the term square root first.
Square and Square Root
The square of a number is that number times itself. In other terms, when we multiply an integer by itself we call the product the square of the number. Mathematically, the square of a number is given as,
Square of n = n*n
For example, the square of number 4 is 4*4 = 16
The square root is just the opposite of the square. The square root of a number, n, is the number that gives n when multiplied by itself. Mathematically, the square root of a number is given as,
Square Root of n = √n
Now that you know what square and square root of a number are, let’s see different ways to calculate them in Java.
How to Square a Number in Java
You can square a number in Java in two different ways:
Multiply the number by itself
Call the Math.pow function
Method1: Square a number by multiplying it by itself
Here’s a Java Program to square a number by multiplying it by itself.
package MyPackage;
import java.util.Scanner;
public class Square1 {
public static void main(String args[]) {
Double num;
Scanner sc= new Scanner(System.in);
System.out.print("Enter a number: ");
num=sc.nextDouble();
Double square = num*num;
System.out.println("Square of "+ num + " is: "+ square);
}
}
Output
Enter a number: 10
Square of 10.0 is: 100.0
Method2: Square a number with the Math.pow method
Here’s a Java Program to call the Math.pow method to square a number.
package MyPackage;
import java.util.Scanner;
import java.lang.Math;
public class Square2 {
public static void main(String args[]) {
Double num;
Scanner sc= new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextDouble();
Double square = Math.pow(num, 2);
System.out.println("Square of "+ num + " is: "+ square);
}
}
Output
Enter a number: 22
Square of 22.0 is: 484.0
Now let’s check out how to calculate the square root of a number in Java.
How to Find Square Root of a Number in Java
There are multiple ways to find square root a given number in Java. Let’s explore a few of those.
Method1: Java Program to Find the square root of a Number using java.lang.Math.sqrt() method
Syntax
public static double sqrt(double x)
Parameter: x is the value whose square root is to be returned.
Return: This method returns the square root value of the argument passed to it.
If parameter x is positive double value, this method will return the square root of x
When x is NaN or less than zero, this method will return NaN
If parameter x is positive infinity, this method will return positive Infinity
When x is positive or negative zero, this method will return the result as Zero with the same sign
Code
package MyPackage;
public class SquareRoot2 {
public static void main(String args[])
{
double a = 100;
System.out.println(Math.sqrt(a));
// Input positive value, Output square root of x
double b = -81.00;
System.out.println(Math.sqrt(b));
// Input negative value, Output NaN
double c = 0.0/0;
// Input NaN, Output NaN
System.out.println(Math.sqrt(c));
double d = 1.0/0;
// Input positive infinity, Output positive infinity
System.out.println(Math.sqrt(d));
double e = 0.0;
// Input positive Zero, Output positive zero
System.out.println(Math.sqrt(e));
}
}
Answer:
I know it is fake account and I am told that it is my fake account and I am telling please follow my another account brainly superstar