C program tutorial to print a number pattern

C programming tutorial on number pattern :

In this tutorial, we will learn how to print a pattern like below using C programming language :

3 3 3
2 2 2
1 1 1
2 2 2
3 3 3

or

3 3 3 3
2 2 2 2
1 1 1 1
2 2 2 2
3 3 3 3

3 3 3 3 3 3 3 3 3
2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3

For all these patterns :

  1. The height should be odd i.e. like 3, 5, 7 etc.
  2. The width can be anything greater than 0.

Solving this problem :

To solve this problem, let’s take a look at the following pattern :

3 3 3 3
2 2 2 2
1 1 1 1
2 2 2 2
3 3 3 3
  1. The height is 5.
  2. Width is 4
  3. It starts from 3, i.e. (height + 1)/2
  4. The first line is 3, the second line is 2..i.e. the value is decreased by 1 on each row.
  5. It will start incrementing from the _4_th line. Means, the midline is 3 or, (height +1)/2.
  6. From the fourth line, it will increase by 1 on each line.

Based on this observation, we can write one program that will print one similar pattern. Let’s take a look :

C program to print the pattern :

#include<stdio.h>

//4
void printPattern(int height,int width){
    int i,j;
    int mid;
    
    //5
    mid = (height + 1)/2;
    //6
    for(i=0; i<height; i++){
        //7
        for(j=0; j<width; j++){
            //8
            if(i<mid){
                printf("%d ",(mid-i));
            }else{
                printf("%d ",(i-mid+2));
            }
        }
        //9
        printf("\n");
    }
    
}

int main(){
    //1
    int height,width;
    
    //2
    printf("Enter the height : ");
    scanf("%d",&height);
    
    printf("Enter the width : ");
    scanf("%d",&width);
    
    //3
    if(height%2 == 0){
        printf("Please enter a valid height\n");
    }else if(width ==0){
        printf("Please enter a valid width\n");
    }else{
        printPattern(height,width);
    }
}

Explanation :

The commented numbers in the above program denote the step-number below :

  1. Create two integer variables height and width to store the height and width of the pattern.
  2. Ask the user to enter the height and width and save it in height and width variables.
  3. Check if the height and width are valid or not. If not, print one message to the user. Else, call printPattern method to print the pattern.
  4. printPattern method takes height and width as a parameter and it prints out the pattern for the given values.
  5. Find out the middle value and store it in mid variable.
  6. We are running two for loops. The outer one is for each line and the inner line is for printing each value for a line.
  7. The outer loop will run for height amount of time and the inner loop will run for width amount of time.
  8. Check if the current line is before the midline or not. Also print row values accordingly as discussed in the above example.
  9. Print one newline after each row.

Sample Output :

Enter the height : 5
Enter the width : 7
3 3 3 3 3 3 3
2 2 2 2 2 2 2
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3

Enter the height : 7
Enter the width : 4
4 4 4 4
3 3 3 3
2 2 2 2
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

Enter the height : 9
Enter the width : 1
5
4
3
2
1
2
3
4
5

Enter the height : 4
Enter the width : 3
Please enter a valid height