Recursive sum of digits of a number is prime or not
Last Updated :
28 Apr, 2021
Given a number n, we need to find the sum of each digits of the number till the number becomes a single digit. We need to print "yes" if the sum is a prime or "no" if it is not prime.
Examples:
Input : 5602
Output: No
Explanation:
Step 1- 5+6+0+2 = 13
Step 2- 1+3 = 4
4 is not prime
Input : 56
Output : Yes
Explanation:
Step 1- 5+6 = 11
Step 2- 1+1 = 2 hence 2 is prime
The idea is simple, we can quickly find recursive sum of digits.
int recDigSum(int n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
Once, recursive sum is calculated, check if it is prime or not by simply checking if it is 2, 3, 5 or 7 (These are only single digit primes).
C++
// CPP code to check if
// recursive sum of
// digits is prime or not.
#include<iostream>
using namespace std;
// Function for recursive
// digit sum
int recDigSum(int n)
{
if (n == 0)
return 0;
else
{
if (n % 9 == 0)
return 9;
else
return n % 9;
}
}
// function to check if prime
// or not the single digit
void check(int n)
{
// calls function which
// returns sum till
// single digit
n = recDigSum(n);
// checking prime
if (n == 2 or n == 3 or n == 5 or n == 7)
cout << "Yes";
else
cout << "No";
}
// Driver code
int main()
{
int n = 5602;
check(n);
}
// This code is contributed by Shreyanshi.
Java
// java code to check if
// recursive sum of
// digits is prime or not.
import java.io.*;
class GFG
{
// Function for recursive
// digit sum
static int recDigSum(int n)
{
if (n == 0)
return 0;
else
{
if (n % 9 == 0)
return 9;
else
return n % 9;
}
}
// function to check if prime
// or not the single digit
static void check(int n)
{
// calls function which
// returns sum till
// single digit
n = recDigSum(n);
// checking prime
if (n == 2 || n == 3 || n == 5 || n == 7)
System.out.println ( "Yes");
else
System.out.println("No");
}
// Driver code
public static void main (String[] args)
{
int n = 5602;
check(n);
}
}
// This code is contributed by vt_m
Python
# Python code to check if recursive sum
# of digits is prime or not.
def recDigSum(n):
if n == 0:
return 0
else:
if n % 9 == 0:
return 9
else:
return n % 9
# function to check if prime or not
# the single digit
def check(n):
# calls function which returns sum
# till single digit
n = recDigSum(n)
# checking prime
if n == 2 or n == 3 or n == 5 or n == 7:
print "Yes"
else:
print "No"
# driver code
n = 5602
check(n)
C#
// C# code to check if
// recursive sum of
// digits is prime or not.
using System;
class GFG
{
// Function for recursive
// digit sum
static int recDigSum(int n)
{
if (n == 0)
return 0;
else
{
if (n % 9 == 0)
return 9;
else
return n % 9;
}
}
// function to check if prime
// or not the single digit
static void check(int n)
{
// calls function which
// returns sum till
// single digit
n = recDigSum(n);
// checking prime
if (n == 2 || n == 3
|| n == 5 || n == 7)
Console.WriteLine ( "Yes");
else
Console.WriteLine("No");
}
// Driver code
public static void Main ()
{
int n = 5602;
check(n);
}
}
// This code is contributed by anuj_67.
PHP
<?php
// PHP code to check if
// recursive sum of
// digits is prime or not.
// Function for recursive
// digit sum
function recDigSum($n)
{
if ($n == 0)
return 0;
else
{
if ($n % 9 == 0)
return 9;
else
return $n % 9;
}
}
// function to check if prime
// or not the single digit
function check( $n)
{
// calls function which
// returns sum till
// single digit
$n = recDigSum($n);
// checking prime
if ($n == 2 or $n == 3 or
$n == 5 or $n == 7)
echo "Yes";
else
echo "No";
}
// Driver code
$n = 5602;
check($n);
// This code is contributed by ajit.
?>
JavaScript
<script>
// Javascript code to check if
// recursive sum of digits is
// prime or not.
// Function for recursive
// digit sum
function recDigSum(n)
{
if (n == 0)
return 0;
else
{
if (n % 9 == 0)
return 9;
else
return n % 9;
}
}
// Function to check if prime
// or not the single digit
function check(n)
{
// Calls function which
// returns sum till
// single digit
n = recDigSum(n);
// Checking prime
if (n == 2 || n == 3 ||
n == 5 || n == 7)
document.write("Yes");
else
document.write("No");
}
// Driver code
let n = 5602;
check(n);
// This code is contributed by susmitakundugoaldanga
</script>
Output:
No
Similar Reads
Print prime numbers with prime sum of digits in an array Given an array arr[] and the task is to print the additive primes in an array. Additive primes: Primes such that the sum of their digits is also a prime, such as 2, 3, 7, 11, 23 are additive primes but not 13, 19, 31 etc.Examples: Input: arr[] = {2, 4, 6, 11, 12, 18, 7} Output: 2, 11, 7 Input: arr[]
8 min read
Sum of prime numbers without odd prime digits Given an integer N. The task is to find the sum of the first N prime numbers which don't contain any odd primes as their digit.Some of such prime numbers are 2, 11, 19, 29, 41 ...... Examples: Input : N = 2 Output : 13 2 + 11 = 13Input : N = 7 Output : 252 Approach : We first use a Sieve of Eratosth
8 min read
Check if the product of digit sum and its reverse equals the number or not Given a number, check whether the product of digit sum and reverse of digit sum equals the number or not.Examples: Input : 1729 Output : Yes Explanation: digit sum = 1 + 7 + 2 + 9 = 19 Reverse of digit sum = 91 Product = digit sum * Reverse of digit sum = 19 * 91 = 1729 Input : 2334 Output : No Flow
6 min read
Sum of Digits of a Number Given a number n, find the sum of its digits.Examples : Input: n = 687Output: 21Explanation: The sum of its digits are: 6 + 8 + 7 = 21Input: n = 12Output: 3Explanation: The sum of its digits are: 1 + 2 = 3Table of Content[Approach 1] Digit Extraction - O(log10n) Time and O(1) Space[Approach 2] Using
6 min read
Numbers with sum of digits equal to the sum of digits of its all prime factor Given a range, the task is to find the count of the numbers in the given range such that the sum of its digit is equal to the sum of all its prime factors digits sum.Examples: Input: l = 2, r = 10 Output: 5 2, 3, 4, 5 and 7 are such numbers Input: l = 15, r = 22 Output: 3 17, 19 and 22 are such numb
13 min read