Skip to content

Instantly share code, notes, and snippets.

@CSaratakij
Last active April 1, 2016 17:52
Show Gist options
  • Select an option

  • Save CSaratakij/e5272245edba4fde0db07b3ec2d09a32 to your computer and use it in GitHub Desktop.

Select an option

Save CSaratakij/e5272245edba4fde0db07b3ec2d09a32 to your computer and use it in GitHub Desktop.
My example of printing number.
using System;
using System.Collections.Generic;
namespace ExLoopFun
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the positive integer number : ");
var maxNum = 0;
var isNumber = int.TryParse(Console.ReadLine(), out maxNum);
if (isNumber && maxNum > 0)
{
var arryLength = (maxNum * 2) - 1;
var arryResult = new int[arryLength];
var listResult = new List<string>();
var start = 0;
var end = arryLength - 1;
//Init array by 0
for (int i = 0; i < arryResult.Length; i++)
{
arryResult[i] = 0;
}
//Add 1 to array in the specific range
for (int i = 0; i < maxNum; i++)
{
for (int n = start; n <= end; n++)
{
arryResult[n] += 1;
}
//Collect all the element, convert to string and add result to list
var text = "";
for (int c = 0; c < arryResult.Length; c++)
{
text += arryResult[c].ToString();
}
listResult.Add(text);
//Shift the range of assign's index
start += 1;
end -= 1;
}
//Print linear
for (int q = 0; q < listResult.ToArray().Length; q++)
{
Console.WriteLine(listResult.ToArray()[q]);
}
//Print reverse + avoid last index
for (int y = listResult.ToArray().Length - 2; y >= 0; y--)
{
Console.WriteLine(listResult.ToArray()[y]);
}
Console.ReadLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment