A Number is a disarium number if the sum of the digits powered with their respective positions is equal to the number itself.
For Example:
89 is a Disarium number.
89: → 81 + 92 = 8 + 81 = 89
#include <stdio.h>
#include<math.h>
int main()
{
int n, temp;
int sum = 0, count = 0;
printf("Enter a number : ");
scanf("%d", &n);
// First Count the digits of the number
temp = n;
while(temp!=0){
temp = temp/10;
count = count + 1;
}
temp = n;
while(count!=0){
int last_digit = temp%10;
temp = temp/10;
sum = sum + pow(last_digit,count);
count = count - 1;
}
if(sum == n){
printf("%d is a Disarium Number", n);
}else{
printf("%d is not a Disarium Number", n);
}
return 0;
}
// 175 = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 50 + 125 = 175
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int n, temp;
int sum = 0, count = 0;
cout<<"Enter a number : ";
cin>>n;
// First Count the digits of the number
temp = n;
while(temp!=0){
temp = temp/10;
count = count + 1;
}
temp = n;
while(count!=0){
int last_digit = temp%10;
temp = temp/10;
sum = sum + pow(last_digit,count);
count = count - 1;
}
if(sum == n){
cout<<n<<" is a Disarium Number";
}else{
cout<<n<<" is not a Disarium Number";
}
return 0;
Comments
Oops!