Friday, April 3, 2020

PATTERN PRINTING IN C & C++






To print a pattern E of n rows we  print an E with n+1/2 "*" 's at 1st and last row Nd n-1/2 "*" 's at middle

for C++ the code goes here ----------------


#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"No of Rows : ";
cin>>n;
for(i=1;i<=n;i++)
{
if(i==1 || i==n)
{
for(j=1;j<=(n+1)/2;j++)
{
cout<<"*";
}
cout<<"\n";
}

    else if (i==((n+1)/2))
{
for(j=1;j<=(n-1)/2;j++)
{
cout<<"*";
}
cout<<"\n";
}
else
{
cout<<"*\n";
}

}

}


like this.....


if you run this code with n = 15 then no of row will be 15 ,at 1st line and 15 th line no of "*" will be 
15+1/2=8  and at the middle no of "*" will be 15-1/2 = 7 , like this 





similar code goes for  C 



#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n;
printf("No of Rows : ",n);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i==1 || i==n)
{
for(j=1;j<=(n+1)/2;j++)
{
printf("*");
}
printf("\n");
}
    else if (i==((n+1)/2))
{
for(j=1;j<=(n-1)/2;j++)
{
printf("*");
}
printf("\n");
}
else
{
printf("*\n");
}
}
}






Share on Whatsapp

No comments:

Post a Comment