Displaying digits in forward direction in C? -


i'm trying make program takes inputted integer , reads digits forwards. 12345 be...

digit: 1 digit: 2 digit: 3 digit: 4 last digit: 5 

inputs trailing zeros (like 100000) run problems though. in forward direction, of these zeros show 9's , last integer not show 'last digit:'. ideas?

#include <stdio.h> #include <math.h> int main(){     int n = 0;     int = 0;     int j = 0;     int k = 0;     int output2 = 0;     int count = 0;      printf("number? > ");     scanf("%d", &n);      = n;     j = n;       while (i){         = i/10;         count++;     }       printf("foward direction \n");     while (count > 0){         k = j;         k /= pow(10, count - 1);         output2 = k % 10;         if (output2 >= pow(10, count - 1)){             printf("last digit: %d \n", output2);         }         else {             printf("digit: %d \n", output2);         }         count -= 1;     }      return 0; } 

if want clever, simple , elegant, can use recursion.

void print_digits(int n) {     if (n >= 10) {         print_digits(n / 10);     }     putc('0' + n % 10, stdout); } 

in code, pow() erroneous - don't try use floating-point numbers solve problems integer numbers.

edit: here's full homework done op, @darron happy well:

void print_digits(int n, int islast) {     if (n >= 10) {         print_digits(n / 10, 0);     }      if (islast) {         printf("last ");     }      printf("digit: %d\n", n % 10); } 

call islast argument being true (nonzero):

print_digits(12345, 1); 

this produces

digit: 1 digit: 2 digit: 3 digit: 4 last digit: 5 

as output.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -