Subscribe

RSS Feed (xml)

Thursday, November 26, 2009

Write a C program to print the given alphabet pyramid -2


A B C D E F G F E D C B A
  A B C D E F E D C B A
    A B C D E D C B A
      A B C D C B A
        A B C B A
          A B A
            A 


#include<stdio.h>
#incldue<conio.h>
int main( )
{
  int r,c,askey,sp;
  clrscr( );
  
  for( r=7; r>=1; r-- )
  {
    for(sp=6; sp>=r; sp--)
        printf("  "); //2 spaces

    askey=65;
    for(c=1; c<=r; c++ )
      printf("%2c", askey++ );
    
    for(c=r-1; c>=1; c-- )
       printf("%2c", --askey);
   
   printf("\n");
  }
  getch();
  return 0;
}

Output:-
A B C D E F G F E D C B A
  A B C D E F E D C B A
    A B C D E D C B A
      A B C D C B A
        A B C B A
          A B A
            A
 

11 comments:

  1. this is wrong program i had try taht the output is different

    ReplyDelete
  2. This is 100% Correct program.
    I have execute this program at my PC...

    ReplyDelete
  3. @gaurav
    thanq 4 uploading this prgm......
    askey--;
    is missing after printf in 3rd forloop
    which gives the above output

    ReplyDelete
  4. can you make it in cpp?? please

    ReplyDelete
    Replies
    1. #include

      main()
      {
      int i,r,c,askey,sp;

      printf("Enter how many rows : ");
      scanf("%d",&r);

      for(i=r;i>=1;i--){
      for(sp=6;sp>=i;sp--)
      printf(" ");

      askey=64;
      for(c=1;c<=i;c++)
      printf("%2c",++askey);

      for(c=i-1;c>=1;c--)
      printf("%2c",--askey);

      printf("\n");
      }
      }


      This is the correct code. In the post the output was coming a bit different . So small change is required. Try my code. in case of any error. reply me.

      Delete
  5. Nice! You can also try C code champ for several C programs, codes and tutorials. C codechamp contains a great list of C programs and tutorials.
    http:\\ccodechamp.com

    ReplyDelete
  6. Sir, i could not understand the 2nd for loop. if we are taking user input in 'r', and if r=8,9,10 then,doesnt that 2nd for loop shows an error in output?

    ReplyDelete
  7. in the given code u jst need to change askey=65 to askey=64 & askey++ to ++askey

    ReplyDelete
  8. 3rd foor loop is little bit confusing. Code is correct and working.

    ReplyDelete