用c语言输入一个浮点数,将整数部分和小数部分分离开,分别输出其整数部分和小数部分(小数部分保留两位有效数字)
时间: 2023-03-26 07:01:03 浏览: 98
您好,可以使用以下代码实现:
```c
#include <stdio.h>
int main() {
float num;
int integer_part;
float decimal_part;
printf("请输入一个浮点数:");
scanf("%f", &num);
integer_part = (int)num;
decimal_part = num - integer_part;
printf("整数部分:%d\n", integer_part);
printf("小数部分:%.2f\n", decimal_part);
return ;
}
```
希望能对您有所帮助。
相关问题
请帮我写一个用c语言把浮点数取整数部分和小数部分的算法
Sure, here is a sample algorithm in C language to separate integer and fractional parts of a float number:
```
#include <stdio.h>
int main() {
float num = 3.14;
int integerPart = (int)num; // cast float to int to get integer part
float fractionalPart = num - integerPart; // subtract integer part to get fractional part
printf("Integer Part: %d\n", integerPart);
printf("Fractional Part: %f", fractionalPart);
}
```
When executed, the output would be:
```
Integer Part: 3
Fractional Part: 0.140000
```
Hope this helps!
用C语言编写一个程序,其功能为:从键盘上输入一个浮点数,然后分别输出该数的整数部分和小数部分
```
#include <stdio.h>
int main()
{
float num;
int integerPart;
float decimalPart;
printf("请输入一个浮点数:");
scanf("%f", &num);
integerPart = (int)num;
decimalPart = num - integerPart;
printf("整数部分为:%d\n", integerPart);
printf("小数部分为:%f\n", decimalPart);
return 0;
}
```
运行结果:
```
请输入一个浮点数:3.1415926
整数部分为:3
小数部分为:0.141593
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)