Check Number can Express as Sum of Prime Numbers
Write a C++ program to Check Number can Express as Sum of Prime Numbers. Here’s simple C++ program to Check Number can Express as Sum of Prime Numbers in C++ Programming Language.
What are Functions ?
Function is a block of statements that performs some operations. All C++ programs have at least one function – function called “main()”. This function is entry-point of your program.
A function declaration tells the compiler about a function’s name, return type, and parameters. A function definition provides the actual body of the function.
Defining a Function : :
The general form of a C++ function definition is as follows:
return_type Function_Name( list of parameters )
{
//function’s body
}
- return_type : suggests what the function will return. It can be void, int, char, some pointer or even a class object.
- Function_Name : is the name of the function, using the function name it is called.
- Parameters : are variables to hold values of arguments passed while function is called. A function may or may not contain parameter list.
- Function body : is he part where the code statements are written.
Below is the source code for C++ program to Check Number can Express as Sum of Prime Numbers which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ program to Check Number can Express as Sum of Prime Numbers */ #include <iostream> using namespace std; bool checkPrime(int n); int main() { int n, i; bool flag = false; cout << "\nEnter any positive integer :: "; cin >> n; cout<<"\n"; for(i = 2; i <= n/2; ++i) { if (checkPrime(i)) { if (checkPrime(n - i)) { cout << n << " = " << i << " + " << n-i << endl; flag = true; } } } if (!flag) cout <<"\nThe Number [ "<< n << " ] can't be expressed as sum of two prime numbers.\n"; return 0; } // Check prime number bool checkPrime(int n) { int i; bool isPrime = true; for(i = 2; i <= n/2; ++i) { if(n % i == 0) { isPrime = false; break; } } return isPrime; }
OUTPUT : :
/* C++ program to Check Number can Express as Sum of Prime Numbers */ Enter any positive integer :: 1234 1234 = 3 + 1231 1234 = 5 + 1229 1234 = 11 + 1223 1234 = 17 + 1217 1234 = 41 + 1193 1234 = 47 + 1187 1234 = 53 + 1181 1234 = 71 + 1163 1234 = 83 + 1151 1234 = 131 + 1103 1234 = 137 + 1097 1234 = 173 + 1061 1234 = 251 + 983 1234 = 257 + 977 1234 = 263 + 971 1234 = 281 + 953 1234 = 293 + 941 1234 = 347 + 887 1234 = 353 + 881 1234 = 461 + 773 1234 = 491 + 743 1234 = 557 + 677 1234 = 587 + 647 1234 = 593 + 641 1234 = 617 + 617 Process returned 0
Above is the source code for C++ program to Check Number can Express as Sum of Prime Numbers which is successfully compiled and run on Windows System.The Output of the program is shown above .
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….