Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 4. Printing Triangle
- Create a method for printing triangles as shown below:
- Examples
- Input Output
- 3 1
- 1 2
- 1 2 3
- 1 2
- 1
- 4 1
- 1 2
- 1 2 3
- 1 2 3 4
- 1 2 3
- 1 2
- 1
- using System;
- namespace _04MethodsLabPrintingTriangle
- {
- class Program
- {
- static void Main(string[] args)
- {
- int input = int.Parse(Console.ReadLine());
- TopTriangle(input);
- BottomTriangle(input - 1);
- /*
- for (int i = 1; i <= input; i++)
- PrintLine(1, i); // Print 1... 1 2 ... 1 2 3 n
- for (int a = input - 1; a >= 1; a--)
- PrintLine(1, a); // Print 1 2 3 n ... 1 2 ... 1*/
- }
- /*private static void PrintLine(int start, int end)
- {
- for (int i = start; i <= end; i++)
- {
- Console.Write(i + " ");
- }
- Console.WriteLine();
- }*/
- private static void TopTriangle(int input)
- {
- for (int i = 1; i <= input; i++)
- {
- for (int a = 1; a <= i; a++)
- {
- Console.Write(a + " ");
- }
- Console.WriteLine();
- }
- }
- private static void BottomTriangle(int input)
- {
- for (int i = input; i > 0; i--)
- {
- for (int j = 1; j <= i; j++)
- {
- Console.Write(j + " ");
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment