Given a number, we need to find the sum of all the digits of a number which we get after raising the number to a specified power.

Examples:

Input: number = 5, power = 4 Output: 13 Explanation: Raising 5 to the power 4 we get 625. Now adding all the digits = 6 + 2 + 5 Input: number = 9, power = 5 Output: 27 Explanation: Raising 9 to the power 5 we get 59049. Now adding all the digits = 5 + 9 + 0 + 4 + 9

The approach for Python is explained. we have used pow() function to calculate the base to the power value. Then we have extracted every digit as string using str() method. Since we can’t calculate the sum of strings, we converted every string digit back to integer using int() method. Finally, we used sum() function to get the sum of all the digits. This solution will look very simple in Python but it won’t be so short in other languages. After running both the codes, one can compare the time elapsed and the memory used in both the given language i.e., Python and Java.

Below is the implementation of above idea :

## C++

`// CPP program to illustrate the given problem ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `calculate(` `int` `n, ` `int` `power) ` `{ ` ` ` `int` `sum = 0; ` ` ` `int` `bp = (` `int` `)` `pow` `(n, power); ` ` ` `while` `(bp != 0) { ` ` ` `int` `d = bp % 10; ` ` ` `sum += d; ` ` ` `bp /= 10; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `int` `power = 4; ` ` ` `cout << calculate(n, power); ` `} ` ` ` `// This code is contributed by Nikita Tiwari ` |

*chevron_right*

*filter_none*

## Java

`// Java program to illustrate the given problem ` `public` `class` `base_power { ` ` ` `static` `int` `calculate(` `int` `n, ` `int` `power) ` ` ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` `int` `bp = (` `int` `)Math.pow(n, power); ` ` ` `while` `(bp != ` `0` `) { ` ` ` `int` `d = bp % ` `10` `; ` ` ` `sum += d; ` ` ` `bp /= ` `10` `; ` ` ` `} ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `int` `power = ` `4` `; ` ` ` `System.out.println(calculate(n, power)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to illustrate the given problem ` `def` `calculate(n, power): ` ` ` `return` `sum` `([` `int` `(i) ` `for` `i ` `in` `str` `(` `pow` `(n, power))]) ` ` ` `# Driver Code ` `n ` `=` `5` `power ` `=` `4` `print` `(calculate(n, power)) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of digits of ` `// a given number to a given power ` `using` `System; ` ` ` `public` `class` `base_power ` `{ ` ` ` ` ` `// Function to calculate sum ` ` ` `static` `int` `calculate(` `int` `n, ` `int` `power) ` ` ` `{ ` ` ` `int` `sum = 0; ` ` ` `int` `bp = (` `int` `)Math.Pow(n, power); ` ` ` `while` `(bp != 0) ` ` ` `{ ` ` ` `int` `d = bp % 10; ` ` ` `sum += d; ` ` ` `bp /= 10; ` ` ` `} ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `int` `power = 4; ` ` ` `Console.WriteLine(calculate(n, power)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to illustrate ` `// the given problem ` ` ` `function` `calculate(` `$n` `, ` `$power` `) ` `{ ` ` ` ` ` `$sum` `= 0; ` ` ` `$bp` `= (int)pow(` `$n` `, ` `$power` `); ` ` ` `while` `(` `$bp` `!= 0) ` ` ` `{ ` ` ` `$d` `= ` `$bp` `% 10; ` ` ` `$sum` `+= ` `$d` `; ` ` ` `$bp` `/= 10; ` ` ` `} ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `$power` `= 4; ` `echo` `(calculate(` `$n` `, ` `$power` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

13

This article is contributed by **Chinmoy Lenka**. 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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of digits in 2 raised to power n
- Number of digits in N factorial to the power N
- Find last five digits of a given five digit number raised to power five
- Find the first and last M digits from K-th power of N
- Print last k digits of a^b (a raised to power b)
- Check if given number is a power of d where d is a power of 2
- Find smallest number with given number of digits and sum of digits under given constraints
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits
- Minimum number of digits to be removed so that no two consecutive digits are same
- Find the average of k digits from the beginning and l digits from the end of the given number
- Smallest number with given sum of digits and sum of square of digits
- Check if the sum of digits of number is divisible by all of its digits
- Check if a large number is divisible by a number which is a power of 2
- Sum of the digits of square of the given number which has only 1's as its digits