10 Challenging char pattern programs

by Utsav Banerjee on January 13, 2010


Output of a char pattern program

Output of a char pattern program

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;
    }
    

    Back to top

  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;
    }
    

    Back to top

  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;
    }
    

    Back to top

  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.
    Back to top


About the Author:  This post was written by Utsav Banerjee. You can reach Utsav on email at fugitiveland@gmail.com


2,250 readers have already subscribed to email updates!

Enter your email address:

  • priya

    thankssssssssssssssss a lotttttttt for tisss

  • priya

    i have tried a lottttttttttt fork shaped progarm.really thankzzzz

  • admin

    You are welcome priya. You may thank us by subscribing to our blog’s email updates. You will never regret it. :-)
    http://subscribe.interviewmantra.net

    Also if you are interested in sharing knowledge, you may contribute knowledge to this blog, like these contributors.

  • Geoff

    Anyone reading this working on their programming skills should *please* recognize this is not how you’d write production code. Look at any one of those examples and *without looking at the problem*, try to reverse engineer what the code is trying to do.

    These are brain teasers for C programmers.

    I might suggest a useful approach to this problem would be to define any one of those patterns as follows…

    /*=======================================*/

    #include

    const char * my_pattern[] =
    { \
    “…”, \
    “…”, \
    “111″, \
    “222″, \
    “” \
    };

    /* arbitrary pattern line length */
    #define MAX_LINE_LEN 80

    void emit_pattern(const char ** pattern)
    {
    int i = 0;
    while (strncmp(pattern[i], “”, MAX_LINE_LEN))
    {
    printf(“%.*s\n”, MAX_LINE_LEN, pattern[i]);
    i++;
    }
    }

    main(void)
    {
    emit_pattern(my_pattern);
    return 0;
    }

    /*=======================================*/

    The primary rationale here is you are separating the structure of the data from the structure of the code. Any person who reads this can practically immediately tell what you were trying to do. Production code environments *require* this. You will write code that will outlive your capacity or ability to indefinitely support it. Even if you do support every line of code you write indefinitely, do yourself a favor and write something you can read back a year later and understand what it was meant to do.

    Here is my output when compiled with gcc…

    $gcc –version
    gcc (Debian 4.3.2-1.1) 4.3.2
    Copyright (C) 2008 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    $ gcc pattern.c

    $ ./a.out


    111
    222

    Thanks for reading.

  • rishi

    can you pls solve this for me…
    reply..!!

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

  • Kavitha

    how to print?
    0
    0 1 0
    0 1 2 1 0

  • http://nill meena

    can u solve it please reply soon

  • http://nill meena

    can u solve it please reply soon
    1
    0 1
    1 0 1
    0 1 0 1
    1 0 1 0 1

  • Utsav Banerjee

    @ Geoff : Sorry for such a late reply ,first of all let me remind you that the main goal of pattern program is to solve it using loops. These are given as exercises in order to build up a strong logic base. You can’t just store the reqd. pattern in an array and print it likewise.

  • kv

    can u solve this for me i want to take input from the keyboard any string as name and display the output as the following using for loop in c language
    HELLO
    HELL
    HEL
    HE
    H

  • http://www.anmolanand.wordpress.com Anmol Anand

    Solution to rishi’s problem

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

    Written in C
    #include
    #include

    int main(int argc, char *argv[])
    {
    int i, j,k ;
    for(i=1;i<=4;i++)
    {
    for(j=1;j1)
    for(k=i-1;k>=1;k–)
    {

    printf(“%d”,k);
    }
    printf(“\n”);
    }

    system(“PAUSE”);
    return 0;
    }

  • http://www.anmolanand.wordpress.com Anmol Anand

    solution to Meena’s problem

    1
    0 1
    1 0 1
    0 1 0 1
    1 0 1 0 1

    written in C

    #include
    #include

    int main(int argc, char *argv[])
    {
    int a,b,c,d,e;

    for(a=1; a<=5;a++)
    {
    if(a%2!=0)
    {
    for(b=1;b<=a;b++)
    {
    if(b%2!=0)
    printf("%d",1);
    else
    printf("%d",0);

    }

    }
    if(a%2==0)
    {
    for(c=1;c<=a;c++)
    {
    if(c%2!=0)
    printf("%d",0);
    else
    printf("%d",1);

    }

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

  • http://www.anmolanand.wordpress.com Anmol Anand

    solution to kv’s problem

    /*can u solve this for me i want to take input from the keyboard any string as name and display the output as the following using for loop in c language
    HELLO
    HELL
    HEL
    HE
    H */

    #include
    #include
    #include

    int main(int argc, char *argv[])
    {
    int charlen,len,l;
    printf(“enter number of characters ::”);
    scanf(“%d”,&charlen);

    char input[charlen];
    printf(“\nEnter the word :: “);
    scanf(“%s”,&input);

    while ((l=strlen(input))!=0)
    {
    for(len = 0;len<strlen(input);len++)
    {
    printf("%c",input[len]);
    }
    input[strlen(input)-1]='';
    printf("\n");
    }
    system("PAUSE");
    return 0;
    }

  • salil oak

    Write a ‘C’ Program to show following pattern
    a
    a b
    a b c
    a b c d
    a b c
    a b
    a

  • shah poonam

    can u plz solve this problem 4 me??
    a b c d e
    a b c d
    a b c
    a b
    a

  • kalyani

    can you give me a solution for a a
    b a a b
    c b a a b c
    plz

  • kalyani

    plz can you give me a solution for
    a a
    b a a b
    c b a a b c

  • sneha patel

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

  • S.V.Ramana

    This is code for sneha patel ‘s outpurt
    like

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

    #include
    #include
    void main()
    {
    int i,j,n;
    clrscr();
    printf(“Enter n value”);
    scanf(“%d”,&n);
    int k=1;
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=k;j++)
    {
    printf("*");
    }
    k=k+2;
    printf("\n");
    }
    getch();
    }

  • S.V.Ramana

    This is code for shah poonam’s output like
    abcde
    abcd
    abc
    ab
    a

    #include
    #include
    void main()
    {
    clrscr();
    for(int i=1;i<6;i++)
    {
    const int k=97;
    int m=k;
    for(int j=1;j<=(6-i);j++)
    {
    printf("%c\t",m);
    m++;
    }
    printf("\n");
    }
    getch();
    }

  • raj kumar barnwal

    *
    **
    ***

  • Khnhuzaifah

    Can you give the solution for *****
                                                                ****
                                                                ***
                                                                **
                                                                *

  • Khnhuzaifah

    Can you give me the solution for     
                            1
                          2 2
                         3 3 3
                        4 4 4 4
                       5 5 5 5 5

  • Khnhuzaifah

    Can you give me the solution for
    P
    PQ                                                      
    PQR
    PQRS
    PQRST

  • Khnhuzaifah

    Can you give me the solution for

     
                                            1
                                           21A
                                          321AB
                                         4321ABC
                                        54321ABCD

  • Greeshma Nair

    #include
    #include
    void main()
    {
    int i,j,k,c=5;
    for(i=1;i<=5;i++)
    {
    for(k=1;k<=c;k++)
    {
    printf(" ");
    }
    for(j=1;j<=i;j++)
    {
    printf("%2d",i);
    }
    printf("n");
    c–;
    }
    getch();
    }

  • bala

    please tell the solution for given below
                                 i
                             h 1 h
                          g         g
                       f  2        2  f
                    e                     e
                d  4                  4  d
             c                                 c
          b  1                             1  b
       a                                             a  
          b  1                             1  b
             c                                 c
                d  4                  4  d
                    e                     e
                       f  2        2  f
                          g         g
                             h 1 h
                                 i

  • Prashant

    give me pattern for this

    ABCDEFGFEDCBA
    ABCDEF   FEDCBA
    ABCDE        EDCBA
    ABCD              DCBA
    ABC                    CBA
    AB                         BA
    A                              A

  • Manojraghav54

    #include
    main()
    {
    Printf(“fuck off”);
    }

  • Prashant

    #include
    #include
    void main()
    {                    int i,j;
                          for(i=5;i>=1;i–)
                          {         for(j=1;j<=i;j++)
                                                 printf("%c",j+64);
                                     printf("n");
                          }
                          getch();
    }  

  • Chizomw

    give me a pattern for this just using funtions
                    w
                 www
             wwwww
          wwwwwww
            wwwww
               www
                  w

  • Engr. Siyal

    Write a program which prints Alphabets A to Z and also prints their ASCII code?

  • BMengrMUET

    Plz can u solve this question for me (but in C programming format)

    1-      Write a program which reverses the given string into reverse word order.
    Example:-
    Input:
    The Quick Brown Fox Jumps Over The Lazy Dog.
    Output:
    Dog Lazy The Over Jumps Fox Brown Quick The.

  • Kashikarroshni

    please give me this c progame

  • Kashikarroshni

    please send me this programme

  • http://twitter.com/GreatDharmatma Ajitem Sahasrabuddhe

    Hello,

    First of all, I would like to congratulate you for all the hard work and effort put forth in building this website. I am sure, many people like me having a passion for C Language found this site very useful.

    I would just like to contribute to the effort made by you. I have come-up with a shorter solution for Program #6. The method used here is simple but long. My method is a little complicated but people comfortable with pointers would find it a lot easier.

    #include #include #include int main(){    char pattern[] = “A B C D E D C B A”;    int i,pos = 0;    for (i = 0; i < 9; i++)    {        printf("%sn", pattern);        if (i < 4)        {            *(pattern + 8 – pos) = ' ';            *(pattern + 8 + pos) = ' ';            pos+=2;        }        else        {            pos-=2;            *(pattern + 8 – pos) = *(pattern + 8 – (2+pos)) + 1;            *(pattern + 8 + pos) = *(pattern + 8 – (2+pos)) + 1;        }    }    getch();    return (EXIT_SUCCESS);}

    This program was successfully executed to print the desired pattern using GCC 3.4.5 (mingw special) using NetBeans 7.0.1 IDE on Windows 7 x64.

    Regards,
    Ajitem Sahasrabuddhe
    bZk@live.in

  • http://twitter.com/GreatDharmatma Ajitem Sahasrabuddhe

    Hello,

    First of all, I would like to congratulate you for all the hard work and effort put forth in building this website. I am sure, many people like me having a passion for C Language found this site very useful.

    I would just like to contribute to the effort made by you. I have come-up with a shorter solution for Program #6. The method used here is simple but long. My method is a little complicated but people comfortable with pointers would find it a lot easier.

    #include #include #include int main(){    char pattern[] = “A B C D E D C B A”;    int i,pos = 0;    for (i = 0; i < 9; i++)    {        printf("%sn", pattern);        if (i < 4)        {            *(pattern + 8 – pos) = ' ';            *(pattern + 8 + pos) = ' ';            pos+=2;        }        else        {            pos-=2;            *(pattern + 8 – pos) = *(pattern + 8 – (2+pos)) + 1;            *(pattern + 8 + pos) = *(pattern + 8 – (2+pos)) + 1;        }    }    getch();    return (EXIT_SUCCESS);}

    This program was successfully executed to print the desired pattern using GCC 3.4.5 (mingw special) using NetBeans 7.0.1 IDE on Windows 7 x64.

    Regards,
    Ajitem Sahasrabuddhe
    bZk@live.in

  • http://twitter.com/GreatDharmatma Ajitem Sahasrabuddhe

    Alternate Code for Program #9

    #include #include #include int main(){    char pattern = ‘X’;    int i;    for (i = 1; i <= 9; i++)    {        if(i = 6)        {            printf(“%*.*c%*.*cn”,10-i,2,pattern,(2*i)-10,2,pattern);        }    }    getch();    return (EXIT_SUCCESS);}

  • Sriram

    #include
    int main()
    {for(i=5;i>=1;i–)
    {
    for(j=1;j<i;j++)
    {
    printf("*");
    }
    printf("n");
    }
    return 0;
    }

  • Prashanth kumar

    Give me a pattern for this 

    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    BCDEFGHIJKLMNOPQRSTUVWXYZA
    CDEFGHIJKLMNOPQRSTUVWXYZAB
    DEFGHIJKLMNOPQRSTUVWXYZABC
    EFGHIJKLMNOPQRSTUVWXYZABCD
    FGHIJKLMNOPQRSTUVWXYZABCDE
    GHIJKLMNOPQRSTUVWXYZABCDEF  
    and so on ……. ……….

Previous post:

Next post: