Find Number of 0's at end of int

I wanted to find out the number of 0's at end of integer. Suppose anyone enter 2020 it should count 1,if number is 2000 it should show 3 etc;

I tried following but doesn't accomplish what i want :(

Console.WriteLine("Enter Number :");
int num = int.Parse(Console.ReadLine());
int count = 0;

for (int i = 1; i < num.ToString().Count(); i++)
{
   //some logic
}

Console.WriteLine("Zero in the tail is :");
Console.WriteLine(count);
Jon Skeet
people
quotationmark

You're not changing anything within your loop - so basically, on each iteration it will either increase Count or it won't, and it'll do the same thing each time - so Count will either be the length of the string, or it will be 0.

The simplest option I can think of in terms of text manipulation would be:

string text = num.ToString();
int count = text.Length - text.TrimEnd('0').Length;

Without using text manipulation, however, you could just use division and remainder operations:

int count = 0;
// Keep going while the last digit is 0
while (num > 0 && num % 10 == 0)
{
    num = num / 10;
    count++;
}

Note that this will yield a count of 0 for the number 0... whereas the first approach will give a count of 1 (because 0.ToString() is "0"). Adjust either piece of code to suit your requirements :)

people

See more on this question at Stackoverflow