counte no of digits
i want to count the number of zero from 1 to 1000 numbers in java piz
33 Replies  2474 Views  Last Post: 24 July 2010  05:30 AM
#1
counte no of digits
Posted 21 July 2010  03:47 AM
piz give me the code
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
#5
Re: counte no of digits
Posted 21 July 2010  04:17 AM
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
#7
Re: counte no of digits
Posted 21 July 2010  06:17 AM
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
//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
#10
Re: counte no of digits
Posted 21 July 2010  06:36 AM
#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
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:
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
#15
Re: counte no of digits
Posted 21 July 2010  08:24 AM
