Given an integer ‘n’, the task is to check whether the sum of digits at the odd positions (from right to left) is prime or not.
If it is prime then, print “YES” or “NO” otherwise.
Examples:
Input: n = 123
Output: NO
As, 1 + 3 = 4 is not prime.Input: n = 42
Output: YES
Since, 2 is a prime.
Approach: First, find the sum of the digits which are at odd positions i.e, 1, 3, 5, … (starting from right).
If the sum is prime then print ‘YES’ else print ‘NO’.
Below is the implementation of the above approach:
C++
// C++ program to do Primality test // for the sum of digits at // odd places of a number #include <bits/stdc++.h> using namespace std; // Function that return sum // of the digits at odd places 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; } // Function that returns true // if the number is prime // else false bool check_prime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This condition is checked so that // we can skip middle five // numbers in the below loop 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; } // Driver code int main() { int n = 223; // Get the sum of the // digits at odd places int sum = sum_odd(n); if (check_prime(sum)) cout << "YES" << endl; else cout << "NO" << endl; return 0; } |
Java
// Java program to do Primality test // for the sum of digits at // odd places of a number import java.io.*; class GFG { // Function that return sum // of the digits at odd places static int sum_odd(int n) { int sum = 0, pos = 1; while (n>0) { if (pos % 2 == 1) sum += n % 10; n = n / 10; pos++; } return sum; } // Function that returns true // if the number is prime // else false static boolean check_prime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This condition is checked so that // we can skip middle five // numbers in the below loop 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; } // Driver code public static void main (String[] args) { int n = 223; // Get the sum of the // digits at odd places int sum = sum_odd(n); if (check_prime(sum)) System.out.println ("YES" ); else System.out.println("NO"); } } |
Python3
# Python3 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 def sum_odd(n): sums = 0 pos = 1 while (n!=0): if (pos % 2 == 1): sums += n % 10 n = n // 10 pos+=1 return sums # Function to check if a # number is prime def check_prime(n): # Corner cases if (n <= 1): return False if (n <= 3): return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 == 0 or n % 3 == 0): return False for i in range(5,n,6): if (n % i == 0 or n % (i + 2) == 0): return False return True #driver code n = 223# Get the sum of the # digits at odd places sums = sum_odd(n) if (check_prime(sums)): print("YES") else: print("NO") #this code is improved by sahilshelangia |
C#
// C# program to do Primality test // for the sum of digits at // odd places of a number using System; public class GFG{ // Function that return sum // of the digits at odd places static int sum_odd(int n) { int sum = 0, pos = 1; while (n>0) { if (pos % 2 == 1) sum += n % 10; n = n / 10; pos++; } return sum; } // Function that returns true // if the number is prime // else false static bool check_prime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This condition is checked so that // we can skip middle five // numbers in the below loop 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; } // Driver code static public void Main (){ int n = 223; // Get the sum of the // digits at odd places int sum = sum_odd(n); if (check_prime(sum)) Console.WriteLine("YES" ); else Console.WriteLine("NO"); } } |
PHP
<?php // PHP 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 sum_odd($n) { $sum = 0; $pos = 1; while ($n) { if ($pos % 2 == 1) $sum += $n % 10; $n = (int)($n / 10); $pos++; } return $sum; } // Function that returns true // if the number is prime // else false function check_prime($n) { // Corner cases if ($n <= 1) return false; if ($n <= 3) return true; // This condition is checked so // that we can skip middle five // numbers in the below loop if ($n % 2 == 0 || $n % 3 == 0) return false; for ($i = 5; $i * $i <= $n; $i = ($i + 6)) if ($n % $i == 0 || $n % ($i + 2) == 0) return false; return true; } // Driver code $n = 223; // Get the sum of the // digits at odd places $sum = sum_odd($n); if (check_prime($sum)) echo "YES"; else echo "NO"; // This code is contributed by ajit ?> |
YES
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check whether sum of digits at odd places of a number is divisible by K
- Find the sum of digits of a number at even and odd places
- Check if product of digits of a number at even and odd places is equal
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Primality Test | Set 1 (Introduction and School Method)
- Primality Test | Set 2 (Fermat Method)
- Primality Test | Set 3 (Miller–Rabin)
- Primality Test | Set 4 (Solovay-Strassen)
- Lucas Primality Test
- AKS Primality Test
- Primality Test | Set 5(Using Lucas-Lehmer Series)
- Vantieghems Theorem for Primality Test
- Implementation of Wilson Primality test
- Check whether product of digits at even places of a number is divisible by K
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Arrangement of the characters of a word such that all vowels are at odd places
- Count of numbers in range which are divisible by M and have digit D at odd places
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
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.

