English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

如提 我要怎麼去寫出這個程式ㄚ 請會寫的大大幫我想依下 謝 謝了

2006-07-27 05:45:26 · 2 個解答 · 發問者 Louis Wu 1 in 電腦與網際網路 程式設計

2 個解答

先前有人問過相同的 C 程式碼,我只是把它改寫成 C++ 而已。

//Power by Microsoft Visual Studio 2005
//可以使用 Dev-C++ 編譯此程式
#include
#include
#include
using namespace std;
int main(int argc,char *argv[]){
//=====START=====//
bool check_prime(int number),retHigh;
int a,nhighPrime;
cout<<"Input a integer: ";
cin>>a;
nhighPrime=a;
do{
retHigh=check_prime(nhighPrime);
nhighPrime++;
}while(!retHigh);
cout<<"Bigger prime: "<<--nhighPrime< //=====END=====//
system("PAUSE");
return EXIT_SUCCESS;
}
bool check_prime(int number){//Check number
int i;
bool retBoolean=true;
//1 is True, 0 is False
for(i=2;i<=static_cast(sqrt(static_cast(number)));i++){
if((number%i)==0){
retBoolean=false;//false
break;
}
}
return retBoolean;//return boolean value
}

2006-07-27 10:53:22 補充:
抱歉!有一行要修改:錯誤=>nhighPrime=a;正確=>nhighPrime=a+1;

2006-07-27 06:39:51 · answer #1 · answered by Big_John-tw 7 · 0 0

......不好意思,我只會比較簡單的 = =;

==========================================

#include "stdafx.h"
#include "iostream"

using namespace std;

bool prime(int n);

int _tmain(int argc, _TCHAR* argv[])
{
int n;

cout<< "enter a number : ";
cin >> n;

for(int m = n + 1; (prime(m) == false); m++)
{
prime(m);
}

cout<< m << endl;

return 0;
}


bool prime(int n)
{
bool result = true;

for(int i = 2; i < n; i++)
{
if(n % i == 0)
result = false;
}

return result;
}

2006-07-27 07:16:36 · answer #2 · answered by gene 2 · 0 0

fedest.com, questions and answers