Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to input in an integer array

Tags:

c#

How can I take input in an array in C#?

I have written a code it's not working right. For example, if I input 1, then it gives an output 49.

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

namespace Google
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            int[] array=new int[26];
            Console.Write("Enter number of cases : ");
            n = Console.Read();
            for (int i = 0; i < n; i++)
            {
                array[i] = Console.Read();
                Console.WriteLine( array[i]);
            }
            Console.Read();
        }  
    }
}
like image 934
Shubham Marathia Avatar asked Dec 02 '22 15:12

Shubham Marathia


2 Answers

arr = Array.ConvertAll(Console.ReadLine().Trim().Split(' '),Convert.ToInt32);
like image 76
Sayak Sinha Avatar answered Dec 22 '22 22:12

Sayak Sinha


Console.Read returns the character code, not the number you entered.

Use int.Parse(Console.ReadLine()) instead:

n = int.Parse(Console.ReadLine());
//...
array[i] = int.Parse(Console.ReadLine());
like image 24
pascalhein Avatar answered Dec 22 '22 21:12

pascalhein