0% found this document useful (0 votes)
12 views1 page

Usb 20

Vg

Uploaded by

Abishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Usb 20

Vg

Uploaded by

Abishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

GEEKSFORGEEKS

Programs to print Interesting


Patterns
Program to print the following pattern:

Examples :

Input : 5

Output:

* * * * * * * * * *

* * * * * * * *

* * * * * *

* * * *

* *

* *

* * * *

* * * * * *

* * * * * * * *

* * * * * * * * * *

Recommended: Please try your approach on


{IDE} first, before moving on to the solution.
This program is divided into four parts.

C++ C Java Python3 C# PHP Javascript

// C++ program to print


// the given pattern
#include<iostream>
using namespace std;

void pattern(int n)
{
int i, j;

// This is upper half of pattern


for(i = 1; i <= n; i++)
{
for(j = 1; j <= (2 * n); j++)
{

// Left part of pattern


if (i > (n - j + 1))
cout << " ";
else
cout << "*";

// Right part of pattern


if ((i + n) > j)
cout << " ";
else
cout << "*";
}
cout << endl ;
}

// This is lower half of pattern


for(i = 1; i <= n; i++)
{
for(j = 1; j <= (2 * n); j++)
{

// Right Part of pattern


if (i < j)
cout << " ";
else
cout << "*";

// Left Part of pattern


if (i <= ((2 * n) - j))
cout << " ";
else
cout << "*";
}
cout << endl;
}
}

// Driver Code
int main()
{
pattern(7);

return 0;
}
// This code is contributed by bunnyra

Output

* * * * * * * * * * * * * *

* * * * * * * * * * * *

* * * * * * * * * *

* * * * * * * *

* * * * * *

* * * *

* *

* *

* * * *

* * * * * *

* * * * * * * *

* * * * * * * * * *

* * * * * * * * * * * *

* * * * * * * * * * * * * *

Time Complexity: O(n2)


Auxiliary Space: O(1)

Program to print following pattern:

Examples :

Input : 5

Output:

* *

* * * *

* * * * * *

* * * * * * * *

* * * * * * * * * *

* * * * * * * * * *

* * * * * * * *

* * * * * *

* * * *

* *

Recommended: Please try your approach on


{IDE} first, before moving on to the solution.
This program is divided into four parts.

C++ C Java Python3 C# PHP Javascript

// C++ program to print the


// given pattern
#include <bits/stdc++.h>
using namespace std;

void pattern(int n)
{
int i, j;

// This is upper half of pattern


for (i = 1; i <= n; i++)
{
for (j = 1; j <= (2 * n); j++)
{
// Left part of pattern
if (i < j)
cout << " ";
else
cout << "*";

// Right part of pattern


if (i <= ((2 * n) - j))
cout << " ";
else
cout << "*";
}
cout << "\n";
}

// This is lower half of pattern


for (i = 1; i <= n; i++)
{
for (j = 1; j <= (2 * n); j++)
{
// Left part of pattern
if (i > (n - j + 1))
cout <<" ";
else
cout <<"*";

// Right part of pattern


if ((i + n) > j)
cout << " ";
else
cout << "*";
}
cout << "\n";
}
}
// Driver Code
int main()
{
pattern(7);
return 0;
}
// This code is contributed by shivani

Output

* *

* * * *

* * * * * *

* * * * * * * *

* * * * * * * * * *

* * * * * * * * * * * *

* * * * * * * * * * * * * *

* * * * * * * * * * * * * *

* * * * * * * * * * * *

* * * * * * * * * *

* * * * * * * *

* * * * * *

* * * *

* *

Time Complexity: O(n2)


Auxiliary Space: O(1)

Program to print the following pattern:

Examples:

Input : 9 [For Odd number]

Output:

\*******/

*\*****/*

**\***/**

***\*/***

****/****

***/*\***

**/***\**

*/*****\*

/*******\

Input : 8 [For Even number]

Output :

\******/

*\****/*

**\**/**

***\/***

***/\***

**/**\**

*/****\*

/******\

Recommended: Please try your


approach on {IDE} first, before
moving on to the solution.

Code implementation to print the given


pattern:

C++ Python3

// C++ program to print the given patt


#include <bits/stdc++.h>
using namespace std;

void pattern(int n)
{
// for traversing of rows
for (int i = 1; i <= n; i++) {
// for traversing of columns
for (int j = 1; j <= n; j++) {
// conditions for left-dia
// right-diagonal
if (i == j || i + j == (n
if (i + j == (n + 1))
cout << "/";
}
else {
cout << "\\";
}
}
else
cout << "*";
}
cout << endl;
}
}
// Driver Code
int main()
{
pattern(9);
return 0;
}
// This code is contributed by Nitin K

Output

\*******/

