current position:Home>Lesson 34 life cycle and prime number "interesting C + + programming for primary school students"

Lesson 34 life cycle and prime number "interesting C + + programming for primary school students"

2022-01-27 02:16:11 dllglvzhenfeng

/*
 Try to make a program , Enter a natural number , Judge whether it is prime or not .
*/ 
#include<iostream>
using namespace std; 
int main()
{
	long long i,n; 
	bool flag;
	
	cout<<"n=";
	cin>>n;
	
	flag=true;
	
	for(i=2;i<n;i++)
	{
		if(n%i==0) 
		{
		  //false  It means that it is not a prime number ( Prime number ) 
		  flag=false;
		  break;// Jump out of for loop  
		} 
	}
			
	if(flag) 
	{
		cout<<" Prime number "; // Prime number  
	}
	else 
	{
		cout<<" Not primes "; // Sum  
	}
	
	return 0;
}
/*
 This code can also be further optimized 
 
 prime number , Prime number   except 1 And beyond itself , Can't be divided 

2 3  5 7  11 13 17 19 23...... 

6
1 2 3 6

8 

1 2 4 8

9 
1 3 9
  
*/

 



Informatics Olympiad Provincial election and NOI_PDF.rar

Informatics osay province selection and NOI_PDF.rar- Other document resources -CSDN download

copyright notice
author[dllglvzhenfeng],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270216090823.html

Random recommended