Step 1:- Method
public void PrintPyramid1()
{
int c
= 1;
for (int i
= 1; i <= 5; i++)
{
for (int j
= i; j < 5; j++)
{
Console.Write("
");
}
for (int k
= 1; k <= c; k++)
{
if (k
% 2 == 0)
Console.Write("
");
else Console.Write("*");
}
Console.WriteLine();
c
+= 2;
}
}
public void PrintPyramid2()
{
Console.WriteLine("--------------------------------Ascending");
for (int a
= 1; a <= 5; a++)
{
for (int b
= 1; b <= a; b++)
{
Console.Write(b);
}
Console.WriteLine("");
}
Console.WriteLine("--------------------------------Descending");
for (int a
= 5; a > 0; a--)
{
for (int b
= 1; b <= a; b++)
{
Console.Write(b);
}
Console.WriteLine("");
}
Console.WriteLine("--------------------------------Reverse");
for (int a
= 1; a <= 5; a++)
{
for (int b
= a; b > 0; b--)
{
Console.Write(b);
}
Console.WriteLine("");
}
}
Step 2:- Complete code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_Application_Demo
{
class Program
{
public void PrintPyramid1()
{
int c
= 1;
for (int i
= 1; i <= 5; i++)
{
for (int j
= i; j < 5; j++)
{
Console.Write("
");
}
for (int k
= 1; k <= c; k++)
{
if (k
% 2 == 0)
Console.Write("
");
else Console.Write("*");
}
Console.WriteLine();
c
+= 2;
}
}
public void PrintPyramid2()
{
Console.WriteLine("--------------------------------Ascending");
for (int a
= 1; a <= 5; a++)
{
for (int b
= 1; b <= a; b++)
{
Console.Write(b);
}
Console.WriteLine("");
}
Console.WriteLine("--------------------------------Descending");
for (int a
= 5; a > 0; a--)
{
for (int b
= 1; b <= a; b++)
{
Console.Write(b);
}
Console.WriteLine("");
}
Console.WriteLine("--------------------------------Reverse");
for (int a
= 1; a <= 5; a++)
{
for (int b
= a; b > 0; b--)
{
Console.Write(b);
}
Console.WriteLine("");
}
}
static void Main(string[]
args)
{
Program p
= new Program();
p.PrintPyramid1();
p.PrintPyramid2();
Console.ReadLine();
}
}
}
Example:-
No comments:
Post a Comment