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();
}
}
}
arr = Array.ConvertAll(Console.ReadLine().Trim().Split(' '),Convert.ToInt32);
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With