-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsatest1.c
38 lines (32 loc) · 865 Bytes
/
dsatest1.c
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
#include <stdio.h>
// Function to reverse the number
int reverseNumber(int num) {
// Base case: if the number is 0, return 0
if (num == 0) {
return 0;
}
// Recursive case
// Reverse the rest of the number and append the last digit
int reversed = reverseNumber(num / 10);
int digits = 1;
while (digits <= num / 10) {
digits *= 10;
}
return (num % 10) * digits + reversed;
}
int main() {
int num;
// Input the number
printf("Enter a number: ");
scanf("%d", &num);
// Handle negative numbers
int sign = 1;
if (num < 0) {
sign = -1;
num = -num;
}
// Find and print the reversed number
int reversed = reverseNumber(num) * sign;
printf("The reversed number is: %d\n", reversed);
return 0;
}