Thread: friends numbers program

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    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. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    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. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your choice of variable names are also terrible. Give them a more descriptive name.
    Also, instead ok1, ok2, ..., use break and continue.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    Quote Originally Posted by Elysia View Post
    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. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    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. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    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 subscribe to a feed

Similar Threads

  1. non prime numbers or composite numbers program.help plz!
    By danishzaidi in forum C Programming
    Replies: 10
    Last Post: 11-15-2011, 11:10 AM
  2. hi friends can i help this program error
    By karthikeyanvisu in forum C Programming
    Replies: 5
    Last Post: 02-27-2011, 05:14 AM
  3. Replies: 3
    Last Post: 09-08-2010, 10:26 AM
  4. need a help friends... pls
    By joelove in forum C Programming
    Replies: 4
    Last Post: 02-08-2010, 12:32 PM
  5. help sharing a program with friends...
    By fingerlickin in forum C# Programming
    Replies: 1
    Last Post: 11-05-2005, 09:54 AM