int num = Convert.ToInt32(Console.ReadLine());
With above code I am getting the correct integer value and I am able to do operation on this number and getting the correct result.
But with this below code why Convert.ToInt32(o) method is not converting it to integer value. Why we need to minus with 48.
int[] numarr = number.ToString().Select(o => Convert.ToInt32(o)-48).ToArray();
If I am not subtracting with 48 I am not getting the correct integer value.
Please can anyone explain why this is? Is it required to do every time? Because somewhere else I calculated result without subtracting 48 and I got a correct result.
I am doing a program to print number of occurrences in a number. Here is my code:
Console.WriteLine("Enter the number:");
int number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the number to search:");
int searchnumber = Convert.ToInt32(Console.ReadLine());
int cnt = 0;
int[] numarr = number.ToString().Select(o => Convert.ToInt32(o)-48).ToArray();
for (int i = 0; i < numarr.Length; i++)
{
Console.WriteLine(numarr[i]);
}
for (int i = 0; i < numarr.Length; i++)
{
if (numarr[i] == searchnumber)
{
cnt++;
}
}
Console.WriteLine("Number of occurence of given number is:{0}", cnt);