0

I wrote two programs and they worked fine, then i started the third one where I have to check if two numbers are prime and if A+2=B.I have to do it with two functions and return true or false in the end.So I wrote something and ran the program with the numbers a=3, b=7 which returns false, that is fine, but when I do it with a=5 and b=7, Codeblocks crashes and says: "bool.exe has stopped working".I don't know if my code is correct, and can't understand why it works one time and the other it doesn't.Here it is:

#include <iostream>

using namespace std;

bool prime(int n)
{
    for(int i=0;i<n;i++)
    {
        if(n%i==0) return false;
    }
    return true;
}
bool sdvoeniprosti(int a, int b);
int main()
{
    int a, b, result;
    cin >> a >> b;
    result=sdvoeniprosti(a, b);
    if(result==1) cout << "true";
    else cout << "false";
}
bool sdvoeniprosti(int a, int b)
{
    if(a+2==b && prime(a)==true && prime(b)==true) return true;
    return false;
}

0 Answers0