Simple Staircase Number Pattern in C using for-loops

Reading Time: 4 minutes

Today we are going to learn how to write a C program to print the following number pattern into the console. As usual, I will explain the code line by line. So let’s see how to print a simple staircase number pattern in C using for-loops

This is the number pattern that we will be focusing on for today’s tutorial.

What are we going to achieve?

After we input a number (for example 5), we will be able to get the following final output.

Full code

#include <stdio.h>

int main() {
    int num = 0;
    printf("Enter the maximum no. of rows for the pattern.\n");
    scanf("%d", &num);

    for (int i=1; i<=num; i++){
        for (int j=i; j<=i && j>0; j--){
            printf("%d", j);
        }
        printf("\n");
    }
    return 0;
}

Code Explanation

If you need explanations for Lines 1, 3 also, please refer to my article https://www.rukbook.com/c-program-to-create-a-simple-calculator-explained/ where they are covered.

Line 4
int num = 0;

In the line 4, we have declared and initialized a variable num. Declaring is just telling C what data type and the name of the variable is. Initializing is assigning a value to that variable; in this case zero.

Line 5
printf("Enter the maximum no. of rows for the pattern.\n");

This line simply prints a sentence to the console so that the user can see what to do next. The text asks the user to input the maximum number of rows the number pattern should have.

Line 6
scanf("%d", &num);

This line is responsible for getting the input entered by the user, and assigning that value to the variable num. Using & sign before the variable name num, gets the address of the variable num. So, the value input by the user is sent to the address of the variable num.

Lines 8 to 13
for (int i=1; i<=num; i++){
	for (int j=i; j<=i && j>0; j--){
		printf("%d", j);
	}
	printf("\n");
}

These lines contain a nested for-loop. The term nested refers to the fact that one of the for-loops is present inside the other. This allows the inner for-loop to be completely executed for each iteration of the outer for-loop. For example, if we enter the number of rows as 3 for the input, the for loops will be executed as follows.

For-loop basics at a glance

An Iteration can be thought of as a step of a loop. (Not just a for-loop, but also any other loop like a while-loop).

When consider a for-loop, there are three parts in the code (within the bracket in line 8). Those are,

  • Initialization
  • Condition
  • Increment (or decrement)
initialization in Outer for-loop

The initialization part sets the initial value of the variable for which the for-loop is going to be performed. Note that the initialization is only done once. So here, the initial value of the variable i in the outer for-loop is set to 1.

Condition in Outer for-loop

The for-loop is only executed if the condition is true in each iteration.

Increment (or decrement) in Outer for-loop

The increment (or decrement) part is executed after executing all the lines inside the for-loop. So during the first iteration of the outer for-loop, the value of i is 1 (it becomes 2 after the first iteration).

In the second (inner) for-loop, the three parts can be found.

initialization in Inner for-loop

The initialization part initializes a variable j to value of the variable i. This is because we want to print the numbers of each line from the biggest to the smallest. So we need to start with the maximum value for j, which is the value of i for each line.

Condition in Inner for-loop

In this inner for-loop, the condition part is a little different. We know a conditional expression evaluates to either true or false. That is, 1 or 0. (1 is true, 0 is false). Here, our condition for the inner for-loop is j<=i && j>0. There are two sub-expressions in this conditional expression. These sub-expressions are joined by the && operator, which is the logical AND operator. (note the double ampersand signs).

The first sub-expression checks whether j is equal to or less than i. We do this because we want to have same no. of numbers as the number of the row. For example, one number in row one, two numbers in row two etc.

The second sub-expression checks whether j is greater than 0. This is to end printing numbers once the required numbers for each row are printed. For example, if we do not use this second sub-expression, the program will enter an infinite loop, because then there will no condition to stop the inner for-loop.

Infinite loops should never be allowed in a program. If the program enters an infinite loop, the whole process and the running system temporarily breakdown due to overloading the memory.

Increment (Or Decrement) in Inner for-loop

In the increment (or decrement) part of the inner for-loop, j– can be found. This means, after each iteration of the inner for-loop, the value of j is decreased by one.

Notes
  • In a for-loop statement (inside parenthesis), the postfixes and prefixes have the same effect. So it does not matter whether you use j– or –j here.
  • The inner for-loop prints one number in one iteration (along a row).
  • The outer for-loop prints a whole row in one iteration.

Conclusion

So that’s how to print the simple staircase number pattern in C using for-loops. I hope you enjoyed reading this and why not share this article with your friends and connections by clicking on your favourite social media platform so that others can read this as well?

4.1 8 votes
Article Rating

Leave a comment. We love your feedback.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Pabasara Rathnayake
Pabasara Rathnayake
4 months ago

Next.. next…

2
0
Would love your thoughts, please comment.x
()
x