[Loops]D. Liang 4.20 Printing Prime numbers between 2 and n
Description
Write a program that reads an interger n, then print all the prime numbers between 2 and n, inclusively.
Input
An integer n (2<=n<=2000).
Output
All the prime number between 2 and n, inclusively.
You should display eight prime numbers per line, specify the width of each number’s print field to 5, justify the output to right.
NOTE:
- There is NO SPACE at the end of each line.
- There is A NEWLINE (\n) at the end of last line iff it has eight number.
That is (.
indicates <space> and ¬
indicates <newline>)
....2
or
....2....3....5....7...11...13...17...19¬
Sample Input
59
Sample Output
2 3 5 7 11 13 17 19
23 29 31 37 41 43 47 53
59
// Date:2020/3/19
// Author:xiezhg5
#include <stdio.h>
#include <math.h>
int main(void)
{
int count=0;
int i,j,k;
int m,n;
m=2;
scanf("%d",&n);
for(i=m;i<=n;i++)
{
k=sqrt(i); //遍历2到√i的数即可
for(j=2;j<=k;j++)
if(i%j==0)
{
break; //不是素数立即退出循环
}
if(j>=k+1)
{
if(i==1)
count=count;
else
{
printf("%5d",i);
count++;
if(count%8==0) //注意输出要求
printf("\n");
}
}
}
printf("\n");
return 0;
}