w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Display a triangle


C# Sharp Basic Data Types: Exercise-2 with Solution

Write a C# Sharp program that takes a number and a width also a number, as input and then displays a triangle of that width, using that number.

Sample Solution:-

C# Sharp Code:

using System;
public class Exercise2
{
  public static void Main()
  {
   Console.Write("Input a number: ");
   int num = Convert.ToInt32( Console.ReadLine() );
 
   Console.Write("Input the desired width: ");
   int width = Convert.ToInt32( Console.ReadLine() );
 
   int height = width;   
   for (int row=0; row < height; row++)
   {
    for (int column=0; column < width; column++)
   {
   Console.Write( num );
  }
 
   Console.WriteLine();
   width--;
  }
 }
} 

Sample Output:

Input a number: 2                                                                                             
Input the desired width: 5                                                                                    
22222
2222
222
22  
2 

Flowchart:

Flowchart: Display a triangle

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp program that takes three letters as input and display them in reverse order.
Next: Write a C# Sharp program that takes userid and password as input (type string). After 3 wrong attempts, user will be rejected.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming