1 12 123 patterns in c

In this tutorial, we are going to print 1 12 123 patterns in c.

The logic of the program is:

We will use nested for loop.
The outer for loop will act as row and the inner for loop will act as column.

Let’s iterate through the loop.
When i = 1, the inner loop will only run 1 time, hence printing 1
When i = 2, the inner loop will run 2 times, hence printing 12
When i = 3, inner loop will run 3 time, hence printing 123 and so on.
you can see the pattern.

If you pay close attention to line number 14, as soon as the inner stop executing we start printing from the next line.

#include <stdio.h>

int main() {
    
    int i,j,n;
    printf("Enter the value of n :");
    scanf("%d",&n);
    for(i = 1;i<=n;i++)
    {
        for(j = 1;j <= i; j++)
        {
            printf("%d",j);
        }
        printf("\n");
    }
}

The output of the code is

Enter the value of n :6
1
12
123
1234
12345
123456

Second output

Enter the value of n :3
1
12
123

Facebook
Twitter
LinkedIn
Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

ABOUT ME !!
Johan William

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

RECENT POSTS

TEST