用C++编写函数判别一个数是否是质数,在主程序中实现输入输出

[复制链接]
查看11 | 回复2 | 2015-11-18 15:10:38 | 显示全部楼层 |阅读模式
先定义整型变量n,输入n,通过判断函数判断,最后输出。例如://参考代码如下:#include "iostream"#include "stdio.h" using namespace std; int fun(int n){//判断函数,是质数返回1,否则返回0 int ans =1;for(int i=2;i>n;//输入n if(fun(n))//判断输出 cout>n;//输入n if(fun(n))//判断输出 cout//此为数学函数库。程序中要用到数学函数时必须包含此语句。voidf(intn)//此函数只判断是否为素数。{inti,j=0;for(i=2;i>n;f(n);return0;}
回复

使用道具 举报

千问 | 2015-11-18 15:10:38 | 显示全部楼层
#include void main(){
int n;
scanf("Input a number: %d", &n);
int m = sqrt(n)+1;
if(m == 2)
{
printf("%d为质数", n);
return;
}
for(int i = 2; i
//此为数学函数库。程序中要用到数学函数时必须包含此语句。void f(int n)
//此函数只判断是否为素数。{ int i,j=0; for(i=2;i>n; f(n); return 0;}
回复

使用道具 举报

千问 | 2015-11-18 15:10:38 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行