对质数进行问题测试

时间:2016-03-17 23:18:39

标签: c++ primes

您好,我正在为课堂做素数工作,而且我遇到了问题。有人可以帮我一点吗?

有些数字可以使用,但是其他数字却赢了

#include<iostream>
#include "cmath"
#include "ctime"
using namespace std;

int main(){
    int num;
    int i = 2;
    int result;
    cout << "What is the prime you want to enter";
    cin >> num;

    for(; i < num; i++){
        result = num / i;
    }

    if (num % i == 0) {
        cout << "Your number is not prime\nIt is divisble by: " << i << endl;
    }

    if(num % i != 0){
        cout << "your number is prime\n";
    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

这个if (num % i == 0) { ....必须在循环内完成,否则你只会检查i的最后一个值