Here I am sharing the C and C++ program to print the pattern shown in below image. This pattern is printed using stars (*). It has two parts, the lower half is just opposite of upper half. Comment below if you are facing any problem in understanding the code.
C Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include<stdio.h> int main() { int i,j,k,m,n; printf("Enter size(Ex:5):"); scanf("%d",&n); for(i=1,m=-1;i<=n;++i,m+=2) { if(i==1) for(j=1;j<n*2;++j) printf("*"); else { for(k=i;k<=n;++k) printf("*"); for(k=1;k<=m;++k) printf(" "); for(k=i;k<=n;++k) printf("*"); } printf("n"); } for(i=1,m=n*2-3;i<=n;++i,m-=2) { if(i==n) for(j=1;j<n*2;++j) printf("*"); else { for(k=1;k<=i;++k) printf("*"); for(k=1;k<=m;++k) printf(" "); for(k=1;k<=i;++k) printf("*"); } printf("n"); } return 0; } |
C++ Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include<iostream> using namespace std; int main() { int i,j,k,m,n; cout<<"Enter size(Ex:5):"; cin>>n; for(i=1,m=-1;i<=n;++i,m+=2) { if(i==1) for(j=1;j<n*2;++j) cout<<"*"; else { for(k=i;k<=n;++k) cout<<"*"; for(k=1;k<=m;++k) cout<<" "; for(k=i;k<=n;++k) cout<<"*"; } cout<<"n"; } for(i=1,m=n*2-3;i<=n;++i,m-=2) { if(i==n) for(j=1;j<n*2;++j) cout<<"*"; else { for(k=1;k<=i;++k) cout<<"*"; for(k=1;k<=m;++k) cout<<" "; for(k=1;k<=i;++k) cout<<"*"; } cout<<"n"; } return 0; } |