Monday 10 October 2011

10 Challenging Char Pattern Programs




                                 
                                 Here are 10 pattern type problems statements with solutions. All the programs are written in C language.  This post is a part of the pattern programs series.
  1. Write a C program to print the following pattern:
    
    A
    B A
    C B A
    D C B A
    E D C B A
  2. Write a C program to print the following pattern:
    A B C D E
    B C D E
    C D E
    D E
    E
    
  3. Write a C program to print the following pattern:
             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
    
  4. Write a C program to print the following pattern:
    
    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
    
  5. Write a C program to print the following pattern:
     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
    A   B   C
    Z   Y   X
    A   B   C   D
    Z   Y   X   W
    A   B   C   D   E
    
  6. Write a C program to print the following pattern:
    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
    
    
  7. Write a C program to print the following pattern:
               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
    
  8. Write a C program to print the following pattern:
    N     N
    N N   N
    N   N N
    N     N
    
  9. Write a C program to print the following pattern:
     X               X
    X           X
    X       X
    X   X
    X
    X   X
    X       X
    X           X
    X               X
    
  10. Write a C program to print the following pattern:
    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
    
  1. Write a C program to print the following pattern:
  2. A
    B A
    C B A
    D C B A
    E D C B A
    Program:
    #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;
    }
    
  3. Write C program to print the following pattern:
    A B C D E
      B C D E
        C D E
          D E
            E
  4. Program:
    #include<stdio.h>
    int main() {
     int i, j;
     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;
    }
    
  5. Write C program to print the following pattern:
    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
  6. Program:
    #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++) {
       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;
        }
        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’. Back to top
  7. Write a C program to print the following pattern:
    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
  8. Program:
    #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 {
        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. Back to top
  9. Write a C program to print the following pattern:
    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
     A   B   C
       Z   Y   X
     A   B   C   D
       Z   Y   X   W
     A   B   C   D   E
  10. Program:
    #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);
      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. Back to top
  11. Write a C program to print the following pattern:
    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
  12. Program:
    #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
      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. Back to top
  13. Write a C program to print the following pattern:
    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
  14. Program:
    #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);
       }
      }
      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. Back to top
  15. Write a C program to print the following pattern:
    N     N
    N N   N
    N   N N
    N     N
  16. Program:
    #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("  ");
       }
      }
      //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. Back to top
  17. Write a C program to print the following pattern:
    X               X
       X           X
         X       X
           X   X
             X
           X   X
         X       X
       X           X
     X               X
  18. Program:
    #include <stdio.h>
    int main() {
     char prnt = 'X';
     int i, j, s, nos = 0;  //nos controls the spacing.
     //1st triangle
     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;
    }
    
  19. Write a C program to print the following pattern:
    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
  20. Program:
    #include <stdio.h>
    int main() {
     char prnt = 'K';
     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.

No comments:

Post a Comment

EARN MONEY ONLINE

Earn upto Rs. 9,000 pm checking Emails. Join now!

register and earn money daily by spending 5 min on the internet by clicking the above red text...

EARN MONEY BY RECIEVING MESSAGES TO YOUR MOBILE BY SIGNING UP HERE..

(CLICK THE BELOW BANNER AND REGISTER)

NOTE

ALL IMAGES AND VIDEOS LINKS CONTAINED IN THIS BLOG WERE FOUND ON THE INTERNET. IF BY ANYHOW ANY OF THEM IS OFFENSIVE TO YOU, PLEASE, CONTACT US ASKING FOR THE REMOVAL. IF YOU OWN COPYRIGHTS OVER ANY OF THEM AND DO NOT AGREE WITH IT BEING SHOWN HERE, PLEASE SEND US AN EMAIL WITH OWNERSHIP PROOF AND WE WILL REMOVE IT.EMAIL US TO jsaikumar93@gmail.com