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

DT
14 Jan, 2020 • 2 mins read min read • en
❤️ 0 💬 0 👁 1812
Pattern Problems in C++

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
☕

Support DT

Appreciate this story? Send a small tip to encourage the author to write more great work!

Please login to tip the author.

DT
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.

★ Recommended For You

Stories tailored to your recent reading preferences

More stories in Art
Xleem • 01 Jul, 2026 • 1 min read min

Life

Life is a long journey, and most of us spend it chasing what we lack, while forgetting to be grateful for what we already hold.

108 0
More stories in Art
Xleem • 01 Jul, 2026 • 1 min read min

Li3f

Just read it

98 1
More stories in Art
Adhiraj • 14 Oct, 2025 • 2 mins read min

अंतर्राष्ट्रीय स्कूल

कहानी का संदर्भ शिक्षा के बढ़ते व्यापारिक स्वरूप से है | किस प्रकार शिक्षा दान के स्थान पर व्यापार बन कर विद्यालयों की गुणवत्ता गिरती जा रही है, इस समस्या का जिक्र कहानी में व्यंगात्मक रूप में प्रस्तुत किया गया है |

268 1
More stories in Art
fazal Esaf • 05 Jul, 2025 • 1 min read min

"मदीना को लिखे गए ख़त"

हर जुमे की सुबह एक बूढ़ा आदमी अपने दिल के टुकड़े काग़ज़ पर रख देता था — मदीना के नाम।" ‘मदीना को लिखे गए ख़त’ एक रूहानी, मोहब्बत-भरी कहानी है, जो इबादत और इंतज़ार के धागों से बुनी गई है। गुलज़ार की नज़्मों की तरह नरम, और दिल को छू लेने वाली इस कहानी में, एक पोती अपने दादा के उन ख़ामोश ख़तों को पढ़ती है, जो मदीना को नहीं, बल्कि अल्लाह से की गई एक धीमी, टूटती बातचीत होते हैं। यह कहानी है उन अल्फ़ाज़ों की जो कभी भेजे नहीं जाते, उन दुआओं की जो जवाब नहीं मांगतीं—बस यकीन रखती हैं।

316 0