# nested for loop question

• 05-20-2008, 06:49 AM
javabob
nested for loop question
say i have the following nested for loops
Code:

```for(x1 = 1; x1 < b; x1++)     for(x2 = 1; x2 < b; x2++)         for(x3 = 1; x3 < b; x3++)             y+=x1 * x2 * x3```
I want the user to input integers a and b, where there are 'a' nested for loops
(goes up to xa) and then output an integer y, In the above example a = 3 , since I won't know what a and b are beforehand I can't hand write the for loops. I tried using recursion but still can't solve the problem after working for several hours and I dont even know if that's the best way to go. Can somebody at least show me a correct first step to solving this problem?
• 05-20-2008, 04:52 PM
bksamrat
it shud surly work wit recursion!! if I wer in ur place I ud do it wit recursin function!! put som mor time.. that's the best way!!
• 05-20-2008, 10:13 PM
hardwired
Code:

```import java.util.Arrays; public class Test {     public static void main(String[] args) {         System.out.printf("%3s %3s %3s %5s %3s%n",                           "x1", "x2", "x3", "next", "y");         int a = 3;         int b = 4;         int x1, x2, x3, y = 0;         for(x1 = 1; x1 < b; x1++) {             for(x2 = 1; x2 < b; x2++) {                 for(x3 = 1; x3 < b; x3++) {                     int next = x1 * x2 * x3;                     y += next;                     System.out.printf("%2d  %2d  %2d  %3d  %4d%n",                                       x1, x2, x3, next, y);                 }             }         }         System.out.printf("desired y = %d%n", y);         y = 0;         int[] counts = new int[a];         Arrays.fill(counts, 1);         int index = counts.length-1;         int lastCount = counts.length-1;         boolean more = true;         do {             int product = 1;             for(int i = 0; i < counts.length; i++) {                 product *= counts[i];             }             y += product;             System.out.printf("index = %d counts = %s  product = %2d  y = %3d%n",                               index, Arrays.toString(counts), product, y);             more = !isDone(counts, b-1);             if(counts[lastCount] == b-1) {                 index = shiftUp(counts, index, b-1);             } else {                 counts[lastCount]++;             }         } while(more);         System.out.printf("loop y = %d%n", y);     }     private static int shiftUp(int[] counters, int index, int max) {         int nextIndex = counters.length-1;         while(counters[nextIndex] == max && nextIndex > 0) {             counters[nextIndex] = 1;             nextIndex--;         }         for(int i = nextIndex+1; i < counters.length; i++) {             counters[i] = 1;         }         counters[nextIndex]++;         return nextIndex;     }     private static boolean isDone(int[] counters, int limit) {         for(int i = 0; i < counters.length; i++) {             if(counters[i] < limit)                 return false;         }         return true;     } }```
• 05-20-2008, 11:00 PM
javabob
Thank you very much for taking the time to help me. I had a similar approach but i couldn't get the finer details in even after hours of thinking. If you did all that in less than an hour, then you sir are a genious(at least from my point of view)