Check whether the sum of absolute difference of adjacent digits is Prime or not

Given a number a N and the task is to check weather the sum of absolute difference of adjacent digit is a prime or not.

Examples:

Input: N = 142 
Output: Prime
Sum = |1-4| + |4-2| = 5 i.e. prime.

Input: N = 347
Output: Not prime

Approach: Find the sum of absolute difference of adjacent digits and then check if that sum is prime or not.

Below is the implementation of the above approach:

C++

// C++ implementation of the above approach
#include

using namespace std;

// Function to check for a prime number
bool Prime(int n){

if( n == 1){
return false;
}
for (int i=2;i*i<=n;i++){ if (n % i == 0) return false; } return true; } // Function to find the sum of array bool checkSumPrime(string st){ int summ = 0; for (int i=1;i Output:

Prime


My Personal Notes arrow_drop_up

Data science |Machine learning|Programming facebook -https//wwwfacebookcom/profilephpid=100002787011326

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29