Technical Round Question (Pattern in C/C++)
In one of the recent interviews (Technical Round 2) of Maventic, the following design pattern was asked
In this article, I'm going to explain, how we can easily achieve this design pattern in C programming language
If you are familiar with the programming languages. Then this is very easy to understand this concept.
nowadays many companies asking this concept in the Technical rounds, so we need to clear this concept to get the place.
Question: Write a program to design the pattern based on n
example : n = 9
Solution
This is very easy to design, first of all, we need to analyse the pattern. This is the combination of * and blank space.
we only need to identify in which position we need to print * and don't worry about the blank spaces.
[ "-" represent a blank space][ and n = 9]
Here we can solve this in some steps
(i) this program is the shape of a matrix. so we need to use two for loops to solve this problem.example 1:
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(condition)
printf(" *");
else
printf(" ");//blank space
}
printf("\n");
}
(ii) ith 1st and n(9)th index jth 1st and n(9)th index as we can see here, each position we need to print '*'.
in the side loop condition, we can write it as below,
example 2:
if(i==1 || i==n || j==1 || j==n)
printf(" *");
else
printf(" ");
then output comes like the box.
(iii) now we need to draw like:
here we can see it's printing * where-where ith index and
jth index are equals except middle mean [(n/2)+1=5]
so we can write code for this:
example 3 :
if(i==j && j!=(n/2)+1)
printf(" *");
else
printf(" ");
(iv) Now last pattern, reverse of step(iii) pattern
so here we can analyse it's decreasing order of jth index
9,8,7,6,4,3,2,1 except middle means [n/2+1=5]
For this we can write the condition:-
example 4 :
if(j==(n+1)-i && j!=(n/2)+1)
printf(" *");
else
printf(" ");
In the above example, we declared some variation of pattern based on condition.
Now needs to add the condition as well
example 5 :
if(i==j && j!=(n/2)+1 || j==(n+1)-i && j!=(n/2)+1)
printf(" *");
else
printf(" ");
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter num: ");
scanf("%d",&n);
for( i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==1 || j==1 || i==n|| j==n || i==j && j!=(n/2)+1 || j==((n+1)-i) && j!=n/2+1)
{
printf(" *");
}
else
printf(" ");
}
printf("\n");
}
getch();
}
below is some output I have attached.
{
for(j=1;j<=n;j++)
{
if(condition)
printf(" *");
else
printf(" ");//blank space
}
printf("\n");
}
(ii) ith 1st and n(9)th index jth 1st and n(9)th index as we can see here, each position we need to print '*'.
in the side loop condition, we can write it as below,
example 2:
if(i==1 || i==n || j==1 || j==n)
printf(" *");
else
printf(" ");
then output comes like the box.
(iii) now we need to draw like:
here we can see it's printing * where-where ith index and
jth index are equals except middle mean [(n/2)+1=5]
so we can write code for this:
example 3 :
if(i==j && j!=(n/2)+1)
printf(" *");
else
printf(" ");
(iv) Now last pattern, reverse of step(iii) pattern
so here we can analyse it's decreasing order of jth index
9,8,7,6,4,3,2,1 except middle means [n/2+1=5]
For this we can write the condition:-
example 4 :
if(j==(n+1)-i && j!=(n/2)+1)
printf(" *");
else
printf(" ");
In the above example, we declared some variation of pattern based on condition.
Now we combined example3 and example4 to achieve like below
Now needs to add the condition as well
example 5 :
if(i==j && j!=(n/2)+1 || j==(n+1)-i && j!=(n/2)+1)
printf(" *");
else
printf(" ");
And finally, If we combined example 5 with example 2 then the output will be
Final output:Hence, below the final code I am writing
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter num: ");
scanf("%d",&n);
for( i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==1 || j==1 || i==n|| j==n || i==j && j!=(n/2)+1 || j==((n+1)-i) && j!=n/2+1)
{
printf(" *");
}
else
printf(" ");
}
printf("\n");
}
getch();
}
below is some output I have attached.
thank you
ReplyDeleteNice bro thank you
ReplyDeleteWelcome.... bro..
Deletethanx bro . . really gud work
ReplyDeleteYour most welcome bro...Thanks....for your Comments....
DeleteThank you
ReplyDeletemost welcome :)
Deletenyc work.. Can you giv me some more pattern question asked in maventic.. thnks in advn
ReplyDeleteexcllnt bro..
ReplyDeleteThanks ...
Deletecan u suggest us which type of questions we expect from maventic
ReplyDeleteMost probably in Maventic, they will ask pattern based question... and I am telling to one true thing of companies... mostly companies they ask logical based question... those question you can able to make using any programming language... so it's mean that ...only logical or pattern based question... becs they want to know who's logic is good... It's does not matter which language you are using..to do it... I hope it will help you...all the best...
DeleteGREAT BRO AND THANKS
ReplyDeletehow to write same code in java
ReplyDeleteThank you so much for giving me a such nice hint to make that type of pataran
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletereally so nice explanation
ReplyDeletereally so nice explanation
ReplyDelete
ReplyDeletepublic class Pattern {
public static void main(String[] args)
{
int no=15;
int n=(no+1)/2;
int k=no;
for(int i=1;i<=no;i++)
{
k--;
for(int j=1;j<=no;j++)
{
if(i==1 || i==no || j==1 || j==no || i==j || j==k+1)
{
if(i==n && (j!=1 && j!=no) )
System.out.print(" ");
else
System.out.print("*");
}
else
System.out.print(" ");
}
System.out.println();
}
}
}