How can I get this values from a loop function?

The output should be:

1 2 4 8 16

2 6 18 54 162

3 12 48 192 768

4 20 100 500 2500

5 30 180 1080 6480

6 42 294 2058 14406

I am currently stuck with my code:

using System;

class Program
{
    static void Main(string[] args)
    {
        int size = 6; 
    
        for (int i = 1; i <= size; i++)
        {
            int num = i; 
        
            if(num == 1) {
              for (int j = 1; j < size; j++)
              {
                   Console.Write(num + " "); 

                   num *= 2; 
               }
          
               Console.WriteLine();
               }else{
               int num2 = 2;
        
               for (int j = 1; j < size; j++)
               {

               Console.Write(num2 + " "); 
              num2 *= 3;
          }       
           Console.WriteLine();
          }

       }
   }
}

>Solution :

Based on the code and the expected results formula should be something like num *= i+1, so you can try just inner loop without any if:

int size = 6;

for (int i = 1; i <= size; i++)
{
    int num = i;
    for (int j = 1; j < size; j++)
    {
        Console.Write(num + " ");
        num *= i + 1;
    }
    Console.WriteLine();
}

Leave a Reply