Write a code snippet to print the given jagged array using foreach loop?

Tripati_tutu
Posted by Tripati_tutu under C# category on | Points: 40 | Views : 3206
The output should be
10 20 30 40 50
10 20 30 40
10 20 30

Below is the code snippet to get the above jagged array.

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

namespace JaggedArray
{
class Program
{
static void Main(string[] args)
{
int[][] x = new int[3][];
x[0] = new int[5] { 10, 20, 30, 40, 50 };
x[1] = new int[4] { 10, 20, 30, 40 };
x[2] = new int[3] { 10, 20, 30 };
Console.WriteLine("Printing elements using foreach loop...");
foreach (int[] i in x)
{
foreach (int k in i)
Console.Write(k + " ");
Console.WriteLine();
}
Console.ReadKey();
}
}
}

Comments or Responses

Login to post response