Number pattern 9 in C

Write a C program to print the given number pattern using for 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 rows: 5
Input columns: 5
Output:

Required knowledge

Basic C programming, Loop

Logic to print the given pattern

Before getting on to this pattern I recommend you to go through one of my previous number patterns to get the basics of printing number patterns.

Now, once you are familiar with the number patterns. If you look to the pattern carefully you will notice that for each columns in the pattern current row number gets printed.

Program to print the given number pattern

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

#include <stdio.h>

int main()
{
    int rows, cols, i, j;

    /*
     * Reads number of rows, columns to be printed
     */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

    for(i=1; i<=rows; i++)
    {
        for(j=1; j<=cols; j++)
        {
            //Print the current row number
            printf("%d", i);
        }

        printf("\n");
    }

    return 0;
}


Output
Enter number of rows: 5
Enter number of columns: 5
11111
22222
33333
44444
55555


Screenshot

C program to print number pattern


Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment