Author Topic: C# Left / Right Aligned Star Pattern using for loops  (Read 1694 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
C# Left / Right Aligned Star Pattern using for loops
« on: June 29, 2011, 03:26:16 pm »
Here is the source code for C# Left / Right Aligned Star Pattern using for loops

Code: (C#)
//Source Code Listing

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern
{
    class Pattern
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Program for displaying pattern of *.");
            Console.Write("Enter the maximum number of *: ");
            int n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("\nPattern 1 - Left Aligned:\n");
		    for (int i = 1; i <= n; i++)
		    {
			    for (int j = 1; j <= i; j++)
				    Console.Write("*");
			    Console.WriteLine();
    		}

		    Console.WriteLine("\nPattern 2 - Right Aligned:\n");
		    for (int i = n; i >= 1; i--)
		    {
			    for(int j = 0; j < n - i; j++)
				    Console.Write(" ");
			    for (int j = 1; j <= i; j++)
				    Console.Write("*");
			    Console.WriteLine();
		    }
		    Console.WriteLine();
        }
    }
}


Program for displaying pattern of *.
Enter the maximum number of *: 9

Pattern 1 - Left Aligned:

*
**
***
****
*****
******
*******
********
*********

Pattern 2 - Right Aligned:

*********
 ********
  *******
   ******
    *****
     ****
      ***
       **
        *

Press any key to continue . . .
« Last Edit: November 16, 2011, 05:06:04 pm by kathir »

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1