//分离百位、十位、个位 C语言代码
时间: 2024-01-11 16:02:08 浏览: 43
#include <stdio.h>
int main() {
int num, hundreds, tens, ones;
printf("请输入一个三位数:");
scanf("%d", &num);
hundreds = num / 100; // 获取百位数
tens = (num % 100) / 10; // 获取十位数
ones = num % 10; // 获取个位数
printf("百位数:%d,十位数:%d,个位数:%d\n", hundreds, tens, ones);
return 0;
}
相关问题
c语言的代码//分离万位、千位、百位、十位、个位
#include <stdio.h>
int main() {
int num, thousand, hundred, ten, one;
printf("请输入一个五位数:");
scanf("%d", &num);
thousand = num / 1000;
hundred = (num % 1000) / 100;
ten = (num % 100) / 10;
one = num % 10;
printf("万位:%d\n", thousand);
printf("千位:%d\n", hundred);
printf("百位:%d\n", ten);
printf("十位:%d\n", one);
return 0;
}
如何用C语言编写程序转换127分离出它的百位十位个位数反转后输出该三位数
以下是一个可以实现该功能的C语言程序:
```c
#include <stdio.h>
int main()
{
int num = 127; // 定义待转换的数
int hundreds, tens, ones; // 分别表示百位、十位、个位
// 分离百位、十位、个位
ones = num % 10;
num /= 10;
tens = num % 10;
num /= 10;
hundreds = num;
// 反转百位、十位、个位
int temp = ones;
ones = hundreds;
hundreds = temp;
// 输出结果
printf("%d%d%d\n", hundreds, tens, ones);
return 0;
}
```
程序运行输出结果为:
```
721
```
其中,7表示百位,2表示十位,1表示个位。