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;
}
4
  • Oops.. that was the problem, thank you! Commented Jan 31, 2016 at 12:18
  • Yeah I cannot see what this has to do with codeblocks at all. Commented Jan 31, 2016 at 12:36
  • I didn't understand the error, and assumed it has to do with codeblocks, because I didn't notice the mistake in the code. Commented Jan 31, 2016 at 14:01
  • So I assume the fix is to use int i=2? Commented Jan 31, 2016 at 19:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.