KnowledgeBoat Logo

Computer Applications

Write a program to input a number. Calculate its square root and cube root. Finally, display the result by rounding it off.

Sample Input: 5
Square root of 5= 2.2360679
Rounded form= 2
Cube root of 5 = 1.7099759
Rounded form= 2

Java

Java Math Lib Methods

74 Likes

Answer

import java.util.Scanner;

public class KboatRoots
{
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter the number: ");
        int num = in.nextInt();
        
        double sqrtValue = Math.sqrt(num);
        double cbrtValue = Math.cbrt(num);
        long roundSqrt = Math.round(sqrtValue);
        long roundCbrt = Math.round(cbrtValue);
        
        System.out.println("Square root of " + num + " = " + sqrtValue);
        System.out.println("Rounded form = " + roundSqrt);
        System.out.println("Cube root of " + num + " = " + cbrtValue);
        System.out.println("Rounded form = " + roundCbrt);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to input a number. Calculate its square root and cube root. Finally, display the result by rounding it off. Sample Input: 5 Square root of 5= 2.2360679 Rounded form= 2 Cube root of 5 = 1.7099759 Rounded form= 2

Answered By

31 Likes


Related Questions