In: Computer Science
Answer in JAVA
Write a program that would prompt the user to enter an integer. The program then would displays a table of squares and cubes from 1 to the value entered by the user. The program should prompt the user to continue if they wish. Name your class NumberPowers.java, add header and sample output as block comments and uploaded it to this link.
Use these formulas for calculating squares and cubes are:
Assume that the user will enter a valid integer.
The application should continue only if the user enters “y” or “Y” to continue.
Sample Output
Welcome to the Squares and Cubes
table
Enter an integer: 4
Number Squared Cubed
====== ======= =====
1 1 1
2 4 8
3 9 27
4 16 64
Continue? (y/n): y
Enter an integer: 7
Number Squared Cubed
====== ======= =====
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
Continue? (y/n): n
import java.util.Scanner; public class NumberPowers { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Welcome to the Squares and Cubes table\n"); String chice = "Y"; while (chice.equalsIgnoreCase("y")) { System.out.print("Enter an integer: "); int n = scan.nextInt(); System.out.println("\nNumber Squared Cubed"); System.out.println("====== ======= ====="); for (int i = 1; i <= n; i++) { System.out.println(i + " " + (i * i) + " " + (i * i * i)); } System.out.print("\nContinue? (y/n): "); chice = scan.next(); System.out.println(); } } }