public static int FindNumberCount(int lowerIndex, int upperIndex, char toMatch) { var count = Enumerable.Range(lowerIndex, upperIndex).Sum(x => { return x.ToString().ToCharArray().Count(y => y == toMatch); }); return count; }
public static int FindNumberCount(int lowerIndex, int upperIndex, char toMatch)
{
var count = Enumerable.Range(lowerIndex, upperIndex).Sum(x =>
return x.ToString().ToCharArray().Count(y => y == toMatch);
});
return count;
}
int count = 0;for (int i=100;i<=10000;i++){ char[] number = i.ToString().ToCharArray(); for (int j=0;j<number.Length;j++) { // Console.WriteLine(number[j]); if (number[j].ToString() == “9”) count++; }
} Console.Write(count); //3980
--Loop this function for every number from 100 to 10000static void ConvertToInt(Int32 iNum, int iNoToFind,ref int iCnt){ Int32 iDig = Convert.ToInt32(Math.Floor(Math.Log10(iNum)));Int32 iDiv =Convert.ToInt32(Math.Pow(10, iDig));if(iNum/iDiv ==iNoToFind){iCnt++; }if(iNum/10 > 0 && iNum%iDiv > 0)ConvertToInt( iNum%iDiv, iNoToFind, ref iCnt);}
--looped in SQL, can be done similarly in c# as well declare @min int = 0,@max int=100,@ctr int =0,@nines int = 0,@str nvarchar(10)=''while (@min<=@max) Begin set @str = convert(nvarchar(10),@min) set @nines = @nines + (len(@str)-len(replace(@str,'9',''))) set @min=@min+1 End select @nines
public static void Main() { int a = 100; int b = 10000; int count = 0; int c = 0; int x = 0; for (int i = a; i <= b; i++) { c = i; while (c != 0) { x = c % 10; c = c / 10; if (x == 9) { count++; } } } Console.WriteLine(count); }
public static void Main() {int a = 100;int b = 10000;int count = 0;int c = 0;int x = 0;for (int i = a; i <= b; i++){c = i;while (c != 0){x = c % 10;c = c / 10;if (x == 9){count++;}}}Console.WriteLine(count); }