facebook like button

Showing posts with label C programing made easy. Show all posts
Showing posts with label C programing made easy. Show all posts

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.