Number pattern 20 in C

Number pattern 20 in C

Write a C program to print the given number pattern using loop. How to print the given number pattern using for loop in C programming. Logic to print the given number pattern using loop in C program.

Example

Input

Input N: 5

Output



55555
4444
333
22
1


155555
 4444
  333
   22
    1

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

Before we discuss about logic of printing these patterns. I would recommend you to check out some similar number patterns. As both of the patterns mentioned in this program are just vertically flipped image of


1
22
333
4444
55555

Now, moving on to the logic of first pattern that we need to print. Have a careful eye on to the below pattern


55555
4444
333
22
1

Logic to print the above pattern:

1.To iterate through rows, initialize an outer loop from N to 1 (where N is the total number of rows). Note that I have initialized the loop from N to 1 not from 1 to N as the pattern is in descending order hence it will help in the inner loop. As both 1 to N or N to 1 will iterate N times.

2.To print numbers, initialize an inner loop from 1 to current_row_number (note that the current row number will be like n.., 4, 3...1 in decreasing order). Inside this loop print the value of current_row_number.

And you are done, lets implement this now.

Program to print the given number pattern 1




 
/**
 * C program to print number pattern
 */

#include 

int main()
{
    int i, j, N;

    printf("Enter N: ");
    scanf("%d", &N);

    for(i=N; i>=1; i--)
    {
        for(j=1; j<=i; j++)
        {
            printf("%d", i);
        }

        printf("\n");
    }

    return 0;

}



Output

Enter N: 5


55555
4444
333
22
1

Logic to print the given number pattern 2

Now, once you got the logic of first number pattern you can easily get the logic of this pattern. As is it same as pattern 1 just we need to add trailing spaces before the number gets printed. If you point your mouse over the below pattern you can count the number of spaces per row and can easily get the logic in which spaces are printed in the pattern.


55555
 4444
  333
   22
    1

Here the spaces are in ascending order i.e. row1 contains 0 spaces, row2 contains 1, row3 contains 2 and so on. Also each row contains current_row_number - 1 spaces. Logic to print spaces inside outer loop is:

1.To print spaces inside outer loop, run an inner loop from N to current_row_number. Inside this loop print spaces.

Lets, now implement this on code.

Program to print the given number pattern 2




 
/**
 * C program to print number pattern
 */

#include <stdio.h>

int main()
{
    int i, j, N;

    printf("Enter N: ");
    scanf("%d", &N);

    for(i=N; i>=1; i--)
    {
        // Logic to print spaces
        for(j=N; j>i; j--)
        {
            printf(" ");
        }

        // Logic to print numbers
        for(j=1; j<=i; j++)
        {
            printf("%d", i);
        }

        printf("\n");
    }

    return 0;
}






Instagram