counte no of digits

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

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 2342 Views - Last Post: 24 July 2010 - 05:30 AM Rate Topic: -----

#1 pawanoxy  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 10
  • Joined: 21-July 10

counte no of digits

Posted 21 July 2010 - 03:47 AM

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

piz give me the code
Is This A Good Question/Topic? 0
  • +

Replies To: counte no of digits

#2 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: counte no of digits

Posted 21 July 2010 - 03:49 AM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 pawanoxy  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 10
  • Joined: 21-July 10

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: :code:.
Was This Post Helpful? 0
  • +
  • -

#4 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: counte no of digits

Posted 21 July 2010 - 04:14 AM

Hint :- You may need two for loops.
Was This Post Helpful? 0
  • +
  • -

#5 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: counte no of digits

Posted 21 July 2010 - 04:17 AM

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

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

Was This Post Helpful? 0
  • +
  • -

#6 pawanoxy  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 10
  • Joined: 21-July 10

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...
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10775
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

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;
}


Was This Post Helpful? 0
  • +
  • -

#8 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

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:

    	 //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

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10775
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

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. :)
Was This Post Helpful? 0
  • +
  • -

#10 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

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
Was This Post Helpful? 1
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10775
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

Re: counte no of digits

Posted 21 July 2010 - 06:38 AM

View PostLuckless, on 21 July 2010 - 09:36 AM, said:

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

:^:
Was This Post Helpful? 0
  • +
  • -

#12 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2066
  • View blog
  • Posts: 4,299
  • Joined: 11-December 07

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:

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.
Was This Post Helpful? 1
  • +
  • -

#13 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: counte no of digits

Posted 21 July 2010 - 07:49 AM

View Postcfoley, 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);


Was This Post Helpful? 2
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10775
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

Re: counte no of digits

Posted 21 July 2010 - 08:08 AM

I really need to spend some time picking up regex.
Was This Post Helpful? 0
  • +
  • -

#15 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

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.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3