Cpp pattern program to print 13579 3579 579 79

Cpp pattern program to print 13579 3579 579 79

Pattern :
13579
3579
579
79
9

In this, we are going to write a Cpp program in which we are going to print the pattern in which we are going to print the odd numbers. If you observe the pattern carefully you will find that here in row one we have odd elements and as soon as we move to row two you will find that the first element of row one is removed and the rest of the elements are present in row two. And also you will find that as soon as we move to the next row the column is decreasing.

Examples-

Example 1-Input-Enter range of rows:4
Output-1 3 5 7
3 5 7
5 7
7

Example 2-Input-Enter range of rows:7
Output-1 3 5 7 9 11 13
3 5 7 9 11 13
5 7 9 11 13
7 9 11 13
9 11 13
11 13
13

let’s discuss how can we do this-

This is not a tricky program we have to only take a number of rows as input from the user then we will start For loop twice. The first and outer loops are for printing rows and the second and inner loops are to print columns the inner loop will be inside the outer loop. In the outer loop, we were printing rows in the inner loop we were printing columns of our pattern. In the inner loop, we will print our output of the program

using the formula 2*col-1 because we are decreasing our columns from up to down. After that in the outer for loop, we will apply the new line character ‘n’ after every iteration of the loop we have to be in a new line. Now the pattern will be ready.

Program-

#include<iostream>
using namespace std;
int main()
{
int num;
cout<<“Enter range of rows:”;
cin>>num;
for (int row = 1;row<=num;row++)
{

for(int col = row;col<=num;col++)
{
cout<<2*col – 1<<” “;
}
cout<<“n”;
}
return 0;
}

Output:

Input-Enter range of rows:5
Output-1 3 5 7 9
3 5 7 9
5 7 9
7 9
9

Program Explanation –

First of all, we have declared the variable row, col, and num .which we are going to use in the program.

  • Then we have taken a number of rows as input from the user.
  • Then we started first and outer For loop to print rows.
  • Then we started the second and inner for loops to print columns inside the outer loop.
  • Then we printed the output of the program in the inner loop.
  • In the end, we have applied a new line character ‘/n’ in the outer loop.

Conclusion –

In this program, we have learned how to print the pattern the pattern was very simple and easy once you understand the logic you can do a number of programs based on the pattern. We hope that you have understood the pattern and you will try to explore it even more!