C/C++ Program to Print Following Pattern

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

#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

#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;
 
}

C/C++ Program to Print Following Pattern, Pattern printing program, pyramid program, print triangle.

Leave a Comment

Your email address will not be published. Required fields are marked *