通过键盘输入华氏度的城市温度。现在我需要编写一个程序将此温度转换为摄氏度。
所以这是公式:
°C = (°F - 32) x 5/9
示例输入/输出:
Enter Temperature of Dhaka in Fahreinheit: 98.6
Temperature of Dhaka in Centigrade 37.0 C
Now, i have tried with this, but not works.
代码:
# include <stdio.h>
void main()
{
float C;
printf("Pleas Enter Your Fahreinheit Value to see in centrigate=");
scanf("%d",&C);
printf(C);
float output;
output=(C-32)*(5/9);
printf("The centrigate Value is = %.2lf\n\n" ,output);
}
谁能告诉我出了什么问题?
答案 0 :(得分:8)
void main()
{
float far;
printf("Pleas Enter Your Fahreinheit Value to see in centrigate=");
scanf("%f",&far);
// printf(C);
float cel;
cel =(far-32)*(5.0/9.0);
printf("The centrigate Value is = %.2lf\n\n" ,cel);
}
5/9
是整数除法,它为您提供0
。你需要漂浮。所以5.0/9.0
得到小数部分。 我不知道你为什么这么做printf(C);
。那简直不行。使用
printf("c = %f",c);
float的格式说明符是%f
。 %d
用于整数。
C
来存储farenheit
。现在,这没有错。但可能后来引起混乱。尝试在代码中使用有意义的名称,以使其可读。名字越久越好。答案 1 :(得分:3)
问题:
答案 2 :(得分:1)
您应该进行一些更改,请参阅代码注释:
# include <stdio.h>
void main()
{
float C;
float output; //Better to declare at the beginning of the block
printf("Pleas Enter Your Fahreinheit Value to see in centrigate=\n");
scanf("%f",&C); //Scanf need %f to read float
printf("%f\n", C); //becareful with the printf, they need format too.
output=(C-32)*(5.0/9); //if you put 5/9 is not a float division, and returns int.
//you should add 5.0/9.
printf("The centrigate Value is = %.2lf\n\n" ,output);
}
我认为这就是全部。
答案 3 :(得分:0)
第printf(C);
行应为printf("%f\n", C);
答案 4 :(得分:0)
printf("%f\n",C)
代替printf(C)
和
float output
应位于代码的开头,例如float C