C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to determine whether a given number is a Disarium number
ExplanationIn this program, we need to check whether the given number is Disarium or not. Disarium number A number is said to be the Disarium number when the sum of its digit raised to the power of their respective positions is equal to the number itself. For example, 175 is a Disarium number as follows 11 + 72 + 53 = 1 + 49 + 125 = 175 Some of the other examples of Disarium number are 89, 135, 518 etc. To find whether given number is Disarium or not, calculate the sum of digits powered with their respective positions. If the sum is equal to the original number then, the given number is Disarium number. Algorithm
SolutionPython
#calculateLength() will count the digits present in a number
def calculateLength(n):
length = 0;
while(n != 0):
length = length + 1;
n = n//10;
return length;
num = 175;
rem = sum = 0;
len = calculateLength(num);
#Makes a copy of the original number num
n = num;
#Calculates the sum of digits powered with their respective position
while(num > 0):
rem = num%10;
sum = sum + int(rem**len);
num = num//10;
len = len - 1;
#Checks whether the sum is equal to the number itself
if(sum == n):
print(str(n) + " is a disarium number");
else:
print(str(n) + " is not a disarium number");
Output: 175 is a disarium number C
#include <stdio.h>
//calculateLength() will count the digits present in a number
int calculateLength(int n){
int length = 0;
while(n != 0){
length = length + 1;
n = n/10;
}
return length;
}
int main()
{
int num = 175, sum = 0, rem = 0, n;
int len = calculateLength(num);
//Makes a copy of the original number num
n = num;
//Calculates the sum of digits powered with their respective position
while(num > 0){
rem = num%10;
sum = sum + (int)pow(rem,len);
num = num/10;
len--;
}
//Checks whether the sum is equal to the number itself
if(sum == n)
printf("%d is a disarium number", n);
else
printf("%d is not a disarium number", n);
return 0;
}
Output: 175 is a disarium number JAVA
public class DisariumNumber
{
//calculateLength() will count the digits present in a number
public static int calculateLength(int n){
int length = 0;
while(n != 0){
length = length + 1;
n = n/10;
}
return length;
}
public static void main(String[] args) {
int num = 175, sum = 0, rem = 0, n;
int len = calculateLength(num);
//Makes a copy of the original number num
n = num;
//Calculates the sum of digits powered with their respective position
while(num > 0){
rem = num%10;
sum = sum + (int)Math.pow(rem,len);
num = num/10;
len--;
}
//Checks whether the sum is equal to the number itself
if(sum == n)
System.out.println(n + " is a disarium number");
else
System.out.println(n + " is not a disarium number");
}
}
Output: 175 is a disarium number C#
using System;
public class DisariumNumber
{
//calculateLength() will count the digits present in a number
public static int calculateLength(int n){
int length = 0;
while(n != 0){
length = length + 1;
n = n/10;
}
return length;
}
public static void Main()
{
int num = 175, sum = 0, rem = 0, n;
int len = calculateLength(num);
//Makes a copy of the original number num
n = num;
//Calculates the sum of digits powered with their respective position
while(num > 0){
rem = num%10;
sum = sum + (int)Math.Pow(rem,len);
num = num/10;
len--;
}
//Checks whether the sum is equal to the number itself
if(sum == n)
Console.WriteLine(n + " is a disarium number");
else
Console.WriteLine(n + " is not a disarium number");
}
}
Output: 175 is a disarium number PHP
<!DOCTYPE html>
<html>
<body>
<?php
//calculateLength() will count the digits present in a number
function calculateLength($n){
$length = 0;
while($n != 0){
$length = $length + 1;
$n = intval($n/10);
}
return $length;
}
$num = 175;
$rem = $sum = 0;
$len = calculateLength($num);
//Makes a copy of the original number num
$n = $num;
//Calculates the sum of digits powered with their respective position
while($num > 0){
$rem = $num%10;
$sum = $sum + pow($rem,$len);
$num = intval($num/10);
$len--;
}
//Checks whether sum is equal to the number itself
if($sum == $n)
print($n . " is a disarium number");
else
print($n . " is not a disarium number");
?>
</body>
</html>
Output: 175 is a disarium number
Next Topic#
|