Thursday, August 14, 2014

Posted by Dollar Dhingra
Hello There, this is the first part of the series Pattern Programming Questions.
The First Part is specially for beginners who are new to programming and have basic understanding of loops and printing functions. In this part, I will walk you through some basic patterns.

Pattern Questions are not only the frequently asked questions in the interviews but also help you in developing the basic logic in programming. I will explain you some of the basic ones, and leave the rest for you to try.

Following patterns will be covered:

Pattern 1:

*****
*****
*****
*****


Pattern 2:

12345
12345
12345
12345

Pattern 3:

11111
22222
33333
44444

Pattern 4:

11111
22222
33333
44444

Pattern 5:

1
12
123
1234
12345

Pattern 6:

1
22
333
4444
55555

Pattern 7:

1
2   3
4   5   6
7   8   9   10
11 12 13 14 15


Pattern 1:
Lets start with the very basic one.

*****
*****
*****
*****

In the above  pattern there are 5 columns (left to right) and 4 rows(top to bottom).

We take rows as r and columns as c and we will use 2 loops for this, one for printing the rows, and other for columns.

 pseudocode----

r=5, c=5;
for(i=0;i<r;i++)
 {
    for(j=0;j<c;j++)
      {
         printf("*");//this prints star 5 times(c times)
      }
     printf("\n");//for coming to a new line after star is printed
 }

 the printf("\n") is executed 4 times hence 4 rows will be there.

 complete program:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<c;j++)  
     {  
       printf("*");//this prints star 5 times(c times)  
     }  
     printf("\n");//for coming to a new line after star is printed  
   }  
    return 0;  
  }  


 OUTPUT:
Enter number of rows and columns:
4 5

*****
*****
*****
*****


Pattern 2:

12345
12345
12345
12345

Now, we just have to make a small change to achieve this one:

Pseudo:
for(i=0;i<r;i++)
 {
    for(j=0;j<c;j++)
      {
         printf("%d", j+1);//printing the value of j+1 
      }
     printf("\n");
 }



 complete program:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<c;j++)  
     {  
       printf("%d", j+1);//this prints the value of j+1 i.e: 1 2 3 4 5   
     }  
     printf("\n");//for coming to a new line after star is printed  
   }  
    return 0;  
  }  



 OUTPUT:
Enter number of rows and columns:
4 5

12345
12345
12345
12345


Pattern 3:

11111
22222
33333
44444


You guessed it right ! We will now print the value of i in place of j

Pseudo:
for(i=0;i<r;i++)
 {
    for(j=0;j<c;j++)
      {
         printf("%d", i+1);//printing the value of j+1 
      }
     printf("\n");
 }


 complete program:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  #include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<c;j++)  
     {  
       printf("%d", i+1);   
     }  
     printf("\n");//for coming to a new line after star is printed  
   }  
    return 0;  
  }  

 OUTPUT:
Enter number of rows and columns:
4 5

11111
22222
33333
44444


Pattern 4:

*
**
***
****
*****


Now this is the last one that I will explain.

Firstly, there will be 2 for loops(for printing rows and columns)

Second thing to be noticed is that there are N number of stars in Nth row,
i.e. 1 star in 1st row
      2 stars in 2nd row
      3 stars in 3rd row and so on...


so, in the second for loop we have to restrict j (number of columns) till i (number of rows)

Pseudo:
for(i=0;i<r;i++)
 {
    for(j=0;j<=i;j++)//restricting the value of j till i
      {
         printf("%d", i+1); 
      }
     printf("\n");
 }



 complete program:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  #include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<=i;j++)  
     {  
       printf("*");   
     }  
     printf("\n");  
   }  
    return 0;  
  }  


 OUTPUT:
Enter number of rows and columns:
5 5

*
**
***
****
*****
Now you can try the following patterns, It is recommended that you minimize the the browser and try them before looking at the solutions.

 

Pattern 5:

1
12
123
1234
12345



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  #include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<=i;j++)  
     {  
       printf("%d", j+1);   
     }  
     printf("\n");  
   }  
    return 0;  
  }  



Pattern 6:

1
22
333
4444
55555


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  #include <stdio.h>  
  int main()  
  {  
   int r,c,i,j;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<=i;j++)  
     {  
       printf("%d", i+1);   
     }  
     printf("\n");  
   }  
    return 0;  
  }

 

Pattern 7:

1
2   3
4   5   6
7   8   9   10
11 12 13 14 15


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  #include <stdio.h>  
  int main()  
  {  
   int r,c,i,j, count=0;  
   printf("Enter number of rows and columns: \n");  
   scanf("%d %d",&r, &c);  
  for(i=0;i<r;i++)  
   {  
    for(j=0;j<=i;j++)  
     { count++; 
       printf("%3d", count); // "%3d" is used in printf for better indentation   
     }  
     printf("\n");  
   }  
    return 0;  
  }  


I will get back to you with some more complex patterns.


 Please share our blog and like our Facebook Page, This will help us grow. :)

1 comment: