lundi 25 avril 2016

Wrong output when searching for prime numbers in given interval

I am trying to find to find prime numbers between num1 and num2, but this code is yields only 1,2,3 as prime numbers if my input is 1,10

#include <iostream>
using namespace std;
void prime(int num1, int num2)
{
    bool prime = 1; // prime=1 means the number is prime
    for (num1; num1 <= num2; num1++)
    {
        for (int i = 2; i < num1; i++)
        {
            if (num1 % i == 0)
            {
                prime = 0;
                break;
            }
        }

        if (prime == 0)
        {
            cout << num1 << " Is not Prime" << endl;
        }
        else
        {
            cout << num1 << " Is prime" << endl;
        }
    }
}

int main()
{
    int num1, num2;
    cout << "Enter 2 numbers to check the prime numbers between them:";
    cin >> num1 >> num2;
    prime(num1, num2);
}

Aucun commentaire:

Enregistrer un commentaire