Question #125806

Write C++ program to take input in variables "n" and "L" from user and print following character pattern

.For reference, ASCII chart is also given below.

Sample inputs and output are shown below

n=5, L=’A’

A

*C

**E

***G

****I

*****K

****I

***G

**E

*C

A


For sample inputs: n=5, L=’a’

a

*c

**e

***g

****i

*****k

****i

***g

**e

*c

a


Expert's answer

#include <iostream>
using namespace std;


int main()
{
    int n;
    cout << "n = ";
    cin >> n;


    char L;
    cout << "L = ";
    cin >> L;


    int starsCount = 0;
    cout << endl << endl;


    if (L >='a' && L <='z')
    {
        for(L; L <='z'; L+=2)
        {
            if (L>'z')
                break;
            for(int j = 0; j < starsCount; j++)
            {
                cout << '*';
            }
            cout << L << endl;
            starsCount++;
            if (starsCount == 6)
                break;
        }
        starsCount-=2;
        L-=2;




        for(L;L >='a'; L-=2)
        {
            if (L <'a')
                break;
            for(int j = 0; j < starsCount; j++)
            {
                cout << '*';
            }
            cout << L << endl;
            starsCount--;
            if (starsCount == -1)
                break;
        }
    }
    else
    {
        for(L; L <='Z'; L+=2)
        {
            if (L >'Z')
                break;
            for(int j = 0; j < starsCount; j++)
            {
                cout << '*';
            }
            cout << L << endl;
            starsCount++;
            if (starsCount == 6)
                break;
        }
        starsCount-=2;
        L-=2;
        for(L; L >='A'; L-=2)
        {
            if (L <'A')
                break;
            for(int j = 0; j < starsCount; j++)
            {
                cout << '*';
            }
            cout << L << endl;
            starsCount--;
            if (starsCount == -1)
                break;
        }
    }
    return 0;
}

LATEST TUTORIALS
APPROVED BY CLIENTS