- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Recursive sum of digit in n^x, where n and x are very large in C++

We are given positive integer variables as ‘num’ and ‘x’. The task is to recursively calculate the num ^ x then add the digits of a resultant number till the single digit isn’t achieved and the resultant single digit will be the output.

## Let us see various input output scenarios for this −

**Input **− int num = 2345, int x = 3

**Output **− Recursive sum of digit in n^x, where n and x are very large are: 8

**Explanation **− we are given positive integer values as num and x with the values as 2345 and power as 3. Firstly, calculate 2345 ^ 3 i.e. 12,895,213,625. Now , we will add these digits i.e. 1 + 2 + 8 + 9 + 5 + 2 + 1 + 3 + 6 + 2 + 5 i.e. 44. Now we will add 4 + 4 i.e. 8. Since we have achieved the single digit therefore, output is 8.

**Input **− int num = 3, int x = 3

**Output **− Recursive sum of digit in n^x, where n and x are very large are: 9

**Explanation **− we are given positive integer values as num and x with the values as 3 and power as 3. Firstly, calculate 3 ^ 3 i.e. 9. Since we have achieved the single digit therefore, output is 9 and no further calculation is required.

## Approach used in the below program is as follows

Input an integer variable as num and x and pass the data to the function Recursive_Digit(num, x) for further processing.

Inside the function Recursive_Digit(num, x)

Declare variable ‘total’ as long and set it to call the function total_digits(num) which will return the digit sum of a number passed as an argument.

Declare variable as temp of type long and set it with power % 6

Check IF total = 3 OR total = 6 AND power > 1 then return 9.

ELSE IF, power = 1 then return total.

ELSE IF, power = 0 then return 1.

ELSE IF, temp - 0 then return call to total_digits((long)pow(total, 6))

ELSE, return total_digits((long)pow(total, temp)).

Inside the function long total_digits(long num)

Check IF num = 0 then return 0. Check IF, num % 9 = 0 then return 9.

Else, return num % 9

## Example

#include <bits/stdc++.h> using namespace std; long total_digits(long num){ if(num == 0){ return 0; } if(num % 9 == 0){ return 9; } else{ return num % 9; } } long Recursive_Digit(long num, long power){ long total = total_digits(num); long temp = power % 6; if((total == 3 || total == 6) & power > 1){ return 9; } else if (power == 1){ return total; } else if (power == 0){ return 1; } else if (temp == 0){ return total_digits((long)pow(total, 6)); } else{ return total_digits((long)pow(total, temp)); } } int main(){ int num = 2345; int x = 98754; cout<<"Recursive sum of digit in n^x, where n and x are very large are: "<<Recursive_Digit(num, x); return 0; }

## Output

If we run the above code it will generate the following Output

Recursive sum of digit in n^x, where n and x are very large are: 1

- Related Questions & Answers
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Count of values of x <= n for which (n XOR x) = (n – x) in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Find the Number of Solutions of n = x + n x using C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- C program to generate the value of x power n using recursive function
- Pow(x, n) in Python
- Find a number x such that sum of x and its digits is equal to given n in C++
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Absolute difference between the first X and last X Digits of N?
- Find a number x such that sum of x and its digits is equal to given n using C++.
- Sum of first N natural numbers which are divisible by X or Y
- Find x and y satisfying ax + by = n in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++