*\*****/*

**\***/**

***\*/***

****/****

***/*\***

**/***\**

*/*****\*

/*******\

Time Complexity: O(n2)


Auxiliary Space: O(1)

Program to print the following pattern:

Examples :

Input : 8

Output :

7 6 5 4 3 2 1 0

6 5 4 3 2 1 0

5 4 3 2 1 0

4 3 2 1 0

3 2 1 0

2 1 0

1 0

Recommended: Please try your


approach on {IDE} first, before
moving on to the solution.

Code implementation to print the given


pattern:

C++

// C++ program to print the given patt


#include <bits/stdc++.h>
using namespace std;

void pattern(int n)
{
// for traversing of rows
for (int i = 1; i <= n; i++) {
int k = n - i;
// for traversing of columns
for (int j = 1; j <= n; j++) {
if (j <= (n + 1) - i) {
cout << k << " ";
k--;
}
else {
cout << " ";
}
}
cout << endl;
}
}
// Driver Code
int main()
{
pattern(8);
return 0;
}
// This code is contributed by Nitin K

Output

7 6 5 4 3 2 1 0

6 5 4 3 2 1 0

5 4 3 2 1 0

4 3 2 1 0

3 2 1 0

2 1 0

1 0

Time Complexity: O(n2)


Auxiliary Space: O(1)

Program to print the following pattern :

Examples:

Input: 7

Output:

8 2

14 9 3

19 15 10 4

23 20 16 11 5

26 24 21 17 12 6

28 27 25 22 18 13 7

Recommended: Please try your


approach on {IDE} first, before
moving on to the solution.

Code implementation to print the given


pattern:

C++

// C++ program to print the given patt


#include <bits/stdc++.h>
using namespace std;

void pattern(int n)
{
int p, k = 1;
// for traversing of rows
for (int i = 1; i <= n; i++) {
p = k;
// for traversing of columns
for (int j = 1; j <= i; j++) {
cout << p << " ";
p = p - (n - i + j);
}
cout << endl;
k = k + 1 + (n - i);
}
}
// Driver Code
int main()
{
pattern(7);
return 0;
}
// This code is contributed by Nitin K

Output

8 2

14 9 3

19 15 10 4

23 20 16 11 5

26 24 21 17 12 6

28 27 25 22 18 13 7

Time Complexity: O(n2)


Auxiliary Space: O(1)

This article is contributed by Nitin Kumar. If


you like GeeksforGeeks and would like to
contribute, you can also write an article
using write.geeksforgeeks.org or mail your
article to [email protected].
See your article appearing on the
GeeksforGeeks main page and help other
Geeks.
Please write comments if you find anything
incorrect, or you want to share more
information about the topic discussed above.

Article Tags : C Language School Programming

pattern-printing

Recommended Articles
1. Programs to print Triangle and Diamond
patterns using recursion
2. Programs for printing pyramid patterns in
Python
3. Programs for printing pyramid patterns in Java
4. PHP programs for printing pyramid patterns
5. Programs for printing pyramid patterns using
recursion
6. Programs for printing pyramid patterns in C++
7. Program to print interesting pattern
8. Program to print solid and hollow square
patterns
9. Program to print solid and hollow rhombus
patterns
10. Program to print hollow rectangle or square
star patterns
11. Program to print right and left arrow patterns
12. Print di"erent star patterns in SQL
13. Program to print diagonal star patterns
14. Print Patterns in PL/SQL
15. Recursive program to print triangular
patterns
16. C Program to Print Floyd's Triangle Pyramid
Patterns
17. C/C++ Ternary Operator - Some Interesting
Observations
18. Interesting facts about switch statement in C
19. Interesting Facts about Macros and
Preprocessors in C
20. Some Interesting facts about default
arguments in C++
21. Interesting facts about strings in Python | Set
1
22. Interesting facts about strings in Python | Set
2 (Slicing)
23. C++ bitset interesting facts
24. Interesting Facts in C Programming
25. Interesting facts about data-types and
modifiers in C/C++

Read Full Article

A-143, 9th Floor, Sovereign Corporate Tower,


Sector- 136, Noida, Uttar Pradesh (201305)
[email protected]

Company

About Us
Careers
In Media
Contact Us
Privacy Policy
Copyright Policy
Advertise with us

Learn

DSA
Algorithms
Data Structures
SDE Cheat Sheet
Machine Learning
CS Subjects
Video Tutorials
Courses

NEWS

Top News
Technology
Work & Career
Business
Finance
Lifestyle
Knowledge

Languages

Python
Java
CPP
Golang
C#
SQL
Kotlin

Web Development

Web Tutorials

You might also like