Here is source code to print Diamond in c# using for loop. This
c# code is compiled successfully and executed with Microsoft visual studio
2010.
class Program
{
static void Main()
{
int number, i, k, count = 0;
Console.WriteLine("Enter
no of rows........");
number = Convert.ToInt32(Console.ReadLine());
count = number - 1;
for (k = 1; k <= number; k++)
{
for (i = 1; i <= count; i++)
{
Console.Write("
");
}
count--;
for (i = 1; i <= 2 * k - 1; i++)
Console.Write("*");
Console.WriteLine();
}
count = 1;
for (k = 1; k <= number - 1; k++)
{
for (i = 1; i <= count; i++)
Console.Write("
");
count++;
for (i = 1; i <= 2 * (number - k) - 1; i++)
Console.Write("*");
Console.WriteLine();
}
Console.ReadKey();
}
}
Here is the output of the program.
*
***
*****
***
*
*
No comments:
Post a Comment
Note: only a member of this blog may post a comment.