javauser-inputsquare-rootdecrement

Square root decrement from user input


After getting an integer from the user and finding the square root, I need to find the square root of every number from i to 0. I am having trouble figuring out how to properly decrement the input so it prints out the square root of each number in an new line. I apologize if my title is not specific enough, I was having trouble attempting to describe this question.

public class Number {

    public static void main(String[] args) {

    int i = 1;

    Scanner scnr = new Scanner(System.in);
    System.out.print("Please enter a number: ");


    while (i >= 1)
    {

        i = scnr.nextInt();
        System.out.println(Math.sqrt(i));
        i++;
    }
    System.out.println(i);
}

Solution

  • decrement i within loop

    int i = 0;
    Scanner scnr = new Scanner(System.in);
    System.out.print("Please enter a number: ");
    i = scnr.nextInt();
    while (i >= 1)
    {
        System.out.println(Math.sqrt(i));
        i--;
    }