-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabwork1.c
More file actions
50 lines (39 loc) · 1.11 KB
/
Copy pathlabwork1.c
File metadata and controls
50 lines (39 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h> // For abs() function
int main() {
int number;
int divisor = 1;
int temp_num;
int digit;
// 1. Get user input
printf("Enter an integer: ");
scanf("%d", &number);
// Handle the edge case of 0
if (number == 0) {
printf("0\n");
return 0;
}
// Use a temporary variable to find the divisor
// abs() handles negative numbers correctly
temp_num = abs(number);
// 2. Find the correct starting divisor
while (temp_num / divisor >= 10) {
divisor = divisor * 10;
}
// If the number is negative, print the minus sign first
if (number < 0) {
printf("- ");
}
// 3. Loop through, print digits, and update the number
while (divisor > 0) {
// Get the leftmost digit
digit = temp_num / divisor;
printf("%d ", digit);
// Remove the leftmost digit from the number
temp_num = temp_num % divisor;
// Decrease the divisor by a factor of 10
divisor = divisor / 10;
}
printf("\n"); // Print a newline at the end
return 0;
}