These pattern logic will be useful for the interview preparation
1. 4*4 Matrix:
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- Console.Write("*");
- }
- Console.WriteLine();
-
- }
Output:
2. Right angle triangle:
- for (int i = 0; i <= 4; i++)
- {
- for (int j = 0; j <= i; j++)
- {
- Console.Write("*");
-
- }
- Console.WriteLine();
- }
Output:
3. Right angle triangle with numbers:
- for (int i = 1; i <= 4; i++)
- {
- for (int j = 1; j <= i; j++)
- {
- Console.Write(j);
-
- }
- Console.WriteLine();
- }
Output:
4. Triangle
- for (int i = 1; i <= 4; i++)
- {
- for (int j = 0; j <= (4 - i); j++)
- Console.Write(" ");
- for (int k = 1; k <= i; k++)
- Console.Write("*" + " ");
-
- Console.WriteLine();
- }
Output:
5. Pyramid
- for (int i = 1; i <= 4; i++)
- {
- for (int j = 0; j <= (4 - i); j++)
- Console.Write(" ");
- for (int k = 1; k <= i; k++)
- Console.Write("*" + " ");
-
- Console.WriteLine();
- }
- for (int i = 3; i >= 0; i--)
- {
- for (int j = 0; j < (5 - i); j++)
- Console.Write(" ");
- for (int j = 1; j <= i; j++)
- Console.Write("*" + " ");
- Console.WriteLine();
- }
Output:
6. Pyramid with numbers.
- for (int i = 1; i <= 4; i++)
- {
- for (int j = 0; j <= (4 - i); j++)
- Console.Write(" ");
- for (int k = 1; k <= i; k++)
- Console.Write(i + " ");
-
- Console.WriteLine();
- }
- for (int i = 3; i >= 0; i--)
- {
- for (int j = 0; j < (5 - i); j++)
- Console.Write(" ");
- for (int j = 1; j <= i; j++)
- Console.Write(i + " ");
- Console.WriteLine();
- }
Output:
I hope you have enjoyed this blog.