2

I'm trying to create a program whereby it will display a (5 digit integer).
An example would be if a user entered "23456", it will output "2 3 4 5 6".

I'm only allowed to capture the input(scanf) with integer(%d). I managed to do it without a for/while loop.

fivedigit = num / 10000; 
fivedigitr = num % 10000; 
fourdigit = fivedigitr / 1000; 
fourdigitr = fivedigitr % 1000;
threedigit = fourdigitr / 100;
threedigitr = fourdigitr % 100;
twodigit = threedigitr / 10;
twodigitr = threedigitr % 10;
onedigit = twodigitr / 1;
onedigitr = twodigitr % 1 
printf("%d %d %d %d %d\n", fivedigit,fourdigit,threedigit, twodigit, onedigit);

However, right now I'm needed to create a Version 2 using a loop. I'm unsure of how to cut the calculations short as to reduce the redundancy. The only thing that I managed to figure out is to reduce the "10000" by dividing by 10.

for(i=0; i<5; i++){
fivedigit = num / division2;
fivedigitr = num % division2;
division2 = division2/10; 
}

Any help would be appreciated. I just need to know the basic ways on how to reduce the redundancy and I can move on from there and figure out.

Thanks.

D3FTY
  • 115
  • 1
  • 1
  • 7
  • @SpencerWieczorek I'm not allowed to capture input as a string/char. The requirements is to capture the input as an integer. – D3FTY Feb 22 '18 at 04:40
  • @MFisherKDX I understand its easier with a string/char. Its an assignment that was assigned to me and those are the requirements. – D3FTY Feb 22 '18 at 04:42
  • 1
    The loop is fine, but before the loop you need to set `fivedigitr = num` and inside the loop you need to change `num` to `fivedigitr`. OTOH you could just change `fivedigitr` to `num` in the loop. – user3386109 Feb 22 '18 at 04:43
  • @user3386109 I'll need 2 variables I assume? As I'm calculating the quotient and remainder of it. And I'll need the remainder(%) in order to calculate for the following number etc. – D3FTY Feb 22 '18 at 04:46
  • Right now the body of the loop has four variables: `fivedigit`, `fivedigitr`, `num`, and `division2`. You only need three: `digit`, `divider`, and either `remainder` or `num`. – user3386109 Feb 22 '18 at 04:49
  • Possible duplicate of [C: how to break apart a multi digit number into separate variables?](https://stackoverflow.com/questions/9302681/c-how-to-break-apart-a-multi-digit-number-into-separate-variables) – MFisherKDX Feb 22 '18 at 04:49
  • @user3386109 I think I got what you mean. I managed to get it. So the only thing I was missing was just assigning it before the loop? I'm trying to understand the process of it. – D3FTY Feb 22 '18 at 04:49
  • 1
    Notice in your first solution that you only use `num` in the first two lines. Then the remainder of one calculation becomes the input to the next calculation. That's what you're trying to duplicate with the loop. – user3386109 Feb 22 '18 at 04:53
  • @user3386109 oh my I totally missed that. Thanks a lot for helping out. I'm still a beginner in this and I may not have see it as you have seen. Once again thanks. – D3FTY Feb 22 '18 at 04:55
  • Glad to help, good luck. – user3386109 Feb 22 '18 at 04:56
  • what about this:`divisor = 10000; num = 23456; for(i=0; i<5; i++){ printf("%d ", num/divisor); num %= divisor; divisor /= 10; }` – Eziz Durdyyev Feb 22 '18 at 05:56

3 Answers3

0

I just need to know the basic ways on how to reduce the redundancy

Extract the least significant digit and first print the others recursively.

// Pseudo code
printnum(n)
  // think about % /
  last_digit = foo(n)   // find least digit of n
  other_digits = bar(n) // find all the digits except the least
  Are there any `other_digits` to print first? {
    printnum(other_digits)  
  }
  print last_digit

If the goal is 5 digits, even for values less than 10,000 and the value will not exceed 99,999, then determine how many myriads, thousands, hundreds, tens, ones.

int divisor = 10000;
while (divisor) {
  int digit = num / divisor;
  printf(" %d", digit);
  num -= digit*divisor;
  divisor /= 10;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

You can get each element of 5 digit number by changing your query to this

 fivedigit = (num / 10000) % 10; 
 fourdigit = (num / 1000) % 10; 
 threedigit = (num / 100) % 10;
 twodigit = (num / 10) % 10;
 onedigitr = twodigitr % 10; 
 printf("%d %d %d %d %d\n", fivedigit, fourdigit, threedigit, twodigit, onedigit);

You can also get it by using loop like this

 for(int i=4; i>=0; i--)
    printf( "%d ", (num / (pow(10,i) ) % 10));

For num=23456 it will print 2 3 4 5 6

0

x is the user input value.digit will contain each digit of that value. the for loop will be updated by x/=10. The condition 0 < x will help the digit to print in correct order.

 for(i=x;0<x;){

 digit=x%10;
 printf("%d,", digit);
 x/=10;
   }
  • 1
    From Review:  Hi, please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Nov 14 '19 at 15:50