Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ Program to Check Disarium Number

Posted in C++ Tutorial
Updated on Oct 27, 2024
By Mari Selvan
πŸ‘οΈ 21 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
C++ Program to Check Disarium Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of programming, exploring mathematical patterns is a fascinating endeavor. One such numeric pattern is the Disarium number.

A Disarium number is a number defined by the sum of its digits each raised to the power of its respective position.

In this tutorial, we'll delve into a C++ program designed to check whether a given number is a Disarium number.

πŸ“„ Example

Let's explore the C++ code that checks whether a given number is a Disarium number.

isDisarium.cpp
Copied
Copy To Clipboard
#include <iostream>
#include <cmath>

// Function to count the number of digits in a given number
int countDigits(int number) {
  int count = 0;
  while (number != 0) {
    count++;
    number /= 10;
  }
  return count;
}

// Function to check if a number is a Disarium number
bool isDisarium(int number) {
  int originalNumber = number;
  int digitCount = countDigits(number);
  int sum = 0;

  while (number != 0) {
    int digit = number % 10;
    sum += pow(digit, digitCount);
    digitCount--;
    number /= 10;
  }

  return (sum == originalNumber);
}

// Driver program
int main() {
  // Replace this value with your desired number
  int inputNumber = 89;

  // Call the function to check if the number is Disarium
  if (isDisarium(inputNumber)) {
    std::cout << inputNumber << " is a Disarium number." << std::endl;
  } else {
    std::cout << inputNumber << " is not a Disarium number." << std::endl;
  }

  return 0;
}

πŸ’» Testing the Program

To test the program with different numbers, replace the value of inputNumber in the main function.

Output
89 is a Disarium number.

Compile and run the program to check if the number is a Disarium number.

🧠 How the Program Works

  1. The program defines a function countDigits to count the number of digits in a given number.
  2. The isDisarium function checks if a number is a Disarium number by summing the digits each raised to the power of its position.
  3. The main function tests the isDisarium function with a sample number (replace it with your desired number).

πŸ“ Between the Given Range

Let's take a look at the C++ code that checks for Disarium numbers in the range from 1 to 100.

isDisarium.cpp
Copied
Copy To Clipboard
#include <iostream>
#include <cmath>

// Function to check if a number is Disarium
bool isDisarium(int number) {
    int num = number;
    int digitCount = 0;
    int sum = 0;

    // Count digits
    while (num != 0) {
        num /= 10;
        digitCount++;
    }

    // Reset num to original value
    num = number;

    // Calculate sum of digits raised to the power of their position
    while (num != 0) {
        int digit = num % 10;
        sum += pow(digit, digitCount);
        num /= 10;
        digitCount--;
    }

    return sum == number;
}

// Driver program
int main() {
    std::cout << "Disarium numbers in the range 1 to 100:" << std::endl;

    // Check and print Disarium numbers in the range 1 to 100
    for (int i = 1; i <= 100; ++i) {
        if (isDisarium(i)) {
            std::cout << i << " ";
        }
    }

    std::cout << std::endl;

    return 0;
}

πŸ’» Testing the Program

Output
Disarium numbers in the range 1 to 100:
1 2 3 4 5 6 7 8 9 89

Compile and run the program to see the Disarium numbers in the specified range.

🧠 How the Program Works

  1. The program defines a function isDisarium that checks if a given number is a Disarium number.
  2. Inside the function, it calculates the count of digits and then calculates the sum of digits raised to the power of their respective positions.
  3. The main function uses a loop to iterate through numbers from 1 to 100 and prints those that are Disarium numbers.

🧐 Understanding the Concept of Disarium Numbers

Before we dive into the code, let's grasp the concept of Disarium numbers.

A Disarium number is a number such that the sum of its digits, each raised to the power of its position, equals the number itself.

For example, the number 89 is a Disarium number because 8^1 + 9^2 equals 89.

πŸŽ‰ Conclusion

Understanding and implementing programs to identify numeric patterns, such as Disarium numbers, is a valuable skill in the world of programming.

The provided C++ program offers a practical example of checking whether a given number follows the Disarium pattern.

Feel free to use and modify this code for your specific use cases. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (C++ Program to Check Disarium Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy