How to input multiple values from user in one line in C#?

CsharpProgrammingServer Side Programming

Use a while loop to input multiple values from the user in one line.

Let’s say you need to get the elements of a matrix. Get it using Console.ReadLine() as shown below −

Console.Write("\nEnter elements - Matrix 1 : ");
for (i = 0; i < m; i++) {
   for (j = 0; j < n; j++) {
      arr1[i, j] = Convert.ToInt16(Console.ReadLine());
   }
}

The following is an example showing how we can input multiple values from user −

Example

 Live Demo

using System;

namespace Demo {
   public class Program {
      public static void Main(string[] args) {
         int[,] arr1 = new int[10, 10];
         int i, j;

         Console.Write("\nEnter Matrix elements: ");
         for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
               arr1[i, j] = Convert.ToInt16(Console.ReadLine());
            }
         }

      }
   }
}

Output

Enter Matrix elements:
raja
Published on 31-Aug-2018 11:33:19
Advertisements