10 challenging char pattern programs

16
Write a C program to print the following pattern: 1. A B A C B A D C B A E D C B A Sol: #include <stdio.h> int main() { char prnt = 'A'; int i, j; for (i = 1; i <= 5; i++) { // Prints the right angle triangle for (j = 1; j <= i; j++) { printf("%2c", ((prnt + (i - j)))); // Prints the characters in the required order } printf("\n"); } return 0; } 2. A B C D E B C D E C D E D E E

Upload: nitin-singhal

Post on 16-Oct-2014

29 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 10 Challenging Char Pattern Programs

Write a C program to print the following pattern:1.

A

B A

C B A

D C B A

E D C B A

Sol:#include <stdio.h> int main() {    char prnt = 'A';    int i, j;    for (i = 1; i <= 5; i++) { // Prints the right angle triangle        for (j = 1; j <= i; j++) {            printf("%2c", ((prnt + (i - j))));  // Prints the characters in the required order        }        printf("\n");    }    return 0;}

2. A B C D E

B C D E

C D E

D E

E

#include<stdio.h>int main() {    int i, j;

Page 2: 10 Challenging Char Pattern Programs

    char c = 'A';    for (i = 0; i < 5; i++) {        for (j = 0; j < 5; j++) {            if (j < i) {                printf(" ");            } else {                printf("%c", c + j);  // The order of the alphabets are increasing in terms of j            }            printf(" ");        }        printf("\n");    }    return 0;}

3. A

a a

B B B

b b b b

C C C C C

c c c c

D D D

d d

E

Sol: #include<stdio.h>int main() {    char prnt_even = 'a';    char prnt_odd = 'A';    int i, j, s, nos = 4,line=0;  //line tracks if the row is odd or even    for (i = 1; i <= 9; (i = i + 2)) {        for (s = nos; s >= 1; s--) {  // For the required spacings            printf("  ");        }        for (j = 1; j <= i; j++) {

Page 3: 10 Challenging Char Pattern Programs

            if(line==0){                if ((i % 2) != 0 && (j % 2) != 0) {                    printf("%2c", prnt_odd);   // Prints capital letter if l=0                    } else {                        printf("  ");                    }                }                else{                    if ((i % 2) != 0 && (j % 2) != 0) {                        printf("%2c", prnt_even);  //else prints a small letter                        } else {                            printf("  ");                        }                    }                }/* After one column the value of line is interchanged and prnt_even or prnt_odd is incremented accordingly */                if(line==0){                    prnt_odd++;                    line=1;                }                else{                    prnt_even++;                    line=0;                }            nos--;  //no of space decremented by 1            printf("\n");        }    nos = 1;    /* Second half of the diamond. This one skips its first row rest goes pretty much the same */    for (i = 7; i >= 1; (i = i - 2)) {        for (s = nos; s >= 1; s--) {            printf("  ");        }        for (j = 1; j <= i; j++) {            if(line==0){                if ((i % 2) != 0 && (j % 2) != 0) {                    printf("%2c", prnt_odd);                    } else {                        printf("  ");                    }                }                else{                    if ((i % 2) != 0 && (j % 2) != 0) {                        printf("%2c", prnt_even);                        } else {                            printf("  ");                        }                    }                }                if(line==0){                    prnt_odd++;                    line=1;                }                else{                    prnt_even++;                    line=0;                }

Page 4: 10 Challenging Char Pattern Programs

                nos++;  //no of spaces is incremented by one                printf("\n");            }            return 0;        }Explanation:This is a diamond formation where all the even places are hollowed. In case of odd row no a capital letter is printed and a small case alphabet otherwise. Both the capital letters n the small letters increment by one starting from ‘A’ or ‘a’.

4.A

Z

B C

Y X

D E F

W V U

G H I J

T S R Q

K L M N O

Sol: #include <stdio.h>int main() {    char prnt_odd = 'A';    char prnt_even = 'Z';    int i, j;    // The right angle has nine rows    for (i = 1; i <= 9; i++) {        for (j = 1; j <= i; j++) {            //checks for the column printing restrictions..            if ((i % 2) != 0 && (j % 2) != 0) {                printf("%2c", prnt_odd);                prnt_odd++;            } else if ((i % 2) == 0 && (j % 2) == 0) {                printf("%2c", prnt_even);                prnt_even--;            } else {

Page 5: 10 Challenging Char Pattern Programs

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

Explanation:This is a right angle triangle of alphabets, having two sets of alphabets. One for the odd rows which starts from ‘A’ and increments by one. And another one for the even rows which starts from ‘Z’ and decrements by 1. The restriction in the columns are: if the row no is odd only the odd places are printed else if the row no is even only the even places are filled, rest are skipped.

5. A B C D E

Z Y X W

A B C D

Z Y X

A B C

Z Y

A B

Z

A

Z

A B

Z Y

Page 6: 10 Challenging Char Pattern Programs

A B C

Z Y X

A B C D

Z Y X W

A B C D E

Sol: #include <stdio.h>//This prints the columns with guidance of the row number supplied.int triangle(int i){    char prnt_odd = 'A';    char prnt_even= 'Z';    int j;    for(j=1; j<=i; j++)    {        //Checks for the restrictions        if((i%2)!=0 && (j%2)!=0)        {            printf("%2c",prnt_odd);            prnt_odd++;        }        else if((i%2)==0 && (j%2)==0)        {            printf("%2c",prnt_even);            prnt_even--;        }        else        {            printf("  ");        }    }    return 0;} int main(){    int i;    //Prints the first right angle triangle with 9 rows    for(i=9; i>=1; i--)    {        triangle(i);        printf("\n");    }    // This prints the second right angle triangle skipping its tip.    for(i=2; i<=9; i++)    {        triangle(i);

Page 7: 10 Challenging Char Pattern Programs

        printf("\n");    }    return 0;}Explanation:This pattern can be seen as two right angle triangles of alphabets, having the same tip with two sets of alphabets. One for the odd rows which starts from ‘A’ and increments by one. And another one for the even rows which starts from ‘Z’ and decrements by 1. The restriction in the columns are: if the row number is odd, only the odd places are printed. Else if the row number is even only the even places are filled, rest are skipped.

A B C D E D C B A

A B C D D C B A

A B C C B A

A B B A

A A

A B B A

A B C C B A

A B C D D C B A

A B C D E D C B A

Sol: #include <stdio.h>/* nos   = Number of Spaces required. i     = Number of columns skip  = The row in which we have to skip the first character form  = Whether the alphabets has to printed in Incremental or Decremental order */int triangle(int nos, int i, int skip, int form) {    char prnt[] = "ABCDE";    int j, s;    for (s = nos; s >= 1; s--) {   // Controls the required spacings        printf("  ");    }    for (j = 0; j <= i; j++) {     // prints the characters row wise

Page 8: 10 Challenging Char Pattern Programs

        if (skip != 1) {           // If we need to skip a row.            if (i == skip && j == 0) {                continue;            }        }        if (form == 0) { /* Decides whether the characters are in incremental or decremental order. */            printf("%2c", prnt[j]);        } else {            printf("%2c", prnt[i - j]);        }    }    return 0;} int main() {    int i, nos = -1;    for (i = 4; i >= 0; i--) {        triangle(0, i, 1, 0);    // Prints the first triangle        triangle(nos, i, 4, 1);  // Prints the second triangle        nos = nos + 2;    // Spacing factor        printf("\n");    }    nos = 5;    for (i = 1; i <= 4; i++) {        triangle(0, i, 0, 0);   // Prints the third triangle        triangle(nos, i, 4, 1); // Prints the fourth triangle        nos = nos - 2;     //Spacing factor.        printf("\n");    }    return 0;}

Explanation:This pattern can be treated as four individual right angle triangles composed of alphabets in a sequential manner.

Page 9: 10 Challenging Char Pattern Programs

7. A

B C D

E F G H I

J K L M N O P

Q R S T U V W X Y

Z

Y X W V U T S R Q

P O N M L K J

I H G F E

D C B

A

Sol: #include <stdio.h>int main() {    char prnt = 'A';    int i, j, s, nos = 5;    for (i = 1; i <= 9; (i = i + 2)) {        for (s = nos; s >= 1; s--) {            printf("  ");        }        for (j = 1; j <= i; j++) {            printf("%2c", prnt);            ++prnt;           //Increments the alphabet            if (i == 9 && j == 9) //for the extra Z            {                printf("\f%2c", prnt);            }

Page 10: 10 Challenging Char Pattern Programs

        }        nos--;        //for the continuation        if (i != 9) {            printf("\n");        }    }    printf("\f"); //Line Feed    for (i = 9; i >= 1; (i = i - 2)) {        //Maintaining the required Spaces        if (i == 9) {            nos = 0;        } else if (i == 7) {            nos = 12;        }        for (s = nos; s >= 1; s--) {            printf("  ");        }        for (j = 1; j <= i; j++) {            --prnt;   //decrements the alphabet before printing it.            printf("%2c", prnt);        }        nos++;        printf("\n");    }    return 0;}Explanation:This can be seen as two isosceles triangle made up of alphabets incrementing by one starting from ‘A’. The connecting Z can be considered additionally.

N N

N N N

N N N

N N

Sol: #include <stdio.h>int main() {    char prnt = 'N';    int i, j, k;    for (i = 1; i <= 4; i++) {        //First right-angle triangle.        for (j = 1; j <= i; j++) {            if (j == 1 || j == i) {    // This Ensures an empty baseless triangle                printf("%2c", prnt);   //Prints the character after 2 spaces.            } else {                printf("  ");

Page 11: 10 Challenging Char Pattern Programs

            }        }        //Second right-angle triangle        for (k = 3; k >= i; k--) {            if (k == i) {    // k=0 has to skipped as it shares the same hypotenuse                if (i == 4) { //For the joining point                    break;                }                printf("%2c", prnt);            } else {                printf("  ");            }        }        printf("\n");    }    return 0;}Explanation:This pattern can be viewed as two baseless right-angle triangles arranged in such a way that they share a common hypotenuse

X X

X X

X X

X X

X

X X

X X

X X

X X

Sol: #include <stdio.h>int main() {    char prnt = 'X';    int i, j, s, nos = 0;  //nos controls the spacing.    //1st triangle

Page 12: 10 Challenging Char Pattern Programs

    for (i = 9; i >= 1; (i = i - 2)) {        for (s = nos; s >= 1; s--) {  //Spacing control            printf("  ");        }        for (j = 1; j <= i; j++) {            if (j == 1 || j == i) { //This hollows the triangle                printf("%2c", prnt);            } else {                printf("  ");            }        }        printf("\n");        nos++;  // In the upper triangle the space increments by 1.    }/* Since both the triangles have the same peak point, while printing the second triangle we'll ignore its peak point. Thus i starts from 3 and the nos from 3.*/    nos = 3;    for (i = 3; i <= 9; (i = i + 2)) {        for (s = nos; s >= 1; s--) {            printf("  ");        }        for (j = 1; j <= i; j++) {            if (j == 1 || j == i) {                printf("%2c", prnt);            } else {                printf("  ");            }        }        printf("\n");        nos--; //The spaces are in a decrementing order.    }    return 0;}

Page 13: 10 Challenging Char Pattern Programs

K K

K K

K K

K K

K K

K K

K K

K

K K

K K

K K

K K

K K

K K

K K

Sol: #include <stdio.h>int main() {    char prnt = 'K';

Page 14: 10 Challenging Char Pattern Programs

    int i, j;    //First right-angle triangle    for (i = 7; i >= 1; i--) {        for (j = 1; j <= i; j++) {            if (j == 1 || j == i) {  //This hollows the triangle                printf("%2c", prnt);            } else {                printf("  ");            }        }        if (i != 1) {            printf("\n");        }    }    // Second triangle    for (i = 1; i <= 7; i++) {        for (j = 1; j <= i; j++) {            if (j == 1 || j == i) {                printf("%2c", prnt);            } else {                printf("  ");            }        }        printf("\n");    }    return 0;}

Explanation:

This pattern can be be seen as an inverted right triangle placed over a

normal right-angled triangle. In addition to the triangles, there is an extra

character in the intersection point.

Since both the triangles share the same tip and in the figure and we have

an extra character in the point of intersection, we can start the second

right angle triangle from the same line where the first one ends.