Nothing Special   »   [go: up one dir, main page]

C Pattern

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1.

Write a Program to Print a Diamond Pattern

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

 C++

// C++ program to print


// Diamond shape
#include <iostream>
using namespace std;

void printDiamond(int n)
{
int space = n - 1;

for (int i = 0; i < n; i++) {


for (int j = 0; j < space; j++)
cout << " ";

// Print i+1 stars


for (int j = 0; j <= i; j++)
cout << "* ";

cout << endl;


space--;
}

space = 0;

// run loop (parent loop)


for (int i = n; i > 0; i--) {
for (int j = 0; j < space; j++)
cout << " ";

// Print i stars
for (int j = 0; j < i; j++)
cout << "* ";

cout << endl;


space++;
}
}

int main()
{
printDiamond(5);
return 0;
}

2. Write a Program to Print a Pyramid Pattern

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

 C++

// C++ Program to
// Print Pyramid pattern
#include <iostream>
using namespace std;

void pattern(int n)
{
int k = 2 * n - 2;

for (int i = 0; i < n; i++) {

for (int j = 0; j < k; j++)


cout << " ";

k = k - 1;
for (int j = 0; j <= i; j++) {
// Printing stars
cout << "* ";
}
cout << endl;
}
}

int main()
{
int n = 5;

pattern(n);
return 0;
}

3. Write a Program to Print the Hourglass Pattern

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

 C++

// C Program to print hourglass pattern


#include <iostream>
using namespace std;

// function to print hourglass pattern


void hourglass(int rows)
{

// first outer loop to iterate each row


for (int i = 0; i < 2 * rows - 1; i++) {

// assigning comparator
int comp;
if (i < rows) {
comp = 2 * i + 1;
}
else {
comp = 2 * (2 * rows - i) - 3;
}

// first inner loop to print leading spaces


for (int j = 0; j < comp; j++) {
cout << ' ';
}
// second inner loop to print star *
for (int k = 0; k < 2 * rows - comp; k++) {
cout << "* ";
}
cout << '\n';
}
}

int main()
{
hourglass(5);
return 0;
}

4. Write a Program to print an Inverted Pyramid


 C++

// C++ Program to print inverted pyramid


#include <iostream>
using namespace std;

int main()
{
int rows = 5;

for (int i = rows; i >= 1; i--) {


for (int j = rows; j > i; j--) {
cout << " ";
}
for (int k = 1; k <= (2 * i - 1); k++) {
cout << "*";
}
cout << endl;
}

return 0;
}

Output
*********
*******
*****
***
*

5. Write a Program to Print Floyd’s Triangle

1
2 3
4 5 6
7 8 9 10

 C++

// C Program to print the Floyd's Triangle


#include <stdio.h>

int main()
{
int rows = 4;
int n = 1;

// outer loop to print all rows


for (int i = 0; i < rows; i++) {

// innter loop to print abphabet in each row


for (int j = 0; j <= i; j++) {
printf("%d ", n++);
}
printf("\n");
}
return 0;
}

Write a Program to Print the Pascal Triangle

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

 C++
// C++ program to print
// Pascal’s Triangle
#include <iostream>
using namespace std;

void printPascal(int n)
{

int arr[n][n];

for (int line = 0; line < n; line++) {


// Every line has number of integers
// equal to line number
for (int i = 0; i <= line; i++) {
// First and last values in every row are 1
if (line == i || i == 0)
arr[line][i] = 1;
else
arr[line][i] = arr[line - 1][i - 1]
+ arr[line - 1][i];
cout << arr[line][i] << " ";
}
cout << "\n";
}
}

int main()
{
int n = 6;
printPascal(n);
return 0;
}

You might also like