Welcome Guest.

# Enter n, output all the prime numbers between 2-n, and count the number of prime numbers

Asked by: Sandra Wilson 183 views IT November 8, 2018

Enter n, output all the prime numbers between 2-n, and count the number of prime numbers

1. +5Votes  You have to explain the language you want to use. The following is the reference for C (input/out format is changed according to your requirements)

#include <stdio.h>
#include < Math.h>
int isPrime(int n)
{
int i;
int k=(int)sqrt((double)n); // Ч
for( i=2; i<=k; i++)
{
if( N%i==0)
return 0;
}
return 1;
}
int main()
{
int i,ct,n;
ct=0;
scanf( "%d",&n);
for(i=2; i<=n; i++)
{
if(isPrime(i))
{
printf("%d\n",i);
ct++;
}
}
printf("total=%d\n" , ct);
return 0;
}

2. +8Votes  1, int prime(int a) This function is wrong (it can be said that the problem is very large), for The following code is not placed in {} 2, int prime (int a) In this function you write yourself, when i = 2 will jump out, will not follow your expectations to i <=sqrt (a 3, according to the writing of your program, did not write out the specific prime number, but how many are output 4, there is a problem I do not know if you think about it, if m> what to do, then for (i =m;i<=n;i++) What happens to this code? ?