Ticker

6/recent/ticker-posts

Advertisement

Basic Pattern Questions in C++ asked in Interview



Basic  Pattern  Coding  Questions  in  C++  
  
1)   Program  to  print  solid  and  hollow  rectangle  star  pattern  is  discussed  here. 
Given  the  number  of  rows  and  columns,  print  the  solid  and  hollow  rectangle  star  patterns.  
  
Solid  rectangular  star  pattern:  
  
Number  of  rows:  3  
Number  of  columns:  5  
  
* *  * *  *   
* *  *  *  *  
* *  * *   *  
 
  
Program  to  print  solid  rectangular  star  pattern  
  
/*  C++  program  to  print  solid  rectangle  star  pattern  */  
  
#include  <iostream>  
  using  namespace  std;  
  
/*  Function  to  print  solid  rectangle*/  
  void  solid_rectangle(int  n,  int  m)  
  
{  
  
int  i,  j;  
  
for  (i  =  1; i   <=  n;  i++)  
  
{  
  
for  (j  =  1; j   <=  m;  j++)  
  
{    cout  <<  “*”;  
  
}    cout  <<  endl;  
  
}    
}    int  main()  
  
{    int  rows,  columns;  
  cout  <<  “nEnter  the  number  of  rows :   “;  
  cin  >>  rows;    cout  <<  “nEnter  the  number  of  columns :   “;  
  cin  >>  columns;  
  cout  <<  endl;    solid_rectangle(rows,  columns); 
  return  0;  
  
}  
  
ii)  Hollow   rectangular  star  pattern:  
  
Number  of  rows:  3  
Number  of  columns:  5  
  
* *   *   *   *  
* *  
* *   *   *   *  
 
  
Program  to  print  hollow  rectangle  star  pattern  
  
/*  C++  program  to  print  hollow  rectangle  star  pattern  */  
  
#include  <iostream>  
  using  namespace  std;  
  
/*  Function  to  print  hollow  rectangle*/  
  void  hollow_rectangle(int  n,  int  m)  
  
{  
  
int  i,  j;  
  
for  (i  =  1; i   <=  n;  i++)  
  
{  
  
for  (j  =  1; j   <=  m;  j++)  
  
{    
if  (i==1  ||  i==n  ||  j==1  ||  j==m)  
  
cout  <<  “*”;    else    
cout  <<  ”  “;  
  
}    cout  <<  endl;  }    
}    int  main()  
  
{    int  rows,  columns;  
  cout  <<  “nEnter  the  number  of  rows :   “;  
  
cin  >>  rows;  
  cout  <<  “nEnter  the  number  of  columns  :  “;  
  cin  >>  columns;  
  cout  <<  endl;  
  hollow_rectangle(rows,  columns);  
  return  0;  
  
}  
  
  
2)  i)  Program   to  print  pyramid  pattern using   stars  or  Pyramid star   patterns  are  discussed  here.  For  any  given  number  n,  print  various  pyramid  star  patterns.  
  
Half  Pyramid  Star  Pattern  
  
*  
* *  
* *  *  
* *  *  *  
* * *   *  *  
* *  * *  *   *  
 
Program  to  print  half  pyramid  pattern  using  stars  
  
//  C++  program  to  print  half  pyramid  pattern  using  stars  
#include  <iostream>  using  namespace  std;  
  
int  main()  
{  
int  i,  j,n;  cin  >>  n;  for(i  =  0; i   <  n;  i++)  
{  
for(j  =  0; j   <=  i;  j++)  
{  
cout  <<  “*”;  
}  cout  <<  endl;  
}  return  0;  
}  
  
  
ii)  Inverted   Half  pyramid  
  
* *   *  *  * *   
* * *   *  *  
* * *  *   
* *  *  
* *  
*  
 
  
Program  to  print  inverted  half  pyramid  pattern  using  stars  
  
//  C++  program  to  print  inverted  half  pyramid  pattern  using  stars  
#include  <iostream>  using  namespace  std;  
  int  main()  
{  
int  i,  j,  n,  k  =  0;  cin  >>  n;  
for(i  =  n; i   >=  1;  –i)  
{  
for(j  =  1; j   <=  i;  ++j)  
{  cout  <<  “*  “;  
}  cout  <<  endl;  
}    return  0;  
}  
  
  
iii)  Full   Pyramid  Star  Pattern  
  
     *   
* *  
* *  *  
* *   *  *  
  
 *  
 
  
Program to   print  full  pyramid pattern   using  stars  
  
//  C++  program  to  print  full  pyramid  pattern  using stars   
#include  <iostream>  using  namespace  std;  
  int  main()  
{  
int  i,  j,  n,  k =   0;  cin  >> n;   
  
for(i  =  1; i   <=  n;  ++i,  k =   0)  
{  
for(j  =  1; j  <=   n  –  i;  ++j)  
{  
cout  <<  ” “;   
}  
  
while(k !=  2  * i-1)  
{  cout  <<  “*  “;  
++k;  
}  cout  <<  endl;  
}    return  0;  
}  
  
  
iv)  Inverted   Full  pyramid  
  
 
* *  *  *   
* * *   
* *   
      *   
 
  
Program  to  print  inverted  full  pyramid  pattern  using  stars  
  
//  C++  program  to  print  inverted  full  pyramid  pattern  using  stars  
#include  <iostream>  using  namespace  std;  
  
int  main()  
{  
int  i,  j,  n,  k  =  0;  cin  >>  n;    for(i=n;  i>=1;  –i)  
{  for(j=0; j   <  n-i;  ++j)  
{  cout  <<  ”  “;  
}    
for(j=i; j   <=  2*i-1;  ++j)  
{ cout  <<  “*  “;  
}    
for(j=0; j   <  i-1;  ++j)  
{  cout  <<  “*  “;  
}    cout  <<  endl;  
}    return  0;  
}  
  
  
v)  Hollow   Full  Pyramid  Star  Pattern  
  
   
*           *   *  
* *  
* *   
* * *  *  *   *  *  *  
 
  
Program  to  print  hollow  full  pyramid  pattern  using  stars  
  
#include  <iostream>  using  namespace  std;  void  printPattern(int);  int  main()  {  int  n;  cin  >>  n;  
int  i,  j,  k  =  0;  
for  (i  =  1; i   <=  n;  i++)  
{  
for  (j  =  i; j   <  n;  j++)  {  cout  <<  ”  “;  
}  
while  (k  !=  (2  * i   –  1))  {  
if  (k  == 0  ||  k ==  2  * i   –  2)  cout  <<  “*”;  
else  
cout  <<  ”  “;  
k++;  
;  }  k  =  0;  cout  <<  endl;  //  print  next  row  
}  
for  (i  =  0; i   <  2  *  n  –  1;  i++)  {  cout  <<  “*”;  
}  
}  
  
  
vi)  Inverted   Half  pyramid  
  
* *  *  *  *   *  
* *  
* *   
* *   
*  
 
  
Program  to  print  inverted  half  pyramid  pattern  
  
#include  <iostream>  using  namespace  std;  
  int  main()  {  int  n;  cin  >>  n;  
int  i,  j,  k  =  0;  for  (i  =  0; i   <  n;  i++)  
{  
cout  <<  “*  “;  
}  for(i=n;  i>=1;--i)  
{  
for(j=0; j   <  n-i;  ++j)  while  (k  !=  (2  * i   -  1))  {  if  (k  ==  0  ||  k  ==  2  * i   -  2)  cout  <<  “*”;  else  
cout  <<  ”  “;  
k++;    }  k  =  0;  cout  <<  endl;  //  print  next  row  
}    
}  
  
  
vii)  Inverted   Hollow  Full  Pyramid  
  
* *  *  * *  *   
* *  
* *   
     *  
 
  
Program  to  print  inverted  hollow  full  pyramid  
  
#include  <iostream>  using  namespace  std;  int  main()  {  int  n;  cin  >>  n;  
int  i,  j;  for  (i  =  1; i   <=  n;  i++)  {  for  (j  =  1; j   <  i;  j++)  {  cout  <<  ”  “;  
}  
  
for  (j  =  1; j   <=  (n  *  2  –  (2  * i   –  1));  j++)  {  
  
if  (i  ==  1  || j   ==  1  || j   ==  (n  *  2  –  (2  * i   –  1)))  {  
cout  << “*”; }  else  {  cout  <<  ”  “;  
}  }  cout  <<  endl;  
}  
}  
  
3)  i)  Program   to  print  pyramid  pattern  printing  using  numbers  or  Pyramid  number  patterns  is  discussed  here.  Given  a  number  n,  the  various  patterns  of  pyramids  using  numbers  are  produced  as  output.  
  
Half  pyramid  pattern  using  numbers  
  
1  
1  2  
1  2  3  
1  2  3  4  
1  2  3  4  5  
 
  
Program  to  print  half  pyramid  pattern  using  numbers  
  
//  C++  program  –  Half  Pyramid  Pattern  Printing  using  numbers  
#include  <iostream>  using  namespace  std;  
  int  main()  
{  
int  i,  j,n;  cin  >>  n;  for(i  =  1; i   <=  n;  i++)  
{  
for(j  =  1; j   <=  i;  j++)  
{  
cout  << j   <<  ”  “;  
}  cout  <<  endl;  
}  
return  0;  
}  
  
  
ii)  Inverted   half  pyramid  pattern  using  numbers  
  
1  2  3  4 5   
1  2  3  4  
1  2  3   
1  2  
1  
 
  
Program  to  print  inverted  half  pyramid  pattern  printing  using  numbers  
  
//  C++  program  –  Pyramid  Pattern  Printing  using  numbers  
#include  <iostream>  using  namespace  std;  
  
int  main()  
{  
int  i,  j,n;  cin  >>  n;  for(i  =  1; i   <=  n;  i++)  
{  
for(j  =  i; j   <=  n;  j++)  
{  
cout  << j   <<  ”  “;  
}  
cout  <<  endl;  
}  return  0;  
}  
  
  
iii)  Full   pyramid  pattern  using  numbers  
  
        1  
2 3  2  
3 4  5  4  3  
4 5  6  7  6  5  4  
5 6  7  8  9  8  7  6 5   
 
  
Program  to  print  full  pyramid  using  numbers  
  
#include  <iostream>  using  namespace  std;  int  main()  {       int  rows,  count  =  0,  count1  =  0,  k  =  0;       cout  <<  "Enter  number  of  rows:  ";       cin  >>  rows;       for(int i   =  1; i   <=  rows;  ++i)  
     {  
         for(int  space  =  1;  space  <=  rows-i;  ++space)  
         {               cout  <<  "   ";  
             ++count;  
         }           while(k  !=  2*i-1)  
         {               if  (count  <=  rows-1)  
             {                   cout  <<  i+k  <<  "  ";  
                 ++count;  
             }               else  
             {  
                 ++count1;                   cout  <<  i+k-2*count1  <<  "  ";  
             }  
             ++k;           }           count1  =  count  =  k  =  0;          cout  <<  endl;  
     }       return  0;  
}  
  
  
iv)  Hollow   half  pyramid  pattern  using  numbers  
  
1  
1  2  
1    3  
1      4  
1  2  3 4  5   
 
  
Program  to  print  hollow  half  pyramid  pattern  using  numbers  
  
/*  C++  program  print  hollow  half  pyramid  pattern  using  numbers  */  
#include  <iostream>  using  namespace  std;  
  int  main()  
{  
int  i,  j,  n,  k  =  0;  cin  >>  n;  for(i  =  1; i   <=  n;  i++)  
{  
for(j  =  1; j   <=  i;  j++)  
{  
if  (j  ==  1  || j   == i   || i   ==  n)  cout  <<  j;  else  
cout  <<  ”  “;  
}  
cout  <<  endl;  
}  return  0;  
}  
  
  
v)  Hollow   Inverted  Half  Pyramid  Pattern  using  numbers  
  
1 2  3  4  5  
2 5  
3 5  
4 5  
5  
 
  
Program  to  print  hollow  inverted  half  pyramid  pattern  using  numbers  
  
/*  C++  program  to  print  hollow  inverted  half  pyramid  pattern  using  numbers  */  
#include  <iostream>  using  namespace  std;  
  
