Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我需要做什么来开发一个程序,在一个数字中找到主要因素?_C++ - Fatal编程技术网

C++ 我需要做什么来开发一个程序,在一个数字中找到主要因素?

C++ 我需要做什么来开发一个程序,在一个数字中找到主要因素?,c++,C++,在这个程序中,我需要提示用户输入一个数字,程序将在其中找到该数字的主要因子。以下是我目前掌握的情况: int n; cin >> n; for (int i = 2; i <= n; ++i) { bool iprime = true; if (n % i == 0) iprime = false; { } } intn; cin>>n; 对于(int i=2;i以下是您将如何使用布尔: int p; cout << "Enter a

在这个程序中,我需要提示用户输入一个数字,程序将在其中找到该数字的主要因子。以下是我目前掌握的情况:

int n;
cin >> n;

for (int i = 2; i <= n; ++i) {
    bool iprime = true;
    if (n % i == 0) iprime = false; {

    }
}
intn;
cin>>n;

对于(int i=2;i以下是您将如何使用布尔:

int p;
cout << "Enter a number: ";
cin >> p;

bool prime = true;
for(int i = 2; i < p; ++i)
{
    if(p % i == 0){
        prime = false;
    }
}
if(!prime) cout << "not"<< endl;
else cout << "prime" << endl;
intp;
cout>p;
布尔素数=真;
对于(int i=2;i如果(!prime)cout你可以试试这个,你可以用long int来表示大的数字:)

int n=0;
cout>n;
//如果它可以整除为2,那么2是n的素因子,依此类推。。。。
而(n%2==0)
{
库特
int n=0;

cout << "Pick a Number: ";
cin >> n;

// If it's divisible to 2 then 2 is a prime factor of n and so on....
while(n%2==0)
{
    cout << "2 ";
    n=n/2;
}

    while(n%3==0)
{
    cout << "3 ";
    n=n/3;
}

    while(n%5==0)
{
    cout << "5 ";
    n=n/5;
}

    while(n%7==0)
{
    cout << "7 ";
    n=n/7;
}

// If n is not divisible to 2-7...
if(n>7)
{
    cout << n;
}

return 0;