C/C++编程学习 - 第22周 ④ 质数判断

题目链接

题目描述

读入一个数N,判断这个数是不是质数(prime number)。
质数:一个数 N 除了1和它本身不存在其他约数,这样的数叫做质数。
约数:整数a除以整数b(b≠0) 除得的商正好是整数而没有余数。则a称为b的倍数,b称为a的约数。

Input
输入一个数N

Output
如果N是质数,输出"yes" 如果N不是质数,输出"no"

Sample Input

111

Sample Output

no

思路

判断输入的数是否为质数。

C++代码:

#include<bits/stdc++.h>
using namespace std;
bool Is_Prime(int n)
{
    
      
	for(int i = 2; i * i <= n; i++)
		if(n % i == 0) return false;
	return n != 1;
}

int main()
{
    
    
	int n;
	while(cin >> n)
		if(Is_Prime(n)) cout << "yes" << endl;
		else cout << "no" << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44826711/article/details/113572590