Software & Finance





Visual C++ Programming Loops - Left and Right Aligned Pattern





This program is written in Visual C++ programming language and will accept a number as input. The loops are used to build a left and right aligned pattern of *.

 


Source Code


#include <iostream>

#include <stdio.h>

#include <conio.h>

#include <process.h>

#include <string.h>

 

 

void main()

{

    int i, j, k;

    int n = 0;

    std::cout << "Program for displaying pattern of *.\n";

    std::cout << "Enter the maximum number of *: ";

    std::cin >> n;

 

    std::cout << "\nPattern 1 - Left Aligned:\n";

 

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

    {

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

                std::cout << "*";

          std::cout << "\n";

      }

 

    std::cout << "\nPattern 2 - Right Aligned:\n";

 

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

    {

          for(j = 0; j < n - i; j++)

                std::cout << " ";

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

                std::cout << "*";

          std::cout << "\n";

    }

    std::cout << "\n";

}

Output


Program for displaying pattern of *.

Enter the maximum number of *: 9

 

Pattern 1 - Left Aligned:

 

*

**

***

****

*****

******

*******

********

*********

 

Pattern 2 - Right Aligned:

 

*********

 ********

  *******

   ******

    *****

     ****

      ***

       **

        *

 

Press any key to continue . . .