# Thread: Help with Number Assignment

1. ## Help with Number Assignment

I need to write a function that will ask for an integer. The function needs to determine the sign (positive or negative) and the number of times a digit appear in the given integer for all possible digits (i.e., 0, 1, 2,…9). I'm stuck because I don't know how to use mod/loops to extract each number from the possible input (which can be anything from a 1 digit number [3] to a 10 digit number [1245029341])

A sample of the output should be :

Enter an integer: 1034029
The sign : +
The counts of digits:
0 2
1 1
2 1
3 1
4 1
5 0
6 0
7 0
8 0
9 1

P.S. Can't use arrays (haven't learned them yet)

So basically, my real problem is how to extract each single number from the input at a time until there is no number to extract.

2. Originally Posted by .C-Man.
So basically, my real problem is how to extract each single number from the input at a time until there is no number to extract.
Use this:
Code:
```digit = number % 10;
number /= 10;```
Untill number == 0 of course.

3. To get the digits, in reverse, just compute mod 10 of your number, which gives you the last digit, and then divide by 10 to remove that digit(truncate the number by the last digit) and move on to the next. If you can't use arrays you will probably need 10 variables to keep the count of every digit. Use a switch statement to increase the correct count depending on what digit you encounter.

4. Originally Posted by Sipher
Use this:
Code:
```digit = number % 10;
number /= 10;```
Untill number == 0 of course.
Thanks, I don't know how I didn't get that. But, if I do that until number == 0 wouldn't it add an extra counter to 0?

5. Just do while(number != 0).... what we said. Then, for every digit you get, check what it is and increment the counter for that digit.

6. Code:
```void displayIntegerDigit( void ){

int Num0;
int Num1;
int Num2;
int Num3;
int Num4;
int Num5;
int Num6;
int Num7;
int Num8;
int Num9;
int NumExtract;
int NumberEntered;
int NxtNum;

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

while (NumberEntered != 0){

NumExtract = NumberEntered % 10;
NxtNum = (NumberEntered /= 10);

if(NumExtract == 0){
Num0++;
}
if(NumExtract == 1){
Num1++;
}
if(NumExtract == 2){
Num2++;
}
if(NumExtract == 3){
Num3++;
}
if(NumExtract == 4){
Num4++;
}
if(NumExtract == 5){
Num5++;
}
if(NumExtract == 6){
Num6++;
}
if(NumExtract == 7){
Num7++;
}
if(NumExtract == 8){
Num8++;
}
if(NumExtract == 9){
Num9++;
}

}
printf("\nThe counts of digits: ");
printf("\n0 %d", Num0);
printf("\n1 %d", Num1);
printf("\n2 %d", Num2);
printf("\n3 %d", Num3);
printf("\n4 %d", Num4);
printf("\n5 %d", Num5);
printf("\n6 %d", Num6);
printf("\n7 %d", Num7);
printf("\n8 %d", Num8);
printf("\n9 %d", Num9);

}```
This is what I have so far... it's printing out weird things. I'm still working on it though.

7. Set num0 thru num9 equal to 0 at start of program.

This is not the correct way.
Code:
```while (NumberEntered != 0){

NumExtract = NumberEntered % 10;
NxtNum = (NumberEntered /= 10);```
This might work; untested; when inside a loop you need to change the value tested by the loop 99 percent of the time.

Code:
```NxtNum = NumberEntered;
while (NxtNum != 0){

NumExtract = NxtNum % 10;
NxtNum = (NxtNum /= 10);```
Note: I think the number of zero might be low if the last or first digit is zero.

Tim S.

