求一个数是质还是合

#include<bits/stdc++.h>
using namespace std;
int main() {
    
    
	int n,i=2;
	scanf("%d",&n);
	while((i<=sqrt(n))&&(n%i!=0))
		i++;
	if(n%i==0)cout<<"合数"<<endl;
	else cout<<"质数"<<endl;
	return 0;
}

这个程序可以你输入一个数,程序可以判断这个数 是质数,还是合数,这个程序用了scanf,while(判断),elseif(如果否则)。用2来除,如果有余是质数。如果没有是合数。

猜你喜欢

转载自blog.csdn.net/include007/article/details/112408449
今日推荐