i want to count the number of zero from 1 to 1000 numbers in java piz ....

piz give me the code

# counte no of digits

## i want to count the number of zero from 1 to 1000 numbers in java piz

## 33 Replies - 3065 Views - Last Post: 24 July 2010 - 05:30 AM

##
**Replies To:** counte no of digits

### #3

## Re: counte no of digits

Posted 21 July 2010 - 04:10 AM

class countnumber { public static void main(String args[]) { int i; int l=0; for(i=1;i<=1000;i++) { if(i%10==0) l=l+1; System.out.println(" no of zeros in this string is:"+l); }}}

that is my code now piz help me

**Edited by**macosxnerd101: Welcome to DIC! Please remember to post your code using code tags, like so: .

### #4

## Re: counte no of digits

Posted 21 July 2010 - 04:14 AM

Hint :- You may need two for loops.

### #5

## Re: counte no of digits

Posted 21 July 2010 - 04:17 AM

Well, your variable i can be declared inside the for loop:

You have half of the determining # of zeroes part:

You are correct that if a number mod 10 returns 0, that number has a 0. The problem you did not consider was that the numbers in the hundreds also have 0s, two of them to be precise.

Modify your condition so that you check if the number mod 100 returns 0.

Also, you might as well make your # of zeroes variable start at 3, and make your loop with condition unless you want to check for 1000 every time....

Edit::

I forgot that 0s may exist in numbers such as 101, 903, etc.

To solve that, you would add another condition after your if/else. If number mod 10 does not equal 0 BUT number is below the 10s, you increment by 1.

If you need help, here's a basic approach:

for(int i = 1;.......)

You have half of the determining # of zeroes part:

You are correct that if a number mod 10 returns 0, that number has a 0. The problem you did not consider was that the numbers in the hundreds also have 0s, two of them to be precise.

Modify your condition so that you check if the number mod 100 returns 0.

if(number % 100 == 0) zeroes += 2 // you do not check for 100s again b/c you had it earlier else if(number % 10 == 0) zeroes++;

Also, you might as well make your # of zeroes variable start at 3, and make your loop with condition unless you want to check for 1000 every time....

// remove the inclusive operator for(....; i < 1000; ....)

Edit::

I forgot that 0s may exist in numbers such as 101, 903, etc.

To solve that, you would add another condition after your if/else. If number mod 10 does not equal 0 BUT number is below the 10s, you increment by 1.

If you need help, here's a basic approach:

if() else if() else if(number % 10 != 0 && number % 100 < 10 && number > 100)

This post has been edited by **bcranger**: 21 July 2010 - 04:29 AM

### #6

## Re: counte no of digits

Posted 21 July 2010 - 04:20 AM

m having problem in that code,,,,,m not getting the number of zeros.....n new to c++ and java so can you please help me.....thanks in advance...

### #7

## Re: counte no of digits

Posted 21 July 2010 - 06:17 AM

You may want to use Strings here, or at least make more use of the modulus and division operations with ints to extract individual digits. With Strings, you can extract individual chars using the charAt() method, and compare them to '0'. So if you go the int route, I'd do something like this:

public int numZeros(int x){ int count = 0; while(x > 0){ int digit = x%10; //get the last digit in x, or the remainder when x is divided by 10 //check to see if digit == 0 //and increment count if necessary //divide x by 10 } return count; }

### #8

## Re: counte no of digits

Posted 21 July 2010 - 06:28 AM

instead of all these elses and if for different conditions, why not have a for loop and one if statement:

if I was going to 101, the numbers are 0,10,20,30,40,50,60,70,80,90,100,101.

12 is our output =)

Ninja'd by Mac. But to go on what he said, This utilizes Strings and doesn't require the mod (though you can easily add that in there)

//i from our outside loop for(int j = 0; j < String.valueOf(i).trim().length(); j++){ if(String.valueOf(i).trim().charAt(j) == '0'){ zeroCounter++; } }

if I was going to 101, the numbers are 0,10,20,30,40,50,60,70,80,90,100,101.

12 is our output =)

Ninja'd by Mac. But to go on what he said, This utilizes Strings and doesn't require the mod (though you can easily add that in there)

This post has been edited by **Luckless**: 21 July 2010 - 06:33 AM

### #9

## Re: counte no of digits

Posted 21 July 2010 - 06:33 AM

Your solution is a little easier to implement, but mine is more memory efficient b/c you don't have to worry about creating n Strings.

### #10

## Re: counte no of digits

Posted 21 July 2010 - 06:36 AM

haha, this is true, but I thought I'd show the other way anyways

### #11

## Re: counte no of digits

Posted 21 July 2010 - 06:38 AM

### #12

## Re: counte no of digits

Posted 21 July 2010 - 07:04 AM

Damn, I thought Macos had posted the String method and I had written up the div...mod method up ready to post. Oh well, I'll post a slight optimisation instead:

1..9 have no zeros so once we get down to a single digit we can stop checking. For the set of numbers 1..1000, this removes 1000 div calculations and 1000 mod calculations.

while(x > 9){

1..9 have no zeros so once we get down to a single digit we can stop checking. For the set of numbers 1..1000, this removes 1000 div calculations and 1000 mod calculations.

### #13

## Re: counte no of digits

Posted 21 July 2010 - 07:49 AM

cfoley, on 21 July 2010 - 01:04 PM, said:

Damn, I thought Macos had posted the String method and I had written up the div...mod method up ready to post. Oh well, I'll post a slight optimisation instead:

Always the regex solution left

int a =40500; Pattern p = Pattern.compile("[0]"); Matcher m = p.matcher(String.valueOf(a)); int count=0; while(m.find()){ count++; } System.out.println(count);

### #14

## Re: counte no of digits

Posted 21 July 2010 - 08:08 AM

I really need to spend some time picking up regex.

### #15

## Re: counte no of digits

Posted 21 July 2010 - 08:24 AM

Yeah, it's pretty neat some of the stuff you can do using regex especially when it comes to the use of validation within software.