In this tutorial you will learn about the C++ Program to Print Number Pattern and its application with practical example.
C++ Program to Print Number Pattern
In this tutorial, we will learn to create a C++ program that will Print Number patterns using C++ programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators in C++ Programming.
- Basic Input and Output function in C++ Programming.
- Basic C++ programming.
- Basic arithmetic operations.
- For loop in C++ Programming.
Print Number Pattern:-
The C++ language is a very powerful programming language. In C++ programming we can perform many operations with the help of codings. The C++ language is very easy to create any pattern. In this program, we will learn to Print Number Patterns with the help below code.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
STEP 1: START STEP 2: first declare the variables STEP 3: Take input from number of rows from the user. STEP 4: then start the parent for loop STEP 5: now make the child for loop STEP 6: in this child loop print the blank spaces STEP 7: now use another child loop to start drawing<strong> Number Pattern</strong> STEP 8: Now this for loop will also execute under the parent for loop STEP 9: Print "\n" for changing the line before the increment of the main loop STEP 10: increment of the main for loop STEP 11: return the zero value for main function |
Program:-
To Print Number Pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> using namespace std; int main() { //declare the variables for the program int rw,i,j; //rw it will hold the integer value for the number of rows //i it will hold the integer value for the Parent for loop //i it will hold the integer value for the child for loop //taking input from the user number of rows for the pattern cout << "Enter number of rows: "; cin >> rw; //Making the pattern from the nested for loop for(i = 1; i <= rw; ++i) { for(j = 1; j <= i; ++j) { //printing the pattern on the output cout << j << " "; } cout << "\n"; } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- i = it will hold the integer value to control parent for loop.
- j = it will hold the integer value to control child for loop.
- rw = it will hold the integer value.
Taking the input rows from the users.
Code for the pattern of the number.
Printing the rows of patterns.