Thread: friends numbers program

1. friends numbers program

Hello guys!

I have to make a program in c++ that displays on the screen all the friends numbers <= n . For example , if n is 11 then it should display the pairs :
(3,5);(5,7)
Friends numbers are those numbers whose difference is 2 and both numbers are prime. The difference between 5 and 7 is 2 and they are prime.The same is for 5 and 7,11 and 13 and so on.
Here is my piece of code and it doesn't work....
Code:
```#include <iostream>
using namespace std;

int main(){
int n,d1,a,b,ok1,ok2,d2,i;

cin >> n;
for (a = 3;a < n;a++) {ok1 = 0;
for  (d1 = 2;d1 <= a/2;d1++)
if (a % d1 == 0) ok1 = 1;}
if (ok1 == 0) {
for (a = 3;a < n;a++) b = a+2;
ok2 = 0;
for (d2 = 2;d2 <= b/2;d2++) {
if (b % d1 == 0) ok2 = 1;}
if (ok2 == 0) cout << "(" << a << "," << b << ")" << ";";
a++;
}
}
return 0;
}```
Could someone help me out with the program ?
Thanks in advance !

2. Your code formatting is just terrible, but I will help you out anyway.
Code:
```for (a = 3;a < n;a++)
{
ok1 = 0;
for (d1 = 2;d1 <= a/2; d1++)
{
if (a % d1 == 0)
{
ok1 = 1;
}
}
if (ok1 == 0)
{
b = a+2;
ok2 = 0;
for (d2 = 2;d2 <= b/2;d2++)
{
if (b % d1 == 0)
{
ok2 = 1;
}
}
if (ok2 == 0)
{
cout << "(" << a << "," << b << ")" << ";";
}
}
}```

3. Your choice of variable names are also terrible. Give them a more descriptive name.
Also, instead ok1, ok2, ..., use break and continue.

4. Originally Posted by Elysia
Your choice of variable names are also terrible. Give them a more descriptive name.
Also, instead ok1, ok2, ..., use break and continue.
This is my usual notation.I am not a native english speaker so it is common for me to use ok1,ok2 and other 'terrible' variable names.
Also my program doesn't work the way it should.If n is 11 the code will display on the screen the numbers 3,9 and 9 is not a prime number...
How should I fix the problem?

5. Most of your codes are right,just a few mistakes about the "IF".When you know how the "if" works,you will know where the mistake is.[tag]#includeint main(){ int i,j,k1,k2,a; std::cout

6. AS fallow ,I just chang the value of k1 k2 ,then it can work#includeint main(){ int i,j,k1,k2,a; std::cout

Popular pages Recent additions