Amit Mohanty

Amit Mohanty

  • 18
  • 48.6k
  • 4.6m

Nested for loops for N times in C#.

Sep 3 2019 5:24 AM
How to nested for loops for n times ?
 
Ex.
  1. int nLoop = 4;  
  2.   
  3. for (int i = 0; i <= 10; i++)  
  4. {  
  5.     for (int j = 0; j <= 5; j++)  
  6.     {  
  7.         for (int k = 0; k <= 12; k++)  
  8.         {  
  9.             for (int l = 0; l <= 5; l++)  
  10.             {  
  11.                 Console.WriteLine(i + " " + j + " " + k + " " + l);  
  12.             }  
  13.         }  
  14.     }  

 So if nLoop is 2 then there would only be 2 for loops i and j
if nLoop is 3 then there would only be 3 for loops i, j and k.
 

Answers (4)