0

Q: Write a program which will print all the pairs of prime numbers whose sum equals the number entered by the user.

My program is able to find the prime numbers until an integer value, n, and outputs it correctly.

Now how do I find the pairs of prime numbers that add up to n.

Any ideas? Should I use arrays?

Here is my code so far.

   //Write a program which will print all the pairs of prime numbers whose sum equals the 
    //number entered by the user. ( suggested by Aniseed ) (Intermediate)

    #include <iostream>

    using namespace std;

    void PrimeNumbers(int n, int count);

    int main(){

        int n = 0;
        int count =0;
        int limit =0;

        cout << "Enter an integer:" << endl;
        cin >> limit;

        for(int n=1; n <= limit; n++){
            PrimeNumbers(n,count);
        }

        cin.get();
        cin.get();
    }

    void PrimeNumbers(int n, int count){


        for(int i =1; i <= n; i++){
            if(n%i==0){
                count ++;
            }

        }

        if(count == 2){
                cout << n << " ";
        }

    }
3
Contributors
5
Replies
50
Views
3 Years
Discussion Span
Last Post by prakash4
1
#include<iostream.h>
#include<conio.h>
 void prime();
int main()
{
             prime();
             getch();
             }

             void prime()
             {
                  int i=0,j=0,k=0,l=0,m=0,n=0,g[20],count=0,v=0;
                  cout<<"Enter integer";
                  cin>>n;
                  while(m<=n)
                  {
                  for(i=1;i<=n;i++)
                            {         
                            if(m%i==0)
                            count++;                                                                    
                            }
                  if(count == 2)
                  {
                  g[k]=m;
                  k++;                                      
                  }
                  count=0;
                  m++;
                  }                           
                  for(l=0;l<k;l++)
                  {

                  for(v=0;v<k;v++)
                  {                                     
                  j=g[l]+g[v];
                  if(j==n)
                  cout<<g[l]<<" "<<g[v]<<endl;
                  }
                  }
                  }

this will be useful to u.......if u enter 5 then the output will be the pair (2,3) and (3,2).....am i correct??

Edited by prakash4

Votes + Comments
yes you are correct
0

u can divide this as two parts
part1: generating prime numbers
part2:selecting the pair of numbers which sum matches the given integer....
from for(l=o;l<k;l++).....the second part will be started.....
in this you will check all the prime numbers sum which matches the integer....
To retain this I kept all the generated prime numbers in an array called g[]....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.