C/C++ Programming Learning-Week 22 ④ Judgment of Prime Numbers

Topic link

Title description

Read in a number N to determine if the number is a prime number.
Prime number: A number N has no other divisors except 1 and itself. Such a number is called a prime number.
Divisor: The quotient obtained by dividing the integer a by the integer b (b≠0) is exactly an integer without a remainder. Then a is called a multiple of b, and b is called a divisor.

Input
input a number N

Output
If N is a prime number, output "yes" If N is not a prime number, output "no"

Sample Input

111

Sample Output

no

Ideas

Determine whether the entered number is a prime number.

C++ code:

#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;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113572590