# Recursive sum of digits of a number is prime or not

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
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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 ` `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

 ` `

Output:

```No
```

This article is contributed by Twinkle Bajaj. 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.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Improved By : jit_t, vt_m

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.