write a program to input appropriate value and print result of {in java}.
note→ Valuable description and documentation needed.
Kindly please don't spam :)
Answers
Code:-
import java.util. *;
class Function
{
public static void main (String[] args)
{
Scanner sc=new Scanner (System.in) ;
System.out.print("Value of a and b");//Using Scanner class
int a=sc.nextInt();
int b=sc.nextInt();
double result= Math.sqrt(Math.pow(a,2))+Math.pow(b,2);//using data type double for Math Functions
System.out.println("Result\t ="+result);//Output
}
}
________________________________
Variable | Data type | use
a | Integer. | to assign the value in main()
b. | Integer. | to assign the value in main()
result. | Double. | to get the result of Calculation
_______________________________
Important Points:-
- I used Scanner class here to insure that we ask user for value of n through keyboard.
- Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings through keyboard. It is the easiest way to read input in a Java program.
- Always divide double data type variable with integer variable , integer variable from double data type variable or double variable from double,To ensure 100% result.
Question:-
Write a program to input appropriate value and print result of √(a² + b²).
Program:-
import java.util.Scanner; // to import Scanner Class
class Calculate //Class name
{
public static void main(String s[]) //main method
{
// Creating objects of scanner class.
Scanner sc=new Scanner(System.in);
System.out.print("Enter a: ");
// Taking input of a
int a=sc.nextInt();
System.out.print("Enter b: ");
int b=sc.nextInt();
// Storing the result.
double result = Math.sqrt(a*a + b*b);
System.out.println("Result is: "+result);
} //end of main() method.
}// end of class.
This is the required program.
I have added comments so that you can understand better.
Note:- Words written in bold letters are keywords.
Variable Description:-
(Variable Name) (Data type) (Function)
1. a int(integer) Used to store the value of a.
2. b int(integer) Used to store the value of b.
3. result double Used to store square root of a² + b².
Math functions used:-
- Math.sqrt():- Used to calculate square root of any positive real number.
Other maths functions:-
- Math.cbrt() :- Used to calculate square root.
- Math.max() :- Used to find maximum of two numbers.
- Math.min() :- Used to find minimum of two numbers.