Pages

Friday, January 28, 2011

C sharp : To print dynamic pyramid.

Now , You can play with pyramids and many different shapes from below mentioned programmed :-


Program(1): To print 1 * 10 Counting Table.

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


namespace pyramid
{
    class Program
    {
        static void Main(string[] args)
        {           
            int k, l;
            for (k = 1; k < 11; k++)
            {
                for (l = 1; l < 11; l++)
                {
                    Console.Write("  |  ");
                    Console.Write(k * l);
                }
                Console.WriteLine("\n");
            }
            }
    }
}



Program(2): To print pyramid as you like.

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

namespace pyramid
{
    class Program
    {
        static void Main(string[] args)
        {           



            {
                Console.WriteLine("Enter the value ");
                int k = int.Parse(Console.ReadLine());
                int n = k - 1;
                int x = 2 * (k - 1) + 1;
                for (int p = 0; p <= n; p++)
                {
                    for (int j = k - 1; j >= 0; j--)
                    {
                        Console.Write(" ");
                    }
                    for (int i = 0; i <= (x - 2 * (k - 1)); i++)
                    {
                        if (i % 2 == 1)
                        {
                            Console.Write("*");
                        }
                        else
                        {
                            Console.Write(" ");
                        }
                    }
                    Console.WriteLine();
                    k--;
                }
                Console.ReadLine();
            }
           }
    }
}


Program(3): To print Tower of Hanoui....

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

namespace pyramid
{
    class Program
    {
        static void Main(string[] args)
        {           

            try
            {
                Console.Write("Enter the for Tower of Hanaui: ");
                int n = Convert.ToInt32(Console.ReadLine());
                for (int i = 1; i <= n; i++)
                {
                    for (int j = n; j >= i; j--)
                    {
                        Console.Write(" ");
                    }
                    for (int k = 1; k <= i; k++)
                    {
                        Console.Write("*");
                    }
                    for (int m = 2; m <= i; m++)
                    {
                        Console.Write("*");
                    }
                    Console.WriteLine();
                    for (int a = 1; a <= n; a++)
                    {
                        for (int j = n; j >= i; j--)
                        {
                            Console.Write(" ");
                        }
                        for (int k = 1; k <= i; k++)
                        {
                            Console.Write("*");
                        }
                        for (int m = 2; m <= i; m++)
                        {
                            Console.Write("*");
                        }
                        Console.WriteLine();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }           
       }
    }




Start Program(4): To print Zigzag Tower

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

namespace pyramid
{
    class Program
    {
        static void Main(string[] args)
        {    

            try
            {
                Console.Write("Enter number for Zigzag Tower: ");
                int n = Convert.ToInt32(Console.ReadLine());
                for (int i = 1; i <= n; i++)
                {
                    for (int j = n; j >= i; j--)
                    {
                        Console.Write(" ");
                    }
                    for (int k = 1; k <= i; k++)
                    {
                        Console.Write("*");
                    }
                    for (int m = 2; m <= i; m++)
                    {
                        Console.Write("*");
                    }
                    Console.WriteLine();
                    for (int a = 1; a <= n; a++)
                    {
                        for (int b = n; b >= a; b--)
                        {
                            Console.Write(" ");
                        }
                        for (int k = 1; k <= i; k++)
                        {
                            Console.Write("*");
                        }
                        for (int m = 2; m <= i; m++)
                        {
                            Console.Write("*");
                        }
                        Console.WriteLine();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }           
      }
   }
}



Program(5): To print Nexted Pyramid...

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

namespace pyramid
{
    class Program
    {
        static void Main(string[] args)
        {    

            try
            {
                Console.Write("Enter number for nexted pyramid: ");
                int n = Convert.ToInt32(Console.ReadLine());
                for (int i = 1; i <= n; i++)
                {
                    for (int j = n; j >= i; j--)
                    {
                        Console.Write(" ");
                    }
                    for (int k = 1; k <= i; k++)
                    {
                        Console.Write("*");
                    }
                    for (int m = 2; m <= i; m++)
                    {
                        Console.Write("*");
                    }
                    Console.WriteLine();
                    for (int a = 1; a <= n; a++)
                    {
                        for (int b = n; b >= a; b--)
                        {
                            Console.Write(" ");
                        }
                        for (int c = 1; c <= a; c++)
                        {
                            Console.Write("*");
                        }
                        for (int d = 2; d <= a; d++)
                        {
                            Console.Write("*");
                        }
                        Console.WriteLine();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }           
       }
   }
}



Program(6): To print two simultaneouly Pyramid...

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

namespace pyramid
{
    class Program
    {
        static void Main(string[] args)
        {    

            try
            {
                Console.Write("Enter number for simultaneously pyramid: ");
                int n = Convert.ToInt32(Console.ReadLine());
                for (int i = 1; i <= n; i++)
                {
                    for (int j = n; j >= i; j--)
                    {
                        Console.Write(" ");
                    }
                    for (int k = 1; k <= i; k++)
                    {
                        Console.Write("*");
                    }
                    for (int m = 2; m <= i; m++)
                    {
                        Console.Write("*");
                    }
                    Console.WriteLine();
                }
                {
                    for (int a = 1; a <= n; a++)
                    {
                        for (int b = n; b >= a; b--)
                        {
                            Console.Write(" ");
                        }
                        for (int c = 1; c <= a; c++)
                        {
                            Console.Write("*");
                        }
                        for (int d = 2; d <= a; d++)
                        {
                            Console.Write("*");
                        }
                        Console.WriteLine();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }           
       }
    }
}


  I hope you enjoy and learn lots of programming concepts; basically looping concepts. You can contact us through this blog or email me at: ksantosh.mca@gmail.com
Thanks, Happy programming.. 


S. K. Singh

No comments:

Post a Comment