8. Code:
```void displayIntegerDigit( void ){

int Num0 = 0;
int Num1 = 0;
int Num2 = 0;
int Num3 = 0;
int Num4 = 0;
int Num5 = 0;
int Num6 = 0;
int Num7 = 0;
int Num8 = 0;
int Num9 = 0;
int NumExtract;
int NumberEntered;
int NxtNum;

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

NxtNum = NumberEntered;
while (NxtNum != 0){

NumExtract = NxtNum % 10;
NxtNum = (NxtNum /= 10);

if(NumExtract == 0){
Num0++;
}
if(NumExtract == 1){
Num1++;
}
if(NumExtract == 2){
Num2++;
}
if(NumExtract == 3){
Num3++;
}
if(NumExtract == 4){
Num4++;
}
if(NumExtract == 5){
Num5++;
}
if(NumExtract == 6){
Num6++;
}
if(NumExtract == 7){
Num7++;
}
if(NumExtract == 8){
Num8++;
}
if(NumExtract == 9){
Num9++;
}
else {
break;
}

}
printf("\nThe counts of digits: ");
printf("\n0 %d", Num0);
printf("\n1 %d", Num1);
printf("\n2 %d", Num2);
printf("\n3 %d", Num3);
printf("\n4 %d", Num4);
printf("\n5 %d", Num5);
printf("\n6 %d", Num6);
printf("\n7 %d", Num7);
printf("\n8 %d", Num8);
printf("\n9 %d", Num9);

}```
Sample Output:

Enter a integer: 8222

The counts of digits:
0 0
1 0
2 1
3 0
4 0
5 0
6 0
7 0
8 0
9 0

It only counts the least significant digit but nothing else.

9. Look at your if statements, and more specifically your else statement. They are disjoint. On the first run through your loop, NumExtract is 2 (as it should be). The if(...1) fails, the if(...2) succeeds, the if(...3) fails, and so on. When the if(...9) fails, you hit your else block, which calls a break.

You should be using:
Code:
```		if(NumExtract == 0){
Num0++;
}
else if(NumExtract == 1){
Num1++;
}
...
else if(NumExtract == 9){
Num9++;
}
else {
break;
}```
so it can only ever enter 1 of those sections. Or better yet, use a switch statement, since it was designed for exactly this sort of situation.

As a side note, after making the changes I suggested, your program still won't work for the input 0 (zero). I'm not sure, if this is for homework, if you need to consider that special case.

10. Yes, I realized that and I changed it. But another problem I'm having is when the number inserted is long that it starts counting to random numbers.
ex.

Enter a integer: 10340291029

The counts of digits:
0 1
1 1
2 0
3 2
4 1
5 2
6 1
7 2
8 0
9 0

or

Enter a integer: 30928490238409238

The counts of digits:
0 0
1 2
2 1
3 3
4 1
5 0
6 1
7 0
8 2
9 0

11. Actually, they're not random. You get the same output in both cases. I changed a small section of your code. Try putting this in your program and running it.

Code:
```    int NumberEntered;
int NxtNum;
int rv;

printf("Enter a integer: ");
rv = scanf( "%d", &NumberEntered );

printf("scanf returned: %d, errno is %d: %s\n", rv, errno, strerror(errno));
printf("The number you entered is %d\n", NumberEntered);

NxtNum = NumberEntered;
while (NxtNum != 0){```

12. Oh, and you will probably need a

Code:
`#include <errno.h>`
at the top of your file.

13. I would use that but unfortunately I can't use anything I haven't learned in class. Also, when the sign is negative it prints out for everything. Do you know why?

Enter a integer: -2389429
The sign: -
The counts of digits:
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0

14. Originally Posted by .C-Man.
I would use that but unfortunately I can't use anything I haven't learned in class.
I didn't intend for you to use that in your final draft that you turn in, it was for your edification. If you make the changes I suggested, you will one, learn about the errno variable and how standard C functions such as scanf describe to you any problems you have, and two, understand why your program doesnt work for those large values.

Originally Posted by .C-Man.
Also, when the sign is negative it prints out for everything. Do you know why?
Yes, I do. I'll tell you once you try out the changes I suggested and tell me what you found.

15. This is what I got: