c# program
write a program to count number of zero between 1 to 1000 using c#
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Aug 30, 2013, 11:04 AM
int numZero = Enumerable.Range(1, 1000).Sum(n => n.ToString().Count(c => c == '0'));
Although this isn't the most efficient way to do it, the logic is to:
1. Convert each number between 1 and 1000 to a string
2. Count how many characters are '0' in this string.
3. Sum the results of step 2.
Assuming you're not allowed to use LINQ, I'd reproduce this logic using traditional code.
Nitesh KejriwalPosted Aug 30, 2013, 8:27 AM