18 Mei 2014

Cara membuat tampilan belah ketupat pada C#

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

namespace ConsoleApplication27
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 10;
            int a, b, c;
            a = 1; c = n - 1;


            for (int i = 0; i < n * 2 - 1; i++)
            {

                if (i < n)
                {

                    for (int j = 0; j < n * 2 - 1; j++)
                    {
                        b = n - a;

                        if ((j < b) || (j >= (b + 2 * a - 1)))
                        {
                            Console.Write("@");
                        }
                        else
                        {
                            Console.Write(" ");
                        }
                    }
                    a = a + 1;
                }


                else
                {

                    for (int j = 0; j < n * 2 - 1; j++)
                    {
                        b = n - c;

                        if ((j < b) || (j >= (b + 2 * c - 1)))
                        {
                            Console.Write("@");
                        }
                        else
                        {
                            Console.Write(" ");
                        }
                    }
                    c = c - 1;
                }


                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }

}