- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Find ways an Integer can be expressed as sum of n-th power of unique natural numbers in C++

In this tutorial, we are going to write a program that find the number of ways a integer can be expressed as sum of given n-th power of unique numbers.

We have two integers **number** and **power**. And we need to find in how many ways can we represent the given **number** as sum of n-th power of unique natural numbers. Let's see an example.

**Input** − number = 50, power = 2

**Output** − 3

There is only possible way we can write 4 as sum of 2 powers.

We will be using recursion to solve the problem. Let's see the steps to solve the problem.

Initialize the number and power.

Write a recursive function with a suitable name. It accepts

**number**,**power**and**i**as arguments.If the

**number**is less than zero or**pow(i, power)**is greater than**number**, then return 0.If the number is zero or pow(i, power) is equal to the number, then return 1.

We have two recursive calls for the functions to compute the total number of ways

Increment the

**i**.In the first recursive call, check for the numbers less than given number.

In the second recursive call, check for the given number.

## Example

Let's see the code.

#include <bits/stdc++.h> using namespace std; int findPossibleWaysCount(int number, int power, int i = 1) { if(number < 0 || number < pow(i, power)) { return 0; } if(number == 0 || number == pow(i, power)) { return 1; } return findPossibleWaysCount(number - pow(i, power), power, i + 1) + findPossibleWaysCount(number, power, i + 1); } int main() { // initializing the number and power int number = 50, power = 2; cout << findPossibleWaysCount(number, power) << endl; return 0; }

## Output

If you run the above code, then you will get the following result.

3

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.

- Related Articles
- C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers
- Check if an integer can be expressed as a sum of two semi-primes in Python
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Program to find number of ways we can get a number which is sum of nth power of unique numbers in Python
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- Find m-th summation of first n natural numbers in C++
- List at least three different ways in which 67 be expressed as the sum of three different prime numbers
- Check if a number can be expressed as power in C++
- Check if a number can be expressed as sum two abundant numbers in C++
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Swift program to check whether a number can be expressed as sum of two prime numbers
- Haskell Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Find the sum of first $n$ odd natural numbers.