I have this problem, I guess it's simple but I have not been able to solve it and I have one day in that, I have a program that accepts 10 numbers and enters them in a vector, but I need to know how many of these 10 start with the number 3 , when I do the code I know all three there are, if for example "33" is entered it says that two numbers start at 3, I have done it using string and int with the same result. If you could help me, Thank you.
using System;
public class Example{
public static void Main(){
Console.Clear();
Console.WriteLine("Programa que acepta 10 numeros y muestra cuantos de estos empiezan con 3.");
Console.WriteLine();
int[] n1 = new int [10];
int cont = 0;
for(int x = 0; x < 10; x++){
Console.Write("Digite el {0} numero: ",x+1);
int.TryParse(Console.ReadLine(), out n1[10]);
int z = n1[x]%10;
if(z == 3){
cont++;
}
}
Console.Write(cont);
Console.ReadKey();
}
}
With string
class programa22{
public static void Main(){
Console.Clear();
Console.WriteLine("Programa que acepta 10 numeros y muestra cuantos de estos empiezan con 3.");
Console.WriteLine();
string[] n1 = new string[10];
int cont = 0;
for(int x = 0; x < 10; x++){
Console.Write("Digite el {0} numero: ",x+1);
n1[x] = Console.ReadLine();
char delimiter = '3';
string[] substrings = n1[x].Split(delimiter);
foreach (var substring in substrings)
cont++;
}
Console.WriteLine();
cont = cont - 10;
Console.Write("La cantidad de numeros que empiezan con 3 es "+cont);
Console.ReadKey();
}
}