Primality test for the sum of digits at odd places of a number | C ++ program to do Primality test for the sum of digits at odd places of a number ; Function that return sum of the digits at odd places ; Function that returns true if the number is prime else false ; Corner cases ; This condition is checked so that we can skip middle five numbers in the below loop ; Driver code ; Get the sum of the digits at odd places
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum_odd ( int n ) { int sum = 0 , pos = 1 ; while ( n ) { if ( pos % 2 == 1 ) sum += n % 10 ; n = n / 10 ; pos ++ ; } return sum ; } bool check_prime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } int main ( ) { int n = 223 ; int sum = sum_odd ( n ) ; if ( check_prime ( sum ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }