Friday, October 3, 2014

Posted by Dollar Dhingra
Hello there !! This is the second part of the Pattern Programming Questions Series.
I recommend you to check out the First Part if you are beginner.

Following Patterns Will be covered

Pattern 1

A
A B
A B C
A B C D
A B C D E

Pattern 2

1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1


Pattern 3

1 3 5 7 9
3 5 7 9
5 7 9
7 9
9

Pattern 4

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

Pattern 5

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

Pattern 6

    1
   232
  34543
 4567654
567898765



Solutions:

Before going through any of the solutions, I strongly recommend you all to first try it for yourself, because "you cannot learn logic, you can only develop it". Zyada ho gya :D :P


Pattern 1

A
A B
A B C
A B C D
A B C D E


The logic here is same as we discussed in many of the questions in Part 1 of this series.
The main difference here is - the letters. You can display letters instead of numbers easily.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
{ int i,j;

for(i='A'; i<='E'; i++)//outer loop runs from character A to character E
{
    for(j='A'; j<=i; j++)
    {
        printf("%c ",j);//%c prints the character value

    }
    printf("\n");
}
    return 0;
}

Notice two things here:
1. In line number 6, Outer loop runs from 'A' to 'E'
2. In line number 10, We are printing the character value of j by writing %c, i.e A, B and so on... If we would have written %d, 65 would have been printed in the place of A.

You can use the same idea in any of the patterns having Alphabetical letters.

Pattern 2

1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1

This pattern will have the reverse logic. i.e. the outer loop will run from 5 to 1.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
{ int i,j;

for(i=5; i>0; i--)//outer loop runs from 5 to 1
{
    for(j=1; j<=i; j++)
    {
        printf("%d ",j);

    }
    printf("\n");
}
    return 0;
} 

Pattern 3

1 3 5 7 9
3 5 7 9
5 7 9
7 9
9

 This one is a little tricky, but its a variation of the above.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
{ int i,j;

for(i=9; i>0; i=i-2)//outer loop runs from 9 to 1 with a decrement of 2
{
    for(j=1; j<=i; j=j+2)//increment of 2
    {
        printf("%d ",j);

    }
    printf("\n");
}
    return 0;
}

Pattern 4

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

Enough of simple ones containing 2 loops !
Just kidding ;) now this involves 3 loops. Why?

look closely





The red triangle represents the pattern of stars, and the purple triangle represents the pattern of blank spaces..
So we have 3 loops, one for purple triangle, one for red triangle, and an outer loop.


Also, we can imagine it as
0 spaces, 5 stars
1 spaces, 4 stars
2 spaces, 3 stars
3 spaces, 2 stars
4 spaces, 1 stars



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int main()
{ int i,j,k;

for(i=0; i<5; i++)//outer loop runs from 5 to 1
{
    for(j=0; j<i;j++)
    {
        printf(" ");//printing spaces
    }
    for(k=5;k>i; k--)
    {
        printf("*");//printing stars

    }
    printf("\n");
}
    return 0;
}


Pattern 5

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

Now comes 4 for loops :D but I guess you are comfortable now.. now think about this, how many patterns does it contains ??

This contains 3 patterns in it.





You can have a fair idea how excellent I am in Designing by looking at this picture :P

Look closely, we have 3 triangles now, red for spaces, purple for stars, and  green for more stars.
 









 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main()
{ int i,j,k,l;

for(i=0; i<5; i++)//outer loop runs from 5 to 1
{
    for(j=5; j>i;j--)
    {
        printf(" ");//printing spaces
    }
    for(k=0;k<=i; k++)
    {
        printf("*");//printing stars of the 2nd triangle

    }
    for(l=1;l<=i;l++)
    {
        printf("*");//printing stars in 3rd triangle

    }
    printf("\n");
}
    return 0;
}

Pattern 6

    1
   121
  12321
 1234321 
123454321 

If you have done the above questions properly then you can attempt this one on your own. It may take some time, but you will figure it out.


Imagine the above diagram again in the 3 triangles.





 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>

int main()
{ int i,j,k,l;

for(i=0; i<5; i++)//outer loop runs from 0 to 5(number of rows)
{
    for(j=4; j>i;j--)
    {
        printf("  ");//printing spaces
    }
    for(k=1;k<=i; k++)
    {
        printf(" %d", k);//printing numbers of the 2nd triangle

    }
    for(l=k;l>=1;l--)
    {
        printf(" %d", l);//printing numbers in 3rd triangle
    }
    printf("\n");
}
    return 0;
}



For any doubts, suggestions or corrections please type in the comments below.

It takes a lot of time and effort, please support us by sharing this and liking us on Facebook.
This will help us grow and will inspire us in growing. happy coding. :)

2 comments:

  1. Hi 46GeeksLab,

    Pattern 4,5 and 6 can be done using two loops or one loop only using if-else condition.

    Code for rendering

    Pattern 4 :

    for(i=0i<5;i++){
    for(j=0;j<5;j++{
    if(i>j) printf(" ");
    else printf("*");
    }
    }

    Using one loop (consider pattern as matrix of n*n then int(x/5) will be your i and x%5 will be your j) :

    for(i=0;i<5*5;i++){
    if(x%5==0 && ((int)x/5)!=0) printf("\n");
    if(((int)x/5)>x%5) printf(" ");
    else printf("*");
    }

    Similarly, pattern no. 5 and 6 will be done.

    -- M|KE A|_PHA.

    ReplyDelete
    Replies
    1. great work ! Mr. mike alpha || cameraman :D

      I just wanted to keep the logic similar for the programs so that its easy for the newbies to understand, and there are numerous ways to code the above patterns ;)

      Delete