This is one of the subjective programming question of Nagarro.
Input String :
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
Your Output should be:
1 4 7 8 5 2 3 6 9

using System;
namespace MatrixWave
{
class Program
{
int[,] mat = new int[3, 3];
static void Main(string[] args)
{
Program p1 = new Program();
p1.Input();
Console.WriteLine("The Inputed Matrix ::");
p1.Show();
Console.WriteLine(":: Output ::");
p1.Wave();
Console.ReadLine();
}
/// <summary>
/// For taking input from user for the matrix
/// </summary>
public void Input()
{
Console.WriteLine("Enter 9 elements for a 3X3 Matrix");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
mat[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
}
/// <summary>
/// Show the inputted elements in matrix form
/// </summary>
public void Show()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(mat[i, j] + "\t");
}
Console.WriteLine("\n");
}
}
/// <summary>
/// Show the matrix in a wave like form
/// </summary>
public void Wave()
{
int j = 0;
int count = 0;
do
{
int i = 0;
if (j < 3)
{
for (int line = 0; line < 3; line++)
{
Console.Write(mat[i, j] + " ");
i++; count++;
}
}
i = 2; j++;
if (j < 3)
{
for (int line = 0; line < 3; line++)
{
Console.Write(mat[i, j] + " ");
i--; count++;
}
j++;
}
} while (count < 9);
}
}
}