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++
#include <bits/stdc++.h>
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;
}
|
Java
import java.io.*;
class GFG {
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;
}
static boolean 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 ;
}
public static void main (String[] args) {
int n = 223 ;
int sum = sum_odd(n);
if (check_prime(sum))
System.out.println ( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
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
def check_prime(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return True
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
n = 223
sums = sum_odd(n)
if (check_prime(sums)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class GFG{
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;
}
static 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 ;
}
static public void Main (){
int n = 223;
int sum = sum_odd(n);
if (check_prime(sum))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
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 check_prime( $n )
{
if ( $n <= 1)
return false;
if ( $n <= 3)
return true;
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;
}
$n = 223;
$sum = sum_odd( $n );
if (check_prime( $sum ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function sum_odd(n)
{
let sum = 0, pos = 1;
while (n) {
if (pos % 2 == 1)
sum += n % 10;
n = Math.floor(n / 10);
pos++;
}
return sum;
}
function check_prime(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for (let i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
let n = 223;
let sum = sum_odd(n);
if (check_prime(sum))
document.write( "YES" + "<br>" );
else
document.write( "NO" + "<br>" );
</script>
|
Time Complexity: O(log10n + sqrt(n))
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!