Showing posts with label C Patterns. Show all posts
Showing posts with label C Patterns. Show all posts

Friday, April 3, 2020

C/C++ PATTERNS

Today we will see how to print square box type patterns (SOLID)


PROBLEM 1 : 

the code goes here

#include<iostream>
using namespace std;
int main()
{
int i,j,n=6;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<" "<<"*";
}
cout<<endl;
}
}




PROBLEM 2 : 




the code goes here



#include<iostream>
using namespace std;
int main()
{
int i,j,n=6;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<" "<<i;
}
cout<<endl;
}
}





PROBLEM 3 :  




the code goes here


#include<iostream>
using namespace std;
int main()
{
int i,j,n=6;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<" "<<j;
}
cout<<endl;
}
}






PROBLEM 4 : 



the code goes here



#include<iostream>
using namespace std;
int main()
{
int i,j,n=6;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<" "<<6-i;
}
cout<<endl;
}
}






PROBLEM 5 : 



the code goes here



#include<iostream>
using namespace std;
main()
{
char i,j;
for(i='A';i<='F';i++)
{
for(j='A';j<='F';j++)
{
cout<<" "<<char(i);
}
cout<<endl;
}
}







PROBLEM 8 : 



the code goes here


#include<iostream>
using namespace std;
main()
{
char i,j;
for(i='A';i<='F';i++)
{
for(j='A';j<='F';j++)
{
cout<<" "<<char(j);
}
cout<<endl;
}
}






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