need help with simple Visual c++ prog

piski

Senior member
Jan 21, 2002
312
0
0
I need to write a program that identifies the number of trailing zeroes in a number

ex. 1000 has 3 trailing zeroes and 1020 has 1 trailing zero..

I decided to set the limit between 1 and 100000. the only thing that I could think of is setting up a set of nested if statements like so

if (number < 100)
{
if (number%10 == 0)
cout << "the number has one trailing zero" <<endl;

the only thing is this can become quite repetetive as i am repeating the same steps for higher numbers..does anyone have any advice? i would greatly appreciate it
K
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: AgentEL
use a while loop and decrement the "number" by dividing by 10
was just going to suggest this as method 2, though it assumes you know what (n % 10) does.

 

piski

Senior member
Jan 21, 2002
312
0
0
1st..I dont understand how decrementing the number by 10 will help to find the number of zeroes at the end..

i see like 10/10 = 1 and but like 100/10 = 10. where does it tell you the number of zeroes there..sorry for being dumb..

2nd. would the while loop be like this?
while (number != 1)
{
statement
}??

thanks in advance
K
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
Originally posted by: piski
1st..I dont understand how decrementing the number by 10 will help to find the number of zeroes at the end..

i see like 10/10 = 1 and but like 100/10 = 10. where does it tell you the number of zeroes there..sorry for being dumb..

2nd. would the while loop be like this?
while (number != 1)
{
statement
}??

thanks in advance
K

hehe well, without giving away the entire thing:

Say you are given the number 132,000.

Your if statement is correct.

if (132,000 % 10 == 0) // you return true
you have a zero at the end

in the next iteration of the while loop, you want to check:

if (13,200 % 10 ==0)
you have another zero at the end

repeat, until the while loop fails ;-)
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
^ if you use this be sure you understand what "%" is and how it works here, in case you're asked to explain your work. Besides, "%" is a handy operator to understand for future use.
 

Zombie

Platinum Member
Dec 8, 1999
2,359
1
71
or convert to string and check the characters, should be easy if you are allowed MFC.
 

piski

Senior member
Jan 21, 2002
312
0
0
Alright well this is what I did
int j, zeroCount;
cout << "Enter another number: ";
cin >> j

while (j >= 1)
{
if (j%10 == 0)
{
zeroCount++
j = j/10
}
else
cout << "There are no trailing zeroes";



and of course its not working
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
looks close, but not quite.

You still want to run: j = j/10 even if the if condition fails.

and i'm assuming you left out a bunch of braces and semicolons out for brevity?
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
your while condition j >= 1 depends on that j variable. If the number ends with a non-zero, it will fail the if condition, go to the else, print out the cout line and the j stays the same.

you will get an infinite loop.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Also think about zerocount, what value does it have before the while?

You're on the right track, working through the logic and finding your bugs is how you'll learn to do something like this without help next time.

Hopefully no one will step in and fix it for you, since you really do need to learn to fix and finish it yourself.
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
best way to code this up is pick a number first and run it through your program. just do it on paper. don't even bother compiling/running anything.

a lot of the questions you have will be solved this way.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
running through the code on paper or even in the debugger (stepping one line at a time) could help -- if you'd done that you'd see why you always need to have j = j/10 .

Edit: great minds think alike :)
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
Originally posted by: DaveSimmons
running through the code on paper or even in the debugger (stepping one line at a time) could help -- if you'd done that you'd see why you always need to have j = j/10 .

haha i like the way you think
 

piski

Senior member
Jan 21, 2002
312
0
0
can someone tell me why i am getting an infinite loop out of this?
while (answer != 'Y' ||answer != 'N')
{
cout << "you have entered the wrong letter. please reenter: ";
cin >> answer;
}