[Loops]D. Liang 4.20 Printing Prime numbers between 2 and n.c

[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:

  1. There is NO SPACE at the end of each line.
  2. There is A NEWLINE (\n) at the end of last line iff it has eight number.

That is (. indicates <space> and ¬ indicates &lt;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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值