Write a C program to reverse and print a given integer number whose absolute value greater than 99 and less than 1000.
时间: 2024-02-29 18:51:14 浏览: 100
Sure, here's the program:
```c
#include <stdio.h>
int main() {
int num, reversed_num = 0;
printf("Enter a number between 100 and 999: ");
scanf("%d", &num);
while (num != 0) {
int remainder = num % 10;
reversed_num = reversed_num * 10 + remainder;
num /= 10;
}
printf("Reversed number: %d", reversed_num);
return 0;
}
```
This program prompts the user to enter a number between 100 and 999, then uses a while loop to reverse the digits of the number. It does this by repeatedly taking the remainder of the number when divided by 10 (which gives the rightmost digit), adding that digit to the reversed number (which has been multiplied by 10 to shift its digits left), and dividing the original number by 10 to discard the rightmost digit. Finally, the program prints the reversed number.
阅读全文