Here is my code to draw a star..But i have a problem..its printing only half star.

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;


namespace triangle
{
class Program
{
int k = int.Parse(Console.ReadLine());


static void Main(string[] args)
{
Console.WriteLine("Enter The Value:");
int k = int.Parse(Console.ReadLine());
int m = k;
int n = k - 1;
int y = n;
int x = 2 * (k - 1) + 1;
int q = x;
// code for writing triangle


for (int p = 0; p <= n; p++)
{
for (int j = k - 1; j >= 0; j--)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" ");
}
for (int i = 0; i <= (x - 2 * (k - 1)); i++)
{


if ((i % 2 == 1 && i == 1) || (i % 2 == 1 && i == (x - 2 * (k - 1))) || (i % 2 == 1 && p == n))
{


Console.Write("*");
}
else
{


Console.Write(" ");
}
}
Console.WriteLine();
k--;
}
// the parameter values will be changed hence initializing again
k = m;
x = q;
n = y;
Console.SetCursorPosition(0,((k/2)));


// code for writing reverse triangle
for (int p = n; p >= 0; p--)
{
for (int j = k-1; j <= n+1; j++)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(" ");
}
for (int i = x; i >= (x - 2 * (k - 1)); i--)
{


if ((i % 2 == 1 && i == x) || (i % 2 == 1 && i == (x - 2 * (k - 1))) || (i % 2 == 1 && p == n))
{
Console.Write("*");
}
else
{
if (Console.CursorVisible == true)
{
Console.Write(" ");
}
else
{
Console.Write("*");
}
}
}
Console.WriteLine();
k--;


}
Console.ReadLine();


}


}
}

Printing Green part of star also prints spaces over red star. Red and Green stars should print at same print, or you had to just move cursor instead printing space.
I tried it basicaally worked, but some of code had to rewrite.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.