Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get this values from a loop function?

The output should be:

1 2 4 8 16

2 6 18 54 162

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading