In Java, Write a program (in array) to take a number as input then display whether the number is equilibrium number or not.
Hint: A number will be called as equilibrium if the sum of its factors is equal to sum of its digits.
package equilibrium;
public class Equilibrium {
static int sum_factors(int n){
int sum = 0;
for(int i=1; i<=n; i++){
if(n % i==0){
sum += i;
}
}
return sum;
}
static int sumDigits(int no)
{
return no == 0 ? 0 : no%10 +
sumDigits(no/10) ;
}
public static void main(String[] args) {
int n = 6;
if(sumDigits(n)==sum_factors(n)){
System.out.println("This is an equilibrium number");
}
else{
System.out.println("This is not an equilibrium number");
}
}
}
Comments
Leave a comment