Q:

C# program to print left and right aligned pattern of given character

0

C# program to print left and right aligned pattern of given character

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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;
            char ch = ' ';

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

            Console.Write("Enter Character : ");
            ch = char.Parse(Console.ReadLine());

            for (outer = 1; outer <= n; outer++)
            {
                for (inner = 0; inner < outer; inner++)
                {
                    Console.Write(ch);
                }
                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 input character pattern on console, according to the value of outer variable, denotes how many character 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;
            char ch = ' ';

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

            Console.Write("Enter Character : ");
            ch = char.Parse(Console.ReadLine());

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

                for (inner = 0; inner < outer; inner++)
                {
                    Console.Write(ch);
                }
                Console.WriteLine();
            }
        }
    }
}

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now