online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
//Created by Usman Khan. //Basic idea from Pratyush Kumar https://www.sololearn.com/Profile/4584198/ /************************************************************************** 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 make condition false and skipps 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; } // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu // Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue