要求用户输入两个数字X,Y,比如输入3,30,算一下一共有几个质数,和所有完全平方的数,比如3-30之间有4,9,16,25,这4个数是完全平方数。最后再把所有质数和可以平方的数都加起来,并且打印出来,
显示结果如下
Prime:3 5 7 11 13 17 19 23 29
Prime count = 9
Square: 4 9 16 25
Square count = 4
Sum of all primes and squares:181
下面是我写的,不过完全平方那部分好像不行,大侠们帮忙看看啊。
#include<stdio.h>
#include <conio.h>
#include <math.h>
int isPrime(int);
int findPrime (int, int ) ;
int findSquare(int,int);
int square (int);
int sum;
int main (void)
{
int x,y,sumPrime;
printf("Enter two numbers\n");
scanf("%d %d",&x,&y);
while(x <= y)
{
printf("Prime: ");
sumPrime=findPrime( x, y);
printf("\nSquare: ");
findSquare(x,y);
printf("\nsum of all primes and squares:%d",sum);
printf("\nEnter two numbers");
scanf("%d %d",&x,&y);
}
return 0;
}
int findPrime (int a, int b )
{
int t=0,c=0;
while(a<=b)
{
if(isPrime(a))
printf("%d ",a);
sum+=a;
c++;
a++;
}
printf("\nPrime count = %d\n",c);
return t ;
}
int isPrime(int n)
{
int i;
for(i=2;i<n;i++)
if(n%i==0)
return 0;
return 1;
}
int findSquare ( int a,int b)
{
int t;
if(square(t))
printf("%d", a);
c++;
sum+=a;
printf("\nSquare count = %d\n",c);
return 1;
}
int square (int x)
{
int i,k;
k = sqrt(i);
for(i=2;i<=x;i++)
{
if(k*k==1)
return 0;
return 1;
}
}