PRINT A PALINDROME WITH NUMBERS AND STARS
APPROACH
- The same flag can be set to 0 and 1 simultaneously to print * and the Ith iteration number continuously inside the triangle.
- The same logic used in the previous problem is used here to create a triangle pattern.
SOLUTION:
using System;
public class Test
{
public static void Main()
{
var number = Console.ReadLine();
int n = Convert.ToInt32(number);
Pattern print = new Pattern();
print.StarNumberPyramid(n);
}
}
public class Pattern
{
public void StarNumberPyramid(int n)
{
int TotalNumberOfLetters = n*2+1;
int leftTriangleCreater = n+1;
int rightTriangleCreater = leftTriangleCreater;
int innerNumber=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=TotalNumberOfLetters;j++)
{
if(j<leftTriangleCreater || j>rightTriangleCreater)
{
Console.Write("*");
}
else
{
if(j==leftTriangleCreater || j==rightTriangleCreater || innerNumber==0)
{
Console.Write(i);
innerNumber=1;
}
else
{
Console.Write("*");
innerNumber=0;
}
}
}
Console.WriteLine();
leftTriangleCreater --;
rightTriangleCreater++;
}
}
}
public class Test
{
public static void Main()
{
var number = Console.ReadLine();
int n = Convert.ToInt32(number);
Pattern print = new Pattern();
print.StarNumberPyramid(n);
}
}
public class Pattern
{
public void StarNumberPyramid(int n)
{
int TotalNumberOfLetters = n*2+1;
int leftTriangleCreater = n+1;
int rightTriangleCreater = leftTriangleCreater;
int innerNumber=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=TotalNumberOfLetters;j++)
{
if(j<leftTriangleCreater || j>rightTriangleCreater)
{
Console.Write("*");
}
else
{
if(j==leftTriangleCreater || j==rightTriangleCreater || innerNumber==0)
{
Console.Write(i);
innerNumber=1;
}
else
{
Console.Write("*");
innerNumber=0;
}
}
}
Console.WriteLine();
leftTriangleCreater --;
rightTriangleCreater++;
}
}
}
OUTPUT:
*****1***** ****2*2**** ***3*3*3*** **4*4*4*4** *5*5*5*5*5*
Comments
Post a Comment