81.5k views
4 votes
Write the code to input a number and print the square root. Use the absolute value function to make sure that if the user enters a negative number, the program does not crash.

User Qba
by
4.7k points

1 Answer

2 votes

Answer:

Step-by-step explanation:

Language?

I'm assuming this is AP CS A so I'll do it in java.

import java.util.Scanner;

class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Number: ");

int sqrtNumber = Math.abs(input.nextInt());

double printableNumber = Math.sqrt(sqrtNumber);

System.out.println(printableNumber);

}

}

User Alevtina
by
5.4k points