Java program that calculates the square, cube, and fourth power of a number
Posted by Samath
Last Updated: February 08, 2021

Write a Java program that reads a number and displays the square, cube, and fourth power. 

Example:

Please enter a Number: 5  

Square: 25.00                                                                                                                      

Cube: 125.00

Fourth Power: 625.00 

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
	    
		Scanner input = new Scanner(System.in);
		double num;
		double square_num;
		double cube_num;
		double fourth_num;
		
		System.out.print("Please enter a Number: ");
		num = input.nextDouble();
		
		input.close();
		
		square_num = num * num;
		cube_num = square_num * num;
		fourth_num = cube_num * num;
		
		System.out.printf("\nSquare: %.2f\n", square_num);
		System.out.printf("Cube: %.2f\n",cube_num);
		System.out.printf("Fourth Power: %.2f\n", fourth_num);
		
	}
}