int  main()  
{  
int  i,  j,  n,  k  =  0;  cin  >>  n;  for(i  =  1; i   <=  n;  i++)  
{  
for(j  =  i; j   <=  n;  j++)  
{  
if  (i  ==  1  || j   == i   || j   ==  n)  cout  <<  j;  else  
cout  <<  ”  “;  
}  cout  <<  endl;  
}  return  0;  
}  
  
  
vi)  Hollow   Full  Pyramid  Pattern  using  numbers  
  
   1   
   1 2   
  1   3   
1 2   3  4  
 
  
Program  to  print  hollow  full  pyramid  pattern  using  numbers  
  
/*  C++  program  to  print  hollow  full  pyramid  pattern  using  numbers  */  
#include  <iostream>  using  namespace  std;  
  int  main()  
{  
int  i,  j,  n;  
cout  <<  “Enter  value  of  n  :  “;  cin  >>  n;  
for(i  =  1; i   <=  n;  i++)  
{  
for(j  =  i; j   <  n;  j++)  
{  cout  <<  ”  “;  
}  
for(j  =  1; j   <=  i;  j++)  
{  
if(j  ==  1  || i   ==  n)  
{  
cout  << j   <<  ”  “;  
}  else  {  cout  <<  ”  “;  
}  
}  
for(j  =  1; j   <  i;  j++)  
{  
if(j  ==  i-1  && j   <  n-1)  
{  
cout  <<  j+1;  
}  else  {  
cout  <<  ”  “;  
}  }  
cout  <<  endl;  
}  return  0;  
}  
  
  
4)  i)  Program   to  print  palindrome  pyramid  pattern  using  numbers  is  discussed  here.  Given  a  number  n,  the  task  is  to  print  a  palindrome  pyramid  containing  n  number  of  rows.  
  
Palindrome  half  pyramid  pattern  using  numbers  
  
Input:  
5  (number  of  rows)  
  
Output:  
1  
1   2   1  
1   2   3   2   1  
1   2   3   4   3   2   1  
1   2   3   4   5   4   3   2   1  
  
Program  to  print  palindrome  pyramid  pattern  using  numbers  
  
/*  C++  program  for  Palindrome  pyramid  pattern  printing  using  numbers  */  
#include<iostream>  #include<stdlib.h>  using  namespace  std;  int  main()  
{  
int  i,j,k,l,n;  cout  <<  “Enter  the  number  of  rows  :  “;  cin  >>  n;  for(i  =  1; i   <=  n;  i++)  
{  for(k  =  1;  k  <=  i;  k++)  
{  cout  <<  k  <<  ”  “;  }  
for(l  =  i-1; l   >=  1;  l–)  
{  
cout  << l   <<  ”  “;  
}  cout  <<  endl;  
}  return  0;  
}  
  
  
ii)  Palindrome   half  pyramid  pattern  using  alphabets  
  
Input:  
5  (number  of  rows)  
  
Output:  
A  
A   B   A  
A   B   C   B   A  
A   B   C   D   C   B   A  
A   B   C   D   E   D   C   B   A  
  
Program  to  print  palindrome  pyramid  pattern  using  alphabets  
  
/*  C++  program  for  Palindrome  pyramid  pattern  printing  using  alphabets  */  
#include<iostream>  #include<stdlib.h>  using  namespace  std;  int  main()  {  
int  i,j,k,l,n;  cout  <<  “Enter  the  number  of  rows  :  “;  cin  >>  n;  
for(i  =  1; i   <=  n;  i++)  
{  for(k  =  1;  k  <=  i;  k++)  
{  cout  <<  char(k+65-1)  <<  ”  “;  
}  
for(l  =  i-1; l   >=  1;  l–)  
{  cout  <<  char(l+65-1)  <<  ”  “;  
}  cout  <<  endl;  
}  return  0;  
}  
  
  
iii)  Palindrome   pyramid  pattern  using  numbers  
  
Input:  
5(number  of  rows)  
  
Output:  
  
           1  
       1   2   1  
     1   2   3   2   1  
   1   2   3   4   3   2   1  
1   2   3   4   5   4   3   2   1  
  
Program  to  print  palindrome  pyramid  pattern  using  numbers  
  
#include  <iostream>  using  namespace  std;  int  main()  {  
int  i,  j,  k,  space,  n;  cin  >>  n;  cout  <<  ”  “;  for  (i=1;  i<=n;  i++)  
{    for  (j=1;  j<=n-i;  j++)  cout  <<  ”  “;  
  for  (j=1,k=2*i-1;  j<=2*i-1;  j++,k–)  
{  
if  (j  <=  k)  cout  <<  j;  else  cout  <<  k;  
}  
cout  <<  endl;  
  
cout  <<  ”  “;  
  }  return  0;  
}  
  
  
iv)  Palindrome   pyramid  pattern  using  numbers  and  stars  
  
Input:  
7  
  
Output:  
*******1********  
*******2*2*******  
******3*3*3******  
*****4*4*4*4*****  
****5*5*5*5*5****  
***6*6*6*6*6*6***  
  
Program  to  print  palindrome  pyramid  pattern  using  numbers  &  stars  
  
#include<iostream>  using  namespace  std;  int  main()  {  int  n,  i,  j,  space,  count  =  1,  num  =  0,  star  =  8;  cin  >>  n;  space  =  n;  
for  (i  =  1; i   <=  n;  i++)  
{  
for  (j  =  1; j   <=  star;  j++)  if(i  + j   <=  star  +  1)  cout  <<  “*”;  
num++;  
for  (j  =  1; j   <=  i;  j++)  
{  cout  <<  num;  
if  (i  >  1  &&  count  <  i)  
{  cout  <<  “*”;  count++;  
}  
}  
for  (j  =  1; j   <=  star;  j++)  if(i  +  n  <= j   +  n)  cout  <<  “*”;  cout  <<  “\n”;  space–;  
count  =  1;  }  return  0;  
}  
  
  
5)  i)  Program   to  print  diamond  star  patterns  is  discussed  here.  Given  a  number  n,  write  a  program  to  print  a  diamond  shape  with  2*n  rows.  
  
Solid  Diamond  pattern  printing  using  stars  
  
Input:  5    
Output:  
     *  
* *    
* *   *   
* *   *  *       *   *   *  *    *  
* *   *   *  
* *   *   
* *   
     *  
 
  
Program  for  solid  diamond  pattern  using  stars  
  
/*  C++  program  –  hollow  diamond  pattern  printing  using  stars  */  
  
#include  <iostream>  using  namespace  std;  
  int  main()  
{  
int  i,  j,  space,  k  =  0,  n;  cout  <<  “\nEnter  the  number  of  rows  :  “;  cin  >>  n;  for  (i  =  1; i   <=  n;  i++)  
{  
for  (j  =  1; j   <=  n  –  i;  j++)  
{  cout  <<  ”  “;  }  
while  (k  !=  (2  * i   –  1))  
{  
if  (k  ==  0  or  k  ==  2  * i   –  2)  
cout  <<  “*”;   
else   
cout  <<  ”  “;  
k++;  
}  k  =  0;  cout  <<  endl;  
}  n–;  
for  (i  =  n; i   >=  1;  i–)  
{  
for  (j  =  0; j   <=  n  –  i;  j++)  
{  cout  <<  ”  “;  
}  k  =  0;  while  (k  !=  (2  * i   –  1))  
{  
if  (k  ==  0  or  k  ==  2  * i   –  2)  cout  <<  “*”;  
else  
cout  <<  ”  “;  
k++;  
}  cout  <<  endl;  
}  
}  
  
  
ii)  Solid   Half  Diamond  pattern  printing  using  stars  
  
Input: 5  
  
Output:  
*  
* *  
* *  *   
* * *  *   
* * *  *  *    
* * *   *  
* *  *   
* *  
*  
 
  
Program  for  solid  half  diamond  pattern  printing  using  stars  
  
