We are here with you hands in hands to facilitate your learning & don't appreciate the idea of copying or replicating solutions. Read More>>

Looking For Something at vustudents.ning.com? Click Here to Search

www.bit.ly/vucodes

+ Link For Assignments, GDBs & Online Quizzes Solution

www.bit.ly/papersvu

+ Link For Past Papers, Solved MCQs, Short Notes & More

//Created by Usman Khan.
//Basic idea from Pratyush Kumar  

/**************************************************************************
Goldbach's Conjecture

Goldbach's conjecture is a rule in math that states the following: every even number greater than 2 can be expressed as the sum of two prime numbers.

Write a program that finds every possible pair of prime numbers, whose sum equals the given number or a set of numbers within a range.

For example:
Input: 16
Output:
3 + 13
5 + 11

Input: 32
Output:
3 + 29
13 + 19

Input: 4, 8
Output:
4: 2 + 2
6: 3 + 3
8: 3 + 5


***************************************************************************/

#include <iostream>
using namespace std;
#define ARRAY_SIZE 1000
int prime(int n)  // Function for checking prime numbers.
{
    int c = 0;
    for (int i = 2; i < n; i++)  
    {
        if (n % i == 0) 
        {
            c++;
        }
    }
    if (c == 0)
    {
        return 1; //if not prime returns 1
    }
    else
    {
        return 0;
    }
}

int main()
{
    int rerun = 0;
    do
    {
        int num;
        int count = 0;
        int a[ARRAY_SIZE];
        int testarray1[ARRAY_SIZE], testarray2[ARRAY_SIZE];
        int testnum1 = 0, testnum2 = 0;
        
        cout "Enter any number less than 1000: " endl;
        cin >> num; //getting numbers from user whose answer user wants.
        for (int i = 2; i < num; i++) // from 2 to inputed number check every number
        {
            if (prime(i)) //if prime(i) returns 0 means number is prime it stores this number to the array.
            {
                a[count] = i;
                count++; //count variable counts total number of prime numbers.
            }
        }
        for (int j = 0; j <= count; j++)  //run loop on all prime numbers
        {
            for (int k = 0; k <= count; k++) //Again runs nested loop. To compare two numbers.  it looks like a two dimensional table or array.
            {
                if (a[k] + a[j] == num) //Checks if the one prime number (defined by outer for loop) + second prime number from the array a[] = the number entered by user.
                {
                    for (int l = 0; l < ARRAY_SIZE; l++) //runs a loop for checking 
                    {
                        if (testarray2[l] == a[k])  // checks if the one prime number is available in testarray2.
                        {
                            testnum1 = 1; // if available change testnum1 flag variable to 1.
                        }
                    }
                    for (int m = 0; m < ARRAY_SIZE; m++) //do the same for second prime number
                    {
                        if (testarray1[m] == a[j])
                        {
                            testnum2 = 1;
                        }
                    }

                    /******************************************
                    for (int l = 0; l < ARRAY_SIZE; l++) //runs a loop for checking 
                    {
                        if (testarray2[l] == a[k])  // checks if the one prime number is available in testarray2.
                        {
                            testnum1 = 1; // if available change testnum1 flag variable to 1.
                        }
                    }
                    for (int m = 0; m < ARRAY_SIZE; m++) //do the same for second prime number
                    {
                        if (testarray1[m] == a[j])
                        {
                            testnum2 = 1;
                        }
                    }

                    This code checks and prevents the same numbers from printing again and again
                    on the screen.

                    For Example;
                    input: 10
                    10 = 3 + 7
                    10 = 5 + 5
                    10 = 7 + 3 //This prevents this type of repetations.
                    ********************************************/


                    if ( !( testnum1 == 1 && testnum2 == 1) ) //Checks if the both falg variables are 1.  
                    {


                        /********************
                        If both are one its means this combination is already printed.
                        so NOT operator makes condition false and skips this code.
                        Otherwise the combination is not yet printed and this codes executes.
                        *********************/


                        cout endl num " = " a[k] " + " a[j]; //prints anser.
                        testarray1[k] = a[k]; //saves first number in testarray1
                        testarray2[j] = a[j]; // saves second number in testarray2
                    }
                }            
            }            
        }        
        cout endl "Do you want to check another number?" endl;
        cout "Enter 1 to continue: " endl "Enter any other number to exit: " endl;
        cin >> rerun;  // Gets input from user if user wnat to check another number or want to exit.
        for (int n = 0; n < ARRAY_SIZE; n++)  //if user reuses programe we have to cleare old values and make code in free condition.
        {
            testarray1[n] = 0;
        }
        for (int p = 0; p < ARRAY_SIZE; p++) // these two loops make all values of testarray1 and 2 to zero. 
        {
            testarray1[p] = 0;
        }
    } while (rerun == 1); // if answer of user is 1 condition become true and program run from start.
    return 0;
}
                            
                                enlightened
Tell me how's this and try yourself.

Run Program

+ How to Join Subject Study Groups & Get Helping Material?

+ How to become Top Reputation, Angels, Intellectual, Featured Members & Moderators?

+ VU Students Reserves The Right to Delete Your Profile, If?


See Your Saved Posts Timeline

Views: 41

.

+ http://bit.ly/vucodes (Link for Assignments, GDBs & Online Quizzes Solution)

+ http://bit.ly/papersvu (Link for Past Papers, Solved MCQs, Short Notes & More)

+ Click Here to Search (Looking For something at vustudents.ning.com?)

+ Click Here To Join (Our facebook study Group)

Latest Activity

+AAg❤️ replied to + "αяsαℓ " Ќąƶµяɨ •"'s discussion Muhabato me li gaye kasmain
3 minutes ago
+ ! ! No Name updated their profile
12 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + Iuuoɔǝut+'s blog post Things about Arabic Language in an info graphic
43 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + Iuuoɔǝut+'s blog post The End Is Not Near, But If An 'Insect Apocalypse' Ever Happens, How Would We Know?
43 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + Iuuoɔǝut+'s blog post Dengue fever facts
43 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked Atif Tariq's discussion Redmi Note 8 Pro 6GB Ram 64 MP Camera Mazey ka Phone hai ya nh ?
44 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + "Jɨyą's discussion okey :x
44 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + "Jɨyą's discussion Kia fark perta h...! XD
44 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + "AS"'s discussion Log tou Masla Banaty Hain..
44 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + "AS"'s discussion Jesi Ab hai ..
44 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + "AS"'s discussion Dil Behlane K liye Log..
44 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + "AS"'s discussion Suna Hai Ab ..
44 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + "αяsαℓ " Ќąƶµяɨ •"'s discussion Muhabato me li gaye kasmain
44 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + M.Tariq Malik's group STA301 Statistics and Probability
46 minutes ago
+ ! ! ! ! ! ! ! ! ! AG joined + M.Tariq Malik's group
46 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + M.Tariq Malik's group MTH401 Differential Equations
47 minutes ago
+ ! ! ! ! ! ! ! ! ! AG joined + M.Tariq Malik's group
48 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + M.Tariq Malik's group MCM301 Communication skills
49 minutes ago
+ ! ! ! ! ! ! ! ! ! AG joined + M.Tariq Malik's group
49 minutes ago
+ ! ! ! ! ! ! ! ! ! AG liked + M.Tariq Malik's group CS602 Computer Graphics
49 minutes ago

© 2019   Created by + M.Tariq Malik.   Powered by

Promote Us  |  Report an Issue  |  Privacy Policy  |  Terms of Service