Write a complete program in a single class called RootMaker

Write a complete program in a single class called RootMaker, which reads in an integer value from the keyboard, and places it in a variable, say num. If num is non-negative, the program should print the square root of num. However, if num is negative, the program should then print the phrase \"negative input\" please it very urgent and thank you.

Solution

RootMaker.java

import java.util.Scanner;

public class RootMaker {

   public static void main(String[] args) {
       //Declaring variable
       int num;
      
//Scanner class object is used to read the inputs entered by the user
   Scanner sc=new Scanner(System.in);
  
   //Getting the number entered by the user
   System.out.print(\"Enter a number :\");
num=sc.nextInt();
  
/* If the number is positive then
* if block will get executed and find square root
*/
if(num>=0)
{
   //Displaying the square root of the positive number
   System.out.printf(\"The Square Root of %d is :%.2f \",num,Math.sqrt(num));
}
//If the number is negative then else-if lock will get executed
else if(num<0)
{
   //Displaying the Phrase
   System.out.println(\":: Negative Input ::\");
}
   }

}

__________________________________

Output:

Enter a number :50
The Square Root of 50 is :7.07

__________________________________

output2:

Enter a number :-10
:: Negative Input ::

_______________Thank You

Write a complete program in a single class called RootMaker, which reads in an integer value from the keyboard, and places it in a variable, say num. If num is

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site