facebook like button

05 May, 2011

program 43: ABCD triangle


The need:
     This program draws a triangle of ABCD on the output screen. This program was written to show that we can increment the characters like numbers.

The code: 
--------------------------------------------
#include<stdio.h>
main()
{
    char z;
    int j,i,k;
    printf("Enter the number of rows..(1 to 26)\t");
    scanf("%d",&k);
    if(k<1||k>26)
    {
        printf("\nThe number entered was not in range of 1 to 26\n");
        printf("exiting...\n");
        exit(0);
    }
    printf("\n\n");
    for (i=1;i<=k;i++)
    {
        z = 'A';
        for (j=0;j<i;j++)
        {    
            printf ("%c  ",z);
            z++;
        }
    printf("\n\n");
    }
}
-------------------------------------------- 

The approach: 
This program is very much similar to that star triangle program(program34 of this blog) with only addition of a character variable z which is getting printed each time in place of star(*). z is also incremented each time thus printing different values in each print in row. When a new row starts z is again initialized with character literal 'A' and the loop repeats.

2 comments:

  1. I need coding of this output:
    ABCDEDCBA
    ABCD_DCBA
    ABC___CBA
    AB_____BA
    A_______A
    space is denoted with '_'

    ReplyDelete
  2. below is the answer of your question -
    http://programsimply.blogspot.com/2011/08/abcd-pattern2.html
    I hope you are not getting your homework done by me. :)

    ReplyDelete

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply