Find digits which has zeo

rteswaran

New member
Joined
Jan 4, 2025
Messages
2
Programming Experience
10+
I am looking solution for below requirements without using linq.

Return total count of zeros found between zero to 100.

Here we should consider less iteration when use for loop because let's say if I want to count for 10000 then loop will executed 10000 times
 
This seems pretty obviously to be homework, so we're not going to just cheat for you. You need to put some thought into the problem and come up with the logic, then try to implement that logic. If you have trouble at the implementation stage then post here and tell us what you did and whay happened and we can help you fix it. We can also help you with the logic part but, again, we're not here to just do it for you. You tell us what you're thinking and we can tell you whether you're on the right track, possibly giving you a point in the right direction if you're not
 
As a quick aside, all that LINQ does is hide the loops from you. The loops are still happening under the covers.

Also, consider that the problem is solvable without loops by using pure mathematical operations, but it is slightly easier by doing loops much like division can be done by doing the division operation, but can be done via repeated subtractions.
 
var count = 0;
var input = 10000;
for(var i=0; i<=input; i++){
var numberString = i.ToString();
foreach(var c in numberString){
if(c=='0'){
count++;
}
}
}

We can achieve using about code but I don't want to iterate for 10000 times
 
Have you considered this possibility?
Explanation:
  • Function declaration:
    The CountZerosBetweenZeroAndHundred function returns an integer which will be the total count of zeros found between 0 and 100.
  • Loop:
    The for loop iterates through numbers from 0 to 100 (inclusive).
  • Zero check:
    • i % 10 == 0: This condition checks if the units digit of the current number (i) is zero.
    • (i / 10) % 10 == 0: This condition checks if the tens digit of the current number is zero.
  • Incrementing count:
    If either condition is true, it means the number contains a zero, so the count variable is incremented by 1.
  • Return value:
    The function returns the final value of count, which represents the total number of zeros found between 0 and 100.


Key points:
  • This approach efficiently checks for zeros in both the units and tens place of each number between 0 and 100.
  • The modulo operator (%) is used to extract the digit in the desired place (units or tens).
  • This solution assumes that a zero is considered any time it appears as a digit in a number between 0 and 100, regardless of its position.
Return value::
int CountZerosBetweenZeroAndHundred()
{
    int count = 0;
    for (int i = 0; i <= 100; i++)
    {
        if (i % 10 == 0 || (i / 10) % 10 == 0) // Check if the units or tens digit is zero
        {
            count++;
        }
    }
    return count;
}

Example usage:

int result = CountZerosBetweenZeroAndHundred();
Console.WriteLine("Total zeros between 0 and 100: " + result); // Output: 20
 
@Rythorian : your approach is still the same as the OP's where you would need to loop N times which is what they are trying to avoid. Also note that there are two zeros in 100 but you are only counting it once.
 
Back
Top Bottom