Console application to take matrix as input and stored in text or word document as matrix format

Karthikreddy
Posted by Karthikreddy under C# category on | Points: 40 | Views : 1761
//most asked question in system testing round while we facing any interview

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

namespace example
{
class Program
{
static void Main(string[] args)
{
try
{
string r;
string c;
int m;
int n;
int num_of_rows;
int num_of_colums;
int[,] a = new int[3, 3];
int k;
string b;
//int[,] b = new int[3, 3];2314
StreamWriter sw;
Console.WriteLine("Enter the Number of rows:");
r = Console.ReadLine();
num_of_rows = Convert.ToInt32(r);
Console.WriteLine("Enter the number of colums:");
c = Console.ReadLine();
num_of_colums = Convert.ToInt32(c);
Console.WriteLine("Enter The Matrix Values");
for (m = 0; m < num_of_rows; m++)
{
for (n = 0; n < num_of_colums; n++)
{
b = Console.ReadLine();
k = Convert.ToInt32(b);
a[m, n] = k;
}
}
Console.WriteLine("Enter The file name to store matrix");
string Fname = Console.ReadLine();
string filename = @"I:\karthik\" + Fname + ".txt"; //for text file
string filename = @"I:\karthik\" + Fname + ".doc"; //for word document
sw = File.CreateText(filename);
for (m = 0; m < num_of_rows; m++)
{
for (n = 0; n < num_of_colums; n++)
{
//string z = "1 ";
Console.Write("{0} ", a[m, n]);

sw.Write("{0} ", a[m, n]); // printed into doc file

}
Console.WriteLine("\n");
sw.WriteLine("\n");

}

Console.WriteLine("File " + Fname + " Is created Successfully");
Console.Write("Enter any key to close....");
Console.Read();

sw.Close();

}
catch (Exception ex)
{
Console.WriteLine("exception occured " + ex.Message);
Console.Write("Enter any key to close....");
Console.Read();
}
}
}
}

Comments or Responses

Login to post response