# Thread: Integer analysis, question

1. ## Integer analysis, question

Design and code a program that will take in an integer number from the user. This number has to be between 1 and 9999 (inclusive). This number will then be analysed by your code as to how many 1000s, 100s, 10s, and 1s make up this number. For example, an entry of 4208 by the user should make your program output something like: 4208 is made up of 4 lots of 1000, 2 lots of 100, 0 lots of 10s, and 8 lots of 1s.
So 4208 = 4 x 1000 + 2 x 100 + 0 x 10 + 8 x 1

Another example is 233. The output should be something like “0 lots of 1000, 2 lots of 100, 3 lots of 10s, and 3 lots of 1s”.

Can i please have help with writing this code, and any examples please.

This is how we have started:
Code:
```#include <stdio.h>

main()
{
int user_in;

printf("enter an integer from value 1 to 9999:\n\n");
scanf("%d", &user_in);

if (user_in < 1000 > 0);
printf("0 x 1000\n");
if (user_in < 2000 > 999);
printf("1 x 1000\n");
if (user_in < 3000 > 1999);
printf("2 x 1000\n");
if (user_in < 4000 > 2999);
printf("3 x 1000\n");
if (user_in < 5000 > 3999);
printf("4 x 1000\n");
if (user_in < 6000 > 4999);
printf("5 x 1000\n");

system("pause");
}```
thanks

2. You need to look at each digit. There are many ways to do this, one of the easiest is to just keep dividing by 10 to start chopping it up. You also probably want to use the modulus operator % to help out.

Quzah.

3. Thanks buddy,

Can you show me the fastest and simplest way you would do it please.

4. Probably the best way is to sit down with a pencil and paper and work out how you would dissect a number... then look in the C documentation to see if there are math functions that will help you achieve that goal...

Quzah already mentioned modulous math (%) ... what is the result of 103 mod 10 ... 3, right? That should give you a pretty strong hint.

5. Two suggestions for you.

1) If you want to use "stair step" type logic, it's usually far better to start with the greatest value, and step down in size, from there, to the smallest value.

2) If you have a number, say 123, you can easily "peel" off the digits one at a time, by using this:

Code:
```number=123
count = 0
while(number > 0)
count++;
digit = number mod 10
number /= 10;
end of while loop```
When loop is 1, your digit will be 1's digit. When it's 2, your digit is the 10's digit, etc.

Makes things a lot easier, imo.

6. Code:
`      if (user_in < 6000 > 4999);`
This!
a < b > c is not C. you've to use && operator a < b && b > c.
and if( A ) ; is not right.
It's the same as
Code:
`if(A) {  // do nothing  }`

7. I am also attacking this question, I see that the modulus (%) can be used but could there please be a post to start the code of as i have tried for hours and have had no success! thanks

8. Start your own thread. Post your own code. Read the homework policy, and use code tags. Or you could just actually search the forum since this problem comes up every week.

Quzah.

9. Post your best try and we will help with that.

An alternative way to using % would be a brute force try:

subtract 1000 until the number is smaller than 1000. Remember the number of times you subtracted.
subtract 100 until the number is smaller than 100. Remember the number of times you subtracted.
subtract 10 until the number is smaller than 10. Remember the number of times you subtracted.
subtract 1 until the number is smaller than 1. Remember the number of times you subtracted.

Hint: there is a pattern in there

10. We are still having a few issues, do you think this is the best way to do this problem?

Thanks guys

Code:
```#include <stdio.h>

main()
{
int user_in;

printf("************* enter an integer from value 1 to 9999: ******************\n\n");
scanf("%d", &user_in);

if(user_in<1000){
printf("0 x 1000\n");
}
else
if((user_in < 2000)&&(user_in >999)){
printf("1 x 1000\n");
}
else
if((user_in < 3000)&&(user_in >1999)){
printf("2 x 1000\n");
}
else
if((user_in < 4000)&&(user_in >2999)){
printf("3 x 1000\n");
}
else
if((user_in < 5000)&&(user_in >3999)){
printf("4 x 1000\n");
}
else
if((user_in < 6000)&&(user_in >4999)){
printf("5 x 1000\n");
}
else
if((user_in < 7000)&&(user_in >5999)){
printf("6 x 1000\n");
}
else
if((user_in < 8000)&&(user_in >6999)){
printf("7 x 1000\n");
}
else
if((user_in < 9000)&&(user_in >7999)){
printf("8 x 1000\n");
}
else
if((user_in < 10000)&&(user_in >8999)){
printf("9 x 1000\n");
}

else
printf("**************** re-enter number *****************");

system("pause");
}```

11. Try it like this...
Code:
```#include <stdio.h>

int main (void)
{ int digit;
int multiplier = 1;
unsigned int number;     // lets us go up to 9 digits

// get number from the operator

while (number > 0)
{ digit = number % 10
printf("%d x %d\n",digit, multiplier);
number /= 10;
multiplier *= 10; }

return 0; }```

12. hi, thanks for the help this is so far what we have written but there must be a way to short cut this otherwise we would be writting a 1500 line code by the time we do this for the 1s units.
Code:
```#include <stdio.h>

main()
{
int user_in;

printf("*************  enter an integer from value 1 to 9999: ******************\n\n");
scanf("%d", &user_in);

if(user_in<1000){
printf("0 x 1000\n");
}
else
if((user_in < 2000)&&(user_in >999)){
printf("1 x 1000\n");
}
else
if((user_in < 3000)&&(user_in >1999)){
printf("2 x 1000\n");
}
else
if((user_in < 4000)&&(user_in >2999)){
printf("3 x 1000\n");
}
else
if((user_in < 5000)&&(user_in >3999)){
printf("4 x 1000\n");
}
else
if((user_in < 6000)&&(user_in >4999)){
printf("5 x 1000\n");
}
else
if((user_in < 7000)&&(user_in >5999)){
printf("6 x 1000\n");
}
else
if((user_in < 8000)&&(user_in >6999)){
printf("7 x 1000\n");
}
else
if((user_in < 9000)&&(user_in >7999)){
printf("8 x 1000\n");
}
else
if((user_in < 10000)&&(user_in >8999)){
printf("9 x 1000\n");
}

else{
printf("****************  re-enter number  *****************");
}
system("pause");
}```

13. You aren't even reading this thread are you?

Quzah.

14. Originally Posted by larrysmiz
hi, thanks for the help this is so far what we have written but there must be a way to short cut this otherwise we would be writting a 1500 line code by the time we do this for the 1s units.
HELLO...

Go take a look at message #11 ... I showed you how to do up to 9 digits in 5 lines of code...
Moreover; I could change 2 words in that and let you do up to 19 digits.

15. hi,
thanks for the help we have now after some time figured out a suitable way to answer this, only that we now want the program to run continuously until stopped by the user, any suggestions would be aprreciated.
Code:
``` #include <stdio.h>

main()
{
int user_in;

printf("*************  enter an integer from value 1 to 9999: ******************\n\n");
scanf("%d", &user_in);

int a  = user_in/1000;
int b = user_in % 1000 / 100;
int c = user_in % 100 / 10;
int d = user_in % 10 / 1;

printf("%d lots of 1000s\n\n",a);

printf("%d lots of 100s\n\n",b);

printf("%d lots of 10s\n\n",c);

printf("%d lots of 1s\n\n",d);

if (user_in > 9999) {
printf("error");
}

system("pause");
}```

Popular pages Recent additions