C language: three methods are used to output each digit of an integer.
Output each digit of an integer. Solution: Method 1:
# Include <stdio. h> # include <string. h> int main () {int I, len; char num [100]; printf ("enter an integer:"); scanf ("% s", num ); len = strlen (num); for (I = 0; I <= len-1; I ++) {printf ("% c \ n ", num [I]);} if (num [0]! = 45) // 45 indicates the ASCII Value of "-" {printf ("bits: % d \ n", len);} else {len = len-1; printf ("digits: % d \ n", len);} return 0 ;}
Result 1: Enter an integer: 7235672356 digits: 5. Press any key to continue... result 2: enter an integer-2378-2378 bits: 4. Press any key to continue... method 2: Program:
# Include <stdio. h> int main () {int num = 0; printf ("enter a non-negative integer:"); scanf ("% d", & num); while (num) {printf ("% d \ t", num % 10); num/= 10;} return 0 ;}
Result: enter a non-negative integer: 9877 8 9. Press any key to continue... method 3: Program:
# Include <stdio. h> int print (int num) // Recursive Implementation {if (num> 9) {print (num/10);} printf ("% d \ t ", num % 10) ;}int main () {int num = 0; printf ("enter an integer:"); scanf ("% d", & num ); print (num); return 0 ;}
Result: enter an integer: 876548 7 6 5 4. Press any key to continue...