0% found this document useful (0 votes)
13 views12 pages

Pattern Printing

The document contains code snippets for printing patterns like stars, numbers, letters and Pascal's triangle using loops in C++. The patterns include right angled triangles, diamonds, letters like A and numbers increasing or decreasing in the patterns.

Uploaded by

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

Pattern Printing

The document contains code snippets for printing patterns like stars, numbers, letters and Pascal's triangle using loops in C++. The patterns include right angled triangles, diamonds, letters like A and numbers increasing or decreasing in the patterns.

Uploaded by

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

*

**
***
****
#include <iostream>
using namespace std;
int main() {
int n, x=1;
cin>>n;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(j<=n-i){
cout<<" ";
}
else{
cout<<"*";
}
}
cout<<endl;
}
return 0;
}
****
****
****
****
#include <iostream>
using namespace std;
int main() {
int n, x=1;
cin>>n;
for(int i=1; i<=n; i++){
for(int j=1; j<=n*2-i; j++){
if(j<=n-i){
cout<<" ";
}
else{
cout<<"*";
}
}
cout<<endl;
}
return 0;
}
*
***
*****
*******
*********
***********
#include <iostream>
using namespace std;
int main() {
int n, x=1;
cin>>n;
for(int i=1; i<=n; i++){
for(int j=1; j<=n*2; j++){
if(j>=n-i+1 && j<=n+i-1 ){
cout<<"*";
}
else{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
1
321
54321
7654321
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=1; i<=n; i++){
int x=i*2-1;
for(int j=1; j<=n*2; j++){
if(j>=n-i+1 && j<=n+i-1){
cout<<x;
x--;
}
else{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
1
121
12321
1234321
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=1; i<=n; i++){
int x=1;
for(int j=1; j<=n*2-1; j++){
if(j<=n+i-1 && j>=n-i+1){
cout<<x;
if(j<n){
x++;
}
else{
x--;
}
}
else{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}

PASCALS TRIANGLE:

#include <iostream>
using namespace std;
int main() {
int num;

cin>>num;
for(int n=0; n<=num; n++){
int factn=1;
for(int i=n; i>1; i--){
factn=factn*i;
}

for(int k=num; k>=0; k--){


if(k<num-n){
cout<<" ";
}
else{

int factk=1;
for(int j=k; j>1; j--){
factk=factk*j;
}
int factnk=1;
for(int p=n-k; p>1; p--){
factnk=factnk*p;
}
int pascalsTriangle=factn/(factk*factnk);
cout<<pascalsTriangle<<" ";

}
cout<<endl;
}
return 0;
}

*******
*** ***
** **
* *
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=1; i<=n; i++){
for(int j=1; j<=n*2-1; j++){
if(j>n-i+1 && j<n+i-1 ){
cout<<" ";
}
else{
cout<<"*";
}
}
cout<<endl;
}
return 0;
}

A
ABA
ABCBA
ABCDCBA
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=1; i<=n; i++){
char ch=65;
for(int j=1; j<=n*2-1; j++){
if(j<=n+i-1 && j>=n-i+1){
cout<<ch;
if(j<=n/2+1)
ch++;
else
ch--;
}
else{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}

*
***
*****
*******
*****
***
*
#include <iostream>
using namespace std;
int main() {
int n, x=1;
cin>>n;
for(int i=1; i<=n*2-1; i++){
for(int j=1; j<=n*2-1; j++){
if(j<=n+x-1 && j>=n-x+1){
cout<<"*";

}
else{
cout<<" ";
}

}
if(i<n/2+1){
x++;
}
else{
x--;
}
cout<<endl;
}

return 0;
}
* *
* *
* *
* *
***
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if ( i!=n)
{
if (j==1 ||j==n)
cout<<"*";

else
cout<<" ";
}
else
{
if (j!=1 && j!=n)
cout<<"*";
else
cout<<" ";
}
}
cout<<endl;
}

return 0;
}

You might also like