Monday, July 28, 2014

Accept number between 1 to 9 and display a pattern .

This is a C# program to print the triangle. This program uses the for loops to print the triangle.


The trick to the triangle is to use a loop within a loop the outside loop controls the number of lines to be output, and the inner loop controls the number of stars to be output on the line.

I will give you 2 very significant hints :

1. For the innner loop, refer to the value of the outer loop. So, if the outer loop is 1 to 5, the inner loop can refer to it to figure out how many stars to print.
2. Insert the "new line" after the inner loop, but inside the main loop .

Algorithm :

  1. Accept a number in the range of 1-9 from the user. Store it in a variable, num.
  2. Declare an integer variable, i .
  3. Set i = 1 .
  4. Repeat until i becomes greater than num : // To display num rows
  • Declare an integer variable j .
  • Set j = i .
  • Display j and insert a space .
  • Set j = j - 1 .
  • if j > 0 go to step c .
  • Insert a line break
  • Set i = i +1 .

This algorithm in C# and it helps you to display triangle pattern in c# . This is the best method to solve it .


Here is source code of the C# Program to Display Number in triangle Shape. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below :

Source Code :

using System;

 class triangle_Shape
{
   public static void Main()
    {
       int num , p;
         
       while(true)
        {
          Console.Write("Enter a number between 1 to 9 : ");

          num = Convert.ToInt32(Console.ReadLine());

          if((num<=9)&&(num>0))
            {
               break;
            }

          Console.WriteLine("Wrong Input!! Please enter a number between 1 to 9");

        }

        for(int row=1; row<=num; row++)
          { 
             p=row;

             for(int col=1; col<=row; col++)
               {
                  Console.Write(""+p);
                  p--;
               }

             Console.Write("\n");
           }

         Console.ReadLine();

      }
} 



OUTPUT :


Enter a number between 1 to 9 : 6
1
21
321
4321
54321
654321


Here is source code of the C# Program to Display Stars in triangle Shape. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below :
using System;

 class triangle_Shape
{
   public static void Main()
    {
       int num , p;
         
       while(true)
        {
          Console.Write("Enter a number between 1 to 9 : ");
          num = Convert.ToInt32(Console.ReadLine());
          if((num<=9)&&(num>0))
            {
               break;
            }
          Console.WriteLine("Wrong Input!! Please enter a number between 1 to 9");
        }

        for(int row=1; row<=num; row++)
          { 
             p=row;
             for(int col=1; col<=row; col++)
               {
                  Console.Write(""+"*");  // Only change this
                  p--;
               }
             Console.Write("\n");
           }
         Console.ReadLine();

      }
} 



OUTPUT :


Enter a number between 1 to 9 : 6
*
**
***
****
*****
******

Note:

You're using the Console.Write and Console.WriteLine commands which are writing at the current cursor position. Your loops are always working in a 10*10 character range, followed by a newline.
An approach to your problem would be absolute cursor position, setting the cursor to the right position and then drawing one character. Each subsequent triangle must be moved by a offset: