[3891 views]

A number is said to be a prime number when it has only two factors, that is, when the factors of the number are 1 and the number itself. Example: 2, 5, 17, 19, etc.

There are many theories which state that any number greater than 2 can be expressed as the sum of two prime numbers. We must also keep in mind that the sum of two prime numbers does not necessarily have to be a prime number. In this article, we will check whether a given number can be displayed as the sum of two prime numbers.

For example:

15 = 2 + 13

25 = 2 + 23

Now let’s take a look at the algorithm and flowchart to check whether two given numbers are twin prime or not, for better understanding.

To avoid redundancy, we will use the concept of functions. Here, we will use a function to check whether a number is prime or not. While writing the program, we can call this function any number of times we want.

**Assumption:** For this algorithm, we assume that the function takes a number ‘num’, to be checked and returns true if the number is prime, otherwise, returns false.

In this problem, we need to check whether a number can be displayed as the sum of two prime numbers. To check this, we need to check whether a number is prime or not, multiple times. Hence, we have written a separate function for doing the prime number check. This function will return true if the number is prime, else returns false.

To do the prime number checking, we must find out the number of factors of the number. We first initialize the number of factors, ‘f’ as 0. Now, we start a loop from 1 to num, where the loop variable is ‘i’. If num% i=0, f is incremented by 1. In this line, we are checking whether the number is divisible by ‘i’ or not. If yes, f is incremented. If the number of factors is equal to 2, the function returns 0, else it returns false.

Now, coming to the main algorithm, we start off by taking the number to be checked as user input and store it in a variable, say ‘n’. We initialize a flag as false and the loop variable I as 2. Now, we check whether i is a prime number. If yes, we check whether ‘n-i’ is a prime number. If both the numbers are prime, we display the numbers and the flag is set as true. Therefore, the number ‘n’ can be displayed as the sum of i and n-i. In the same manner, we check for other pairs. Once the loop is terminated, if the flag is still false, this means the number cannot be displayed as the sum of two prime numbers. Hence, an appropriate message is displayed and the algorithm is terminated.

Note: Here ‘%’ is the modulus operator which returns the remainder value after division.

Use Below Search to get Best Discounts Online

- Algorithm and Flowchart for Stack using Arrays
- Subtraction of Two Matrix Algorithm and Flowchart
- Algorithm and Flowchart to find the smallest element in an array
- Algorithm and Flowchart to find all the factors of an Integer
- Algorithm and Flowchart to check whether a number is Krishnamurthy number or not