[Solved] How can I save values produced in a for loop?


So in a for loop, when declaring the temp variable “i” you need to declare it as int i = 1; instead of “i = 1” I assume you already have a value for the variable d but in this case, I just used d = 123.

       int  d = 123;    
        for (int i = 1; i <= d; i++) 
        {
            if (d % i == 0) 
                System.out.print(i + " ");
        }

Output: 
1 3 41 123

In the case you want to store them and use it later in your code, there are many things you can use such as ArrayList, Queue, Stack, etc.

I used a stack here:

    Stack<Integer> intStack = new Stack<>();
    int d = 123;
    for (int i = 1; i <= d; i++) 
    {
        if (d % i == 0) 
        {
            intStack.push(i);
        }
    }
    while (!intStack.isEmpty())
    {
        System.out.println(intStack.pop());
    }

solved How can I save values produced in a for loop?