【发布时间】:2015-04-20 02:28:18
【问题描述】:
我正在研究算法并且很想学习。我的问题是,我无法使用 C 中的整数数据类型输出数字“600851475143”。所以我切换到双精度数。但是输出是“0.0000”。有人可以告诉我我做错了什么吗?我只是想扫描并打印双数据类型中的任何数字,然后我将专注于获得最高的素数:)
#include <stdio.h>
#include <math.h>
int main()
{
double number;
double div = 2;
double highest = 2;
printf("Please input a number: ");
scanf("%lf", &number);
printf("\n You entered: %lf", &number);
while(number!=0){
if(fmod(number,div) != 0){div = div + 1;}
else{
number = number / div;
printf("\n %lf", div);
if(highest < div){highest = div;}
if(number == 1){break;}
}
}
printf("\n The highest number is %lf", highest);
return 0;}
我做了什么:
在 google 中搜索“scan double in c”,得知“%lf”是正确的方法,但程序没有显示任何内容。
我检查了 Stackoverflow 中的各种问题,例如:
Why does scanf need lf for doubles
Reading in double values with scanf in c
Difference between float and double
Read and Write within a file in C (double it)
Reading and writing double precision from/to files
Reading in double values with scanf in c
- 我搜索过的其他网站:
http://www.technoburst.net/2011/07/reading-double-in-c-using-scanf.html
感谢您用您的知识启发我。
【问题讨论】:
-
使用
long数据类型而不是int,而不是double,因为只要您只需要整数运算,它就是浮点数。作为格式说明符,使用%ld。 -
printf("\n You entered: %lf", &number);-->printf("\n You entered: %f", number);删除l和&。 -
先检查scanf的返回值...