我試圖做到這一點:C++循環各非黃金
- 用戶選擇一個數
- 程序調用的函數
isaPrime()
所以我們看到,如果數是素數或不。
我想做一個循環,所以每次數字不是素數時,用戶必須選擇一個新值。
下面的代碼:
#include <iostream>
#include <math.h>
using namespace std;
bool isPrime (int num)
{
if (num <=1)
return false;
else if (num == 2)
return true;
else if (num % 2 == 0)
return false;
else
{
bool prime = true;
int divisor = 3;
double num_d = static_cast<double>(num);
int upperLimit = static_cast<int>(sqrt(num_d) +1);
while (divisor <= upperLimit)
{
if (num % divisor == 0)
prime = false;
divisor +=2;
}
return prime;
}
}
int main()
{
int p;
do {
cout << "p : ";
cin >> p;
isPrime(p);
} while (isPrime(p));
}
這是不是真的很有趣...... 如果我來到這裏就意味着我無法自己做。 如果你只是在這裏找到無用的帖子,那就離開那裏吧。 –
http://stackoverflow.com/help/mcve問題是題外話題!你現在要做什麼? –
我做了所有這些事情...... –