Letters Triangle


General Statement: For an input letter, output the alphabet from A through that letter in a triangle pattern. The number of letters on a line is the same as the line number of the pattern.

Input: There are an unknown number of values on a single line. An asterisk (*) is used to indicate the end of the data set.

Output: All letters are upper case.
The columns are to be aligned vertically.
Include some white space between outputs.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: All letters are upper case.

Discussion: If the last line of the pattern is incomplete, fill in each “empty” space with an asterisk (*).

Sample Input:
DJC*

Sample Output:
A
BC
D**

A
BC
DEF
GHIJ

A
BC

Solutions:

#include <stdio.h>

int main ()
{
    char input;

    while ( scanf ("%c", &input) && input != '*' ) {

        if ( input == '\n' )
            continue;

        char print = 65;
        int line = 1;
        bool flag = true;

        while ( flag ) {

            for ( int i = 0; i < line; i++ ) {
                if ( input < (char) print ) {
                    printf("*");
                    flag = false;
                }
                else {
                    printf ("%c", print);
                    print++;
                }
            }
            line++;
            printf ("\n");

            if ( input <= (char) print )
                flag = false;
        }
        printf ("\n");
    }

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s