Software & Finance





C++ - Loops To Build Pattern of * and numbers





This program will accept a number as input. The loops are used to build a pattern of * and numbers. There are four loops, two for displaying number and other two for displaying pattern of stars.

 


Source Code


int main()

{

    int n;

    std::cout << "Enter a number: ";

    std::cin >> n;

 

    std::cout << "\n";

 

    for(int i = n; i > 1; i--)

    {

        for(int j = 1; j <= i; j++)

            std::cout << "*";

        std::cout << "\n";

    }

    for(int i = 1; i <= n; i++)

    {

        for(int j = 1; j <= i; j++)

            std::cout << "*";

        std::cout << "\n";

    }

    std::cout << "\n";

 

    for(int i = 1; i < n; i++)

    {

        for(int j = 1; j <= i; j++)

            std::cout << j;

        std::cout << "\n";

    }

    for(int i = n; i >= 0; i--)

    {

        for(int j = 1; j <= i; j++)

            std::cout << j;

        std::cout << "\n";

    }

    std::cout << "\n";

 

    return 0;

}

Output


Enter a number: 6

 

******

*****

****

***

**

*

**

***

****

*****

******

 

1

12

123

1234

12345

123456

12345

1234

123

12

1