Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class RecursiveNestedLoops {
  3. public static int numberOfLoops;
  4. public static int numberOfIterations;
  5. public static int[] loops;
  6.  
  7. public static void main(String[] args) {
  8. Scanner input = new Scanner(System.in);
  9. System.out.print("N = ");
  10. numberOfLoops = input.nextInt();
  11. System.out.print("K = ");
  12. numberOfIterations = input.nextInt();
  13. input.close();
  14. loops = new int[numberOfLoops];
  15. nestedLoops(0);
  16. }
  17.  
  18. public static void nestedLoops(int currentLoop) {
  19. if (currentLoop == numberOfLoops) {
  20. printLoops();
  21. return;
  22. }
  23.  
  24. for (int counter=1;counter<=numberOfIterations;counter++) {
  25. loops[currentLoop] = counter;
  26. nestedLoops(currentLoop + 1);
  27. }
  28. }
  29.  
  30. public static void printLoops() {
  31. for (int i = 0; i < numberOfLoops; i++) {
  32. System.out.printf("%d ", loops[i]);
  33. }
  34. System.out.println();
  35. }
  36.  
  37. printLoops();
  38. return;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement