The Program in C++ Program to find Disarium numbers between 1 to 1000 or not is given below:
#include <iostream>
#include <cmath>
using namespace std;
int numberOfDigits(int n) {
int count = 0;
while (n > 0) {
count++;
n /= 10;
}
return count;
}
bool isDisariumNumber(int n) {
int digits = numberOfDigits(n);
int sum = 0, originalNumber = n;
while (n > 0) {
int digit = n % 10;
sum += pow(digit, digits);
n /= 10;
digits--;
}
return sum == originalNumber;
}
int main() {
cout << "Hello Codeauri Family,The Disarium numbers between 1 and 1000 are: ";
for (int i = 1; i <= 1000; i++) {
if (isDisariumNumber(i)) {
cout << i << " ";
}
}
cout << endl;
return 0;
}
Output:
Hello Codeauri Family,The Disarium numbers between 1 and 1000 are: 1 2 3 4 5 6 7 8 9 89 135 175 518 598
Pro-Tips💡
This program uses the same numberOfDigits
and isDisariumNumber
functions as in the previous program.
The main
function loops through all numbers between 1 and 1000 and calls the isDisariumNumber
function for each number.
If the function returns true, the number is a disarium number and is printed.
The output is a list of all disarium numbers between 1 and 1000.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.