Deserium Number with examples in C++ Program

C++Server Side ProgrammingProgramming

In this tutorial, we are going to learn about the deserium numbers with examples.

The number whose sum of pow(digit, digitsCount) is equal to the given number is called Deserium number.

Let's see the steps to find whether the given number is deserium number or not.

  • Initialize the number.

  • Find the digits count of the number.

  • Initialize a variable to store the sum.

  • Iterate until the number is less than 0.

    • Get the last digit by diving the number with 10.

    • Add the pow(digit, digitsCount) to the sum.

  • If the sum is equal to the number, then it is deserium number else it is not.

Example

Let's see the code.

 Live Demo

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int getDigitsCount(int n) {
   int digitsCount = 0;
   do {
      digitsCount++;
      n = n / 10;
   }
   while (n != 0);
      return digitsCount;
}
bool isDeseriumNumber(int n) {
   int originalNumber = n;
   int digitsCount = getDigitsCount(n);
   int sum = 0;
   while (n != 0) {
      int digit = n % 10;
      sum += pow(digit, digitsCount);
      digitsCount--;
      n = n / 10;
   }
   return sum == originalNumber;
}
int main() {
   int n = 135;
   // int n = 123;
   if (isDeseriumNumber(n)) {
      cout << "Yes";
   }
   else {
      cout << "No";
   }
   cout << endl;
   return 0;
}

Output

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

Yes

Conclusion

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

raja
Published on 27-Jan-2021 12:25:51
Advertisements