# aughhhhhhhhhhhhhhhhhhhh!!! (program problems)

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-22-2002
JohnMayer
help!!! (program problems)
5. Write a program that determines the number of digits in a number:

Enter a number: 374
The number 374 has 3 digits

You may assume that the number has no more than 4 digits. HINT: Use if statements to test the number. For example, if the number is between 0 and 9, it has one digit, in between 10 and 99 = 2 digits, etc.

... i havent been able to get anything right for this code except:

/* Program that determines the number of digits in a number */
/* DATE: 07-22-02 */

#include <stdio.h>

int main()
{
int number;

printf("Enter a number: ");
scanf("%d", & number);

... here's where it gets loco

if (0>= number>= 9);
printf("The number %d has 1 digit", number);

.... am i going ab this totally wrong or what?
helppppppppppppp!!!
• 07-22-2002
Hammer
Re: aughhhhhhhhhhhhhhhhhhhh!!! (program problems)
Quote:

Originally posted by JohnMayer
5. Write a program that determines the number of digits in a number:

Enter a number: 374
The number 374 has 3 digits

You may assume that the number has no more than 4 digits. HINT: Use if statements to test the number. For example, if the number is between 0 and 9, it has one digit, in between 10 and 99 = 2 digits, etc.

If you have to use the if statements, as suggested by the hint, it's going to be a bit lame, there are better ways. Just keeping dividing the number by 10, counting the number of times you can do it..... I'll leave the exact details for you to work out.

[EDIT]And please use the subject field for something constructive, other than venting your anger!
• 07-22-2002
Cshot
You may want to write valid statements.
Quote:

if (0>= number>= 9);
if(number >= 0 && number <= 9)
...
The divide by 10 method is a good way to go. However, with a limitation of 4 digits, I'm sure this problem was meant to get you into thinking about the uses of the if statement.
• 07-22-2002
JohnMayer
thanks hammer & cshot (cshot, your advice ab the && helped... i didn't know you had to write out number twice!!! but thanks so much you guys~!!!!
• 07-22-2002
quzah
Hehehe...

sprintf( buf, "%d", x );

Or, if it's being read directly into a buffer, you can omit that.
Code:

```if( strlen( buffer ) < 2 )     printf("%d is one digit in length.\n", x ); else if( strlen( buffer ) < 3 )     printf("%d is two digits in length.\n", x ); else if( strlen( buffer ) < 4 )     printf("%d is three digits in length.\n", x ); else     printf("%d", is four digits in length.\n"), x );```
There ya go. You've used if statements.

Quzah.
• 07-22-2002
JohnMayer
awwww, thanx Quzah~! you guys def made my day with helping me find the problem with this program.... btw, what languages do you guys know (and what class(es) for comp sci are you guys enrolled at the undergrad/grad level this fall?
• 07-22-2002
Hammer
>what languages do you guys know
English, a little French, bull.... ;)

>(and what class(es) for comp sci are you guys enrolled at the undergrad/grad level this fall?
Yeah, right, like I go to school :rolleyes: :D ahhh, those were the days......
• 07-22-2002
JohnMayer
Hammer-
so you're done with undergrad and/or grad? (i'm guessing you have a comp sci related job then~!
• 07-22-2002
moonwalker
i was thinking....
how can we make this code work for any number of digits ??

i was thinking... we can consider only integers...
input the whole thing as a char array.. and then
the length is
strlen(thatarray) - 1

and first, we'll check and eliminate all the 0s in the beginning..

for example... if someone enters 007,
it deletes 00
takes strlen(array) which is 2 ('cause there's 7 and '\0') and then -1 of that.. which is 1 .. which is the number of significant digits..

is there another way to do this ?
• 07-22-2002
quzah
There is no need to trim the leading zeros. If you use:

sprintf( buf, "%d", value );

Assuming that 'value' is an integer, it will have no preceeding zeros. Additionally, the length will be 'strlen' length, not 'strlen-1'.

There is also a way to do it with a loop by repeating the division process. Divide by ten until there is no remainder, or until the remainder is less than 10. The number of divisions + 1 is the number of digits.

Quzah.
• 07-22-2002
moonwalker
Quote:

Originally posted by quzah
Divide by ten until there is no remainder, or until the remainder is less than 10. The number of divisions + 1 is the number of digits.

How do we do this ? we can't use number%10 ....
then how do we know the remainder ?
• 07-22-2002
quzah
Quote:

How do we do this ? we can't use number%10 ....
then how do we know the remainder ?
What's wrong with people? Doesn't anyone know how to try something on their own first? **** man, how long does it take to throw some test code together:

Code:

```#include <stdio.h> int main ( void ) {     int x = 1234,y,z;     printf("%d / 10 = %d, %d % 10 = %d", x, y=x/10, x, z=x%10 );     return 0; }```
Stick it in a loop and play around with it. Arrrg. I'm so freeking tired of every one wanting everything handed to them.

The whole point of programming is so you can figure out how to solve a problem. How is me giving you the freeking answer helping you learn problemsolving?

Quzah.
• 07-22-2002
moonwalker
hey..
Quote:

Originally posted by quzah

What's wrong with people? Doesn't anyone know how to try something on their own first? **** man, how long does it take to throw some test code together:

The whole point of programming is so you can figure out how to solve a problem. How is me giving you the freeking answer helping you learn problemsolving?

Quzah.

There's also another reason (besides getting their code done by someone else) why people might ask questions, it is because they can't figure out how.

if you're not willing to help me, then don't!!! but never assume i've never tried and never use bad language.. remember, it's as easy for me to type the four letter words as it is for you.
but we all know it is not a good habit, don't we ?
• 07-22-2002
quzah
Quote:

but never assume i've never tried and never use bad language.. remember, it's as easy for me to type the four letter words as it is for you.
Personally I don't give a **** what you say. Nothing anyone can say or type to me can possibly offend me, because people are irrelevant.

I show up, I answer questions. (Over and over and over, the exact same thing, all day long! Because no one can read a FAQ or use a search engine or do virtually anything on their own.) I help people who help themselves first.

I am not a babysitter. I am not here to lead people by the hand and do everything for them. I present a solution to a problem with enough information that any but the brain dead should be able to figure out the answer.

As for your not knowing how, what exactly is it you don't know?

You don't know how to divide?
You don't know how to use the % operator?
You don't know how to use printf?
You don't know how to create a variable?

I mean give me a break, these have got to be the most basic steps in programming there are. Surely your book has covered them by know?

If not, then what on earth are you trying to use a loop for? I mean, that must just blow your feeble mind.

As for telling me not to use "bad language". Get real. You have no rule over me. I'll do and say whatever I please. After all, I happen to live in a supposedly free country. And that aside, manners are for people who actually care what others think of them.

I don't care what any of you think.

Quzah.
... Cranky bastard extraordinare.
• 07-22-2002
moonwalker
shhh...
Quote:

Originally posted by quzah

Personally I don't give a **** what you say. Nothing anyone can say or type to me can possibly offend me, because people are irrelevant.

I show up, I answer questions. (Over and over and over, the exact same thing, all day long! Because no one can read a FAQ or use a search engine or do virtually anything on their own.) I help people who help themselves first.

I am not a babysitter. I am not here to lead people by the hand and do everything for them. I present a solution to a problem with enough information that any but the brain dead should be able to figure out the answer.

As for your not knowing how, what exactly is it you don't know?

You don't know how to divide?
You don't know how to use the % operator?
You don't know how to use printf?
You don't know how to create a variable?

I mean give me a break, these have got to be the most basic steps in programming there are. Surely your book has covered them by know?

If not, then what on earth are you trying to use a loop for? I mean, that must just blow your feeble mind.

As for telling me not to use "bad language". Get real. You have no rule over me. I'll do and say whatever I please. After all, I happen to live in a supposedly free country. And that aside, manners are for people who actually care what others think of them.

I don't care what any of you think.

Quzah.
... Cranky bastard extraordinare.

Now this is definitely my mistake for letting you so go far..
Pride goes before a fall.
I give up teaching ethics to idiots.
Thanks a ton for the guy who invented the ignore button! :)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last