This sequence represents to each given integer its square.
The limit of the numbers to be squared can be entered by the user.
import java.util.Scanner;
import java.lang.Math;
class SquareNumbers {
private static void squareNumberAlgorithm(int n) {
int square;
for (int i = 1; i <= n; i++) {
square = i;
// square = square * square;
square = (int) Math.pow(square, 2);
System.out.println(i + ": " + square);
}
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Up to which number should be squared? ");
int quantitiy = reader.nextInt();
reader.close();
squareNumberAlgorithm(quantitiy);
}
}
Up to which number should be squared? 10
1: 1
2: 4
3: 9
4: 16
5: 25
6: 36
7: 49
8: 64
9: 81
10: 100