Printing pattern in c++
Printing pattern in c++
#include<iostream>
using namespace std;
int patter_1()
{ // Logic i-1 n-(i-1)
// Pattern 1 n=5 i spaces stars
/* ***** 1 0 5
**** 2 1 4
*** 3 2 3
** 4 3 2
* 5 4 1
*/
int row;
cin>>row;
for (int i =1; i<= row; i++)
{
for (int j= 1; j<=row ; j++)
{
// stars
if (j>(i-1))
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
int patter_2()
{ // Logic 2*(i-1) n-(i-1)
// Pattern 2 n = 5 i spaces stars
/* ***** 1 0 5
**** 2 2 4
*** 3 4 3
** 4 6 2
* 5 8 1
*/
int n;
cin>>n;
for(int i= 1; i<=n;i++)
{
//spaces
for(int spaces = 1 ; spaces<=2*(i-1) ; spaces++)
cout<<" ";
//stars
for(int stars = 1 ; stars<= n- (i-1) ; stars++)
cout<<"* ";
cout<<endl;
}
return 0;
}
int pattern_4()
{/*
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
*/
int n;
cin>>n;
for(int i = 1; i<=n*2-1 ; i++)
{
for(int j =1; j<=n ; j++)
{
// lower spaces
if (i>n && j<= i-n)
cout<<" ";
// Upper spaces
else if (j<= (n-i))
cout<<" ";
//stars
else
cout<<"* ";
}
cout<<endl;
}
return 0;
}
int patter_5()
{/*
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
*/
int n;
cin>>n;
int centre = n/2+1;
for(int row = 1; row<= n; row++)
{
for(int column = 1; column<= n; column++)
{
// Lower stars
if (row>centre)
{
int a = row+centre;
int b = (row-centre)*2;
// lower left
if (column <= row-centre)
cout<<"* ";
// lower right
else if (column >= a-b)
cout<<"* ";
// lower gap
else
cout<<" ";
}
// Upper stars
else if ((column<= centre-row) || (column>= centre+row))
cout<<"* ";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
int pattern_9()
{ /*
*
* *
* *
* *
* * * * *
*/
int n =5;
cin>>n;
for(int i= 1; i<= n ; i++)
{
for(int j=1 ; j<= i; j++)
{
// horizonal vertical diagonal line stars
if ((i-j==0) || i==n || j==1 )
cout<<"* ";
// spaces
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
int pattern_10()
{ /*
*
* *
* *
* *
* * * * *
*/
int n ;
cin>>n;
for(int i = 1; i<= n; i++)
{
for (int j= 1 ; j <= n ; j++ )
{
if ((i+j == 6) || j==n || i==n )
cout<<"* ";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
int pattern_6()
{/*
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
*/
int n ;
cout<<"Enter number of rows"<<endl;
cin>>n;
for (int i = 1 , count = 1; i<= n ; i++ ,count = 1) // intializing count=1 after every row and increasing it by 1 when each print statement is running
{
for (int j = 1; j<= n*2-1 ; j++)
{
if ((j> n-i) && (j<n+i))
{
cout<<count<<" ";
count+=1;
}
else
{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
int pattern_7()
{/*
1
2 0 2
3 0 0 0 3
4 0 0 0 0 0 4
5 0 0 0 0 0 0 0 5
*/
int n ;
cout<<"Enter number of rows"<<endl;
cin>>n;
for (int i = 1 , count = 1; i<= n ; i++ , count++)
{
for (int j = 1; j<= n*2-1 ; j++)
{
if ((j> n-i) && (j<n+i))
{
// selecting starting of row to print numbers
if ((j==(n-i)+1) || (j==n+i-1))
cout<<count<<" ";
// between printing zeros
else
cout<<"0 ";
}
else
{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
int pattern_3()
{/*
*
* *
* * *
* * * *
* * * * *
*/
int n;
cin>>n;
for(int i = 1 ,con = 1; i<= n ; i++ , con = 1)
{
for (int j= 1; j<= n*2-1 ; j++ )
{
//star printing
if (j>n-i && j<n+i)
{
if (con%2 == 0) //if con even print space
{
cout<<" ";
}
else // else con odd print star
{
cout<<"* ";
}
con+=1;
}
//space printing
else
{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
int pattern_8()
{
/*
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
*/
int n;
cin>>n;
for(int i = 1 , count = 1; i<= n+1; i++ , count = 1)
{
// left side
for(int j = 1; j<= n+1 ; j++)
{
// print numbers
if (j>n-i+1)
{
if (j==10)
cout<<0;
else
cout<<count;
}
// print spaces
else
cout<<" ";
count+=1;
}
// right side
for (int j = 1 , right_count = 9 ; j<=i-1 ; j++ , right_count-=1)
{
// print numbers
cout<<right_count;
}
cout<<endl;
}
return 0;
}
int main()
{
patter_1();
patter_2();
pattern_3();
pattern_4();
patter_5();
pattern_6();
pattern_7();
pattern_8();
pattern_9();
pattern_10();
return 0;
}
Comments
Post a Comment