Added exercise 3.44
[C-Deitel-Exercises.git] / Chapter-2 / 2-30.c
blob90e831d4f6fb3131004d541c702007c68b587a9d
1 /*C How to Program, 6/E, Deitel & Deitel.
3 Solution of exercise 2.30:
4 (Separating Digits in an Integer) Write a program that inputs one five-digit
5 number, separates the number into its individual digits and prints the digits
6 separated from one another by three spaces each. [Hint: Use combinations of
7 integer division and the remainder operation.] For example, if the user types
8 in 42139, the program should print
10 4 2 1 3 9
12 Written by Juan Carlos Moreno (jcmhsoftware@gmail.com), year 2018.*/
14 #include <stdio.h>
16 int main()
18 int number;
20 /*The five digits of the number*/
21 int first_digit;
22 int second_digit;
23 int third_digit;
24 int fourth_digit;
25 int fifth_digit;
27 /*The remainders*/
28 int first_remainder;
29 int second_remainder;
30 int third_remainder;
31 int fourth_remainder;
33 printf("Enter the number of five digits: ");
34 scanf("%d", &number);
36 first_digit = number / 10000;
37 first_remainder = number % 10000;
39 second_digit = first_remainder / 1000;
40 second_remainder = first_remainder % 1000;
42 third_digit = second_remainder / 100;
43 third_remainder = second_remainder % 100;
45 fourth_digit = third_remainder / 10;
46 fourth_remainder = third_remainder % 10;
48 /*The fourth remainder is the fifth digit*/
49 fifth_digit = fourth_remainder;
51 /*Print the digits separated by three spaces*/
52 printf("%d ", first_digit);
53 printf("%d ", second_digit);
54 printf("%d ", third_digit);
55 printf("%d ", fourth_digit);
56 printf("%d\n", fifth_digit);
58 return 0;