Active Member
★ Black Lounger ★
✔️ HL Verified
💻 Oldtimer
Giving context for the question I wanted to ask in the chatbox
I'm working on an assignment where I put in a number (1-25) and the program will print out the numbers starting at 1 and will also square each number as it counts up
is the code that I have right now. The first numbers are incrementing properly but the numbers after the equals sign are only showing the solution of the final number. So if I type in 4 it's going to look like this:
1 = 16
2 = 16
3 = 16
4 = 16
How do I get the program to square each number as it counts up instead of just squaring the number that I put in the program?
I'm working on an assignment where I put in a number (1-25) and the program will print out the numbers starting at 1 and will also square each number as it counts up
Code:
import java.util.Scanner;
public class Validation
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int inputNumber = 0;
int square;
do
{
System.out.print("Enter a number between 1 and 25: ");
inputNumber = input.nextInt();
} while ( inputNumber > 25 || inputNumber < 1 );
for (int inputTest=1;inputTest<=inputNumber;inputTest++)
{
square = inputNumber * inputNumber;
System.out.printf("%s = %s%n",
inputTest, square);
}
}
}
1 = 16
2 = 16
3 = 16
4 = 16
How do I get the program to square each number as it counts up instead of just squaring the number that I put in the program?