Home » C# programs » C# pattern programs

C# program to print left and right aligned pattern of asterisks

Here, we are going to learn how to write C# program to print left and right aligned patterns of asterisks till N rows?
Submitted by IncludeHelp, on March 26, 2018

Pattern -1

    *
    * * 
    * * *
    * * * *
    * * * * *
    ----- till n row.

Program

using System;
using System.Collections;

namespace ConsoleApplication1
{
 
    class Program
    {
        static void Main()
        {
            int inner = 0;
            int outer = 0;
            
            int n = 0;

            Console.Write("Enter Value of N : ");
            n = Convert.ToInt32(Console.ReadLine());

            for (outer = 1; outer <= n; outer++)
            {
                for (inner = 0; inner < outer; inner++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
        }
    }
}

In this program, we use nested loops, outer loop denotes total number of line to be printed on the basis of ‘n’ variable.

Here Inner loop is used to print ‘*’ on console, according to the value of outer variable, denotes how many ‘*’ should be print on console.

Console.WriteLine() method is used to change line after completion of inner loop.

Pattern -2

        *
       * *
    *  * *
  * *  * *
* * *  * *   
----- till n row.

Program

using System;
using System.Collections;
namespace ConsoleApplication1
{ 
   class Program
    {
        static void Main()
        {
            int inner = 0;
            int outer = 0;
            int space = 0;
            int n = 0;

            Console.Write("Enter Value of N : ");
            n = Convert.ToInt32(Console.ReadLine());

            for (outer = 1; outer <= n; outer++)
            {
                for (space = n; space > outer; space--)
                {
                    Console.Write(" ");
                }
                for (inner = 0; inner < outer; inner++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
        }
    }
}

Same as above, and Space loop is used to print space on console, according to the value of outer variable.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.