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


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("
Enter 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("
Enter 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:

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements