Pattern Problems in C++

The Basic C++ Approach to understand the Patterns. Patterns can be defined as an arrangement of elements in an particular structure

Originally published in en
Reactions 0
1179
DT
DT 14 Jan, 2020 | 2 mins read

Pattern

1

11

111

1001

11111

100001

1111111

10000001

Understanding the Pattern

1.      All the Rows which are “Odd” contains just 1

For example: Row 1, 3, 5, 7

2.      All the Rows which are “Even” contain 1 at the starting and 1 at the last and “Row No. (i) – 2” Zeroes

For example: Row 2, 4, 6, 8

3.      New Line changes each time

C++ Code

 

# include<iostream> //Header File

using namespace std; // Structure C++ commands

int main()

{

    int n; //Input No. of lines

    cin>>n;

    int i; //Declaring i for number of rows

    int j; //Declaring j

    for(i = 1 ;i <= n ; i++)

    {

        if (i%2!=0) //Checking value of "i" as "even" or "odd"

        {

            for(j =i ;j>0 ;j--) //"For" loop for printing 1 in odd rows

            {

                cout<<1 ;

            }

            cout<<endl;

        }

        else

        {

            cout<<1;

            for(j=i-2 ; j > 0 ; j--) // "For" Loop for printing 0 and 1 both in even rows

            {

                cout<<0;

            }

            cout<<1<<endl;

        }

    }   

 

}

 

Conclusion:

While working on any pattern problem analyze the code and write key points on it and re-arrange that points in form of code and written in particular language

 

0 likes

Published By

DT

DT

Comments

Appreciate the author by telling what you feel about the post 💓

Please Login or Create a free account to comment.