/*  C++  program  –  solid  half  diamond  pattern  printing  using  stars  */    
#include  <iostream>  using  namespace  std;  
  int  main()  
{  
int  i,  j,  space,  k  =  0,  n;  cout  <<  “\nEnter  the  number  of  rows  :  “;  cin  >>  n;  for(int  i=1;i<=n;i++)  
{  for(int  j=1;j<=n-i;j++)  
{  cout  <<  ”  “;  
}  for(int  j=1;j<=i;j++)  
{  cout  <<  “*”;  
}  cout  <<  endl;  
}  
  for(int  i=n-1;i>0;i–)  
{  for(int  j=1;j<=n-i;j++)  
{  cout  <<  ”  “;  
}  for(int  j=1;j<=i;j++)  
{  cout  <<  “*”;  
}  cout  <<  endl;  
}  
}  
  
  
iii)  Half   Diamond  pattern  using  stars  and  numbers  in  a  palindromic  pattern 
  
Input:  3  (Number  of  rows)  
  
Output:  
*  
* 1  *  
* 1   2  1  *  
* 1 2   3  2  1  *  
* 1  2  1  *  
* 1  *  
*  
 
  
Program  for  palindromic  pattern  printing  in  a  half  diamond  
  
#include  <iostream>  using  namespace  std;  
  int  main()  
{  
int  i,  j,  N;  
cout  <<  “Enter  the  number  of  rows  :  “;  cin  >>  N;  cout  <<  “*\n”;  for(i=1;  i<=N;  i++)  
{  for(j=1;  j<=i;  j++)  
{  
if(j  ==  1)  
cout  <<  “*”;  cout  <<  j;  
}  for(j=i-1;  j>=1;  j–)  {   
cout  <<  j;  
}  cout  <<  “*”;  cout  <<  “\n”;  
}  
  for(i=N-1;  i>=1;  i–)  
{  for(j=1;  j<=i;  j++)  
{  
if(j  ==  1)  
cout  <<  “*”;   cout  <<  j;  
}  for(j=i-1;  j>=1;  j–)  
{  cout  << j  ;   
}  cout  <<  “*”;  cout  <<  “\n”;  
}  cout  <<  “*\n”;  return  0;  
}  
  
6)  i)  Half   diamond  pattern  printing  using  numbers  
  
Input:  
3  4  
  
Output:  
3  
44  
555  
6666  
555  
44  
3  
  
Program  for  half  diamond  pattern  printing  using  numbers  
  
/*  C++  program  for  diamond  pattern  printing  using  numbers  */  
  
#include  <iostream>  
  using  namespace  std;  
  int  main()  
  
{    int  i,j,s,N,count=0;  
  cin  >>  s  >>  N;  
  for(i=s;count<4;count++)  
  
{    for(j=0;j<count+1;j++)  
  
cout  <<  i;  
  cout  <<  endl;  
  i=i+1;  
  
}    for(i=s+N-2;count>0;count–)  
  
{    for(j=0;j<count-1;j++)  
  cout  <<  i;  cout  <<  endl;  
  i=i-1;    
}    return  0;  
  
}  
  
ii)  Half   diamond  using  numbers  and  stars  
  
Input  :  
4  
  
Output:  
1  
2*2  
3*3*3  
4*4*4*4  
4*4*4*4  
3*3*3  
2*2  
1  
  
Program  for  half  diamond  pattern  printing  using  numbers  and  stars  
  
/*  C+  program  for  diamond  pattern  printing  using  numbers  and  stars  */    
#include  <iostream>  
  using  namespace  std;    int  main()    
{    int  i,j,k,N,count=0;    cin  >>  N;    for(i=1;i<=N;i++)    
{    k=1;    for(j=0;j<i;j++)    
{    cout  <<  i;    if(k<i)    
{    cout  <<  “*”;    k=k+1;  
  
}  
  
}    cout  <<  endl;    
}    for(i=N;i>0;i–)    
{    k=1;    for(j=0;j<i;j++)  {    
cout  <<  i;    if(k<i)  
  
{    
cout  <<  “*”;  
  k=k+1;  
  
}    
}    cout  <<  endl;  
  
}    return  0;  
  
}  
  
iii)  Half   diamond  pattern  using  numbers  and  stars  
  
Input:  
4  
  
Output:  
1  
2*3  
4*5*6  
7*8*9*10  
7*8*9*10  
4*5*6  
2*3  
1  
  
Program  to  print  diamond  pattern  using  numbers  and  stars  
  
  
/*  C++  program  for  diamond  pattern  printing  using  numbers  and  stars  */    
#include  <iostream>  
  using  namespace  std;  
  int  main()  
  
{    int  i,j,count=1,n;  
  cout  <<  “Enter  a  numbern”;  
  
cin  >>  n;    for(i=1;i<=n;i++)  
  
{    for(j=1;j<=i;j++)  
  
{    
if(j<i)    cout  <<  count++  <<  “*”;  
  else    cout  <<  count++;  
  
}    cout  <<  endl;  }    count=count-n;  
  for(i=n;i>=1;i–)  
  
{  for(j=1;j<=i;j++)  
  
{    
if(j<i)    cout  <<  count++  <<  “*”;  
  else    cout  <<  count++;  
  
}    count=(count+1)-2*i;  
  cout  <<  endl;  
  
}    return  0;  
  
}  
  
  
7)Program   to  print  Pascal's  triangle  is discussed   here.  Pascals  triangle  is  a  triangular  array  of  the  binomial  coefficients.  
The  numbers  outside  Pascal's  triangle  are  all  "0".  These  "0s"  are  very  important  for  the  triangular  pattern  to  work  to  form  a  triangular  array.  The  triangle  starts  with  a  number  "1"  above,  and  any  new  number  added  below  the  upper  number  "1"  is  just  the  sum  of  the  two  numbers  above,  except  for  the  edge,  which  is  all  "1".  
   
row  0  =1  row  1  =  (0+1),  (1+0)  =  1,  1  row  2  =  (0+1),  (1+1),  (1+0)  =  1,  2,  1  row  3  =  (0+1),  (1+2),  (2+1),  (1+0)  =  1,  3,  3,  1  row  4  =  (0+1),  (1+3),  (3+3),  (3+1),  (1+0)  =  1,  4,  6,  4,  1  row  5  =  (0+1),  (1+4),  (4+6),  (6+4),  (4+1),(1+0)  =  1,  5,  10,  10,  5,  1  row  6  =  (0+1),  (1+5),  (5+10),  (10+10),  (10+5),  (5+1),  (1+0)  =  1,  6,  15,  20,  15,  6,  1    
//   C++  program  to  print  pascal’s  triangle  
  #include  using  namespace  std;  int  main()  
{  int  rows,  coef  =  1,  space,  i,  j;  cout  <<  “\nEnter  the  number  of  rows  :  “;  cin  >>  rows;  cout  <<  endl;  
  for(i=0;  i<rows;  i++)  { for(space=1;  space  <=  rows-i;  space++)  
cout  <<  ”  ”  ;  
  
for(j=0; j   <=  i;  j++)  
{  if  (j==0  ||  i==0)  coef  =  1;  else  
coef  =  coef*(i-j+1)/j;  
  
cout  <<  coef  <<  ”  “;  
}  cout  <<  endl;  cout  <<  endl;  
}    return  0;  
}  
  
  
Time  complexity:O(n^2)   
  
8) Butterfly   Pattern  Printing  
  
Input:  5  Output:  
   
Solution  for  Butterfly  Pattern:  
#include  <iostream>  using  namespace  std;   int  main()  {  
     int  i,  j,  n;       cin  >>  n;  
     //  upper  half  of  the  pattern       for(i  =  0;  i <   n;  i++)  
     {           for(j  =  0;  j <   (2  *  n);  j++)  
         {               if(i  >=  j)  //  upper  left  triangle                   cout  <<  "*";               else                   cout  <<  "  ";               if(i  >=  (2  * n   -  1)  -  j)   //  upper  right  triangle                   cout  <<  "*";               else                   cout  <<  "  ";  
         }           cout  <<  "\n";  
     }  
     //  bottom  half  of  the  pattern       for(i  =  0; i  <   n;  i++)  
    {            for(j =   0; j   <  (2  *  n);  j++)  
        {                if(i  + j   <= n  -   1)   //  bottom  left  triangle                   cout  <<  "*";               else                   cout  <<  "  ";  
 
             if((i  +  n)  <=  j)   //  bottom  right  triangle                   cout  <<  "*";               else                   cout  << "   ";  
         }           cout  <<  "\n";  
     }       return  0;  
}  
  
  
  
  

Post a Comment

0 Comments