c语言新手程序问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 14:33:56
最近要编写一个输出从a到b之间的素数的问题,a和b键盘输入.
下面是我写的
#include<stdio.h>
#include<math.h>

main()
{
int a,b,i,j,flag;
int x;
printf("input a and b(a<b)");
scanf("%d,%d",&a,&b);
for (i=a;i<=b;i=i+2)
{
flag=0;
x=sqrt(i);
for (j=1;j<x;j++)
{
if (i%j==0)
{flag=1;
break;
}
}
if (flag==0)
{
printf("%5d",i);
getch();
}
}
}
程序能运行?比如a和b分别为3,9,输出就是3.问题出在哪?

#include<iostream>
#include<cmath>
using namespace std;
main()
{
int a,b;
int i,j;
cout<<"enter a and b"<<endl;
cin>>a>>b;
for (i=a;i<=b;i++)
{
for (j=a;j<i;j++)
if (i%j==0)
{
break;
}
if (i==j)
cout<<i<<"is sushu"<<endl;

}
system("pause");
return 0;
}

for (j=2;j<=x;j++) 起点应该是2开始;终点为等于J<=X