loop in C# Console Program ( For loop, foreach loop ) - C# Tutorials

Latest

For Video tutorials Visit my Channel https://www.youtube.com/channel/UCVWpZVbMqLHuQBigIfc9I8Q

Friday, 13 January 2017

loop in C# Console Program ( For loop, foreach loop )

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


namespace Array
{
    class Program
    {
        static void Main(string[] args)
        {
          
            Console.WriteLine("Enter 1 for For loop Array");
            Console.WriteLine("Enter 2 for Foreach loop Array");
            Console.WriteLine("Enter 3 for Exit");
            Console.WriteLine("Enter your Option");

            int op = Convert.ToInt16(Console.ReadLine());
            Console.Clear();
            switch(op)
            {
                case 1:
                    {
                        int[] n = new int[10]; /* n is an array of 10 integers */
                        int i, j;
                        /* initialize elements of array n */
                        for (i = 0; i < 10; i++)
                        {
                            n[i] = i + 100;
                        }
                        /* output each array element's value */
                        for (j = 0; j < 10; j++)
                        {
                            Console.WriteLine("Element[{0}] = {1}", j, n[j]);
                        }
                        break;
                    }
                case 2:
                    {
                        int[] n = new int[10]; /* n is an array of 10 integers */
                        /* initialize elements of array n */
                        for (int i = 0; i < 10; i++)
                        {
                            n[i] = i + 100;
                        }
                        /* output each array element's value */
                        foreach (int j in n)
                        {
                            int i = j - 100;
                            Console.WriteLine("Element[{0}] = {1}", i, j);
                            i++;
                        }
                        break;
                    }
                case 3:
                    {
                        System.Environment.Exit(1);
                        break;
                    }
            }

            Console.ReadKey();
        }
    }

}

No comments:

Post a Comment