You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Farrukh Ahmad edited this page Dec 15, 2015
·
1 revision
In this program If we break down a number which is more than 1 digit, should be equal to sum of itself based on number of times its asked to dobled or tripled and so on
for example, 123 of 2 times means to be 11 + 22 + 33 should be equal to 123. if equal we return
public class InvidualDigitsSum {
public static void main(String args[]){
int a = 123;
int n = 4;
System.out.println(isIndivudalSum(a, n));
}
static boolean isIndivudalSum(int a, int n){
if( a < 10) return true;
double persistInput = a;
int reminder = 0;
double sum = 0;
while ( a > 0){
reminder = a % 10;
a = a / 10;
sum = sum + getDigitSum(reminder, n);
}
if ( persistInput == sum) return true;
return false;
}
// Mathematical way of calculation of digits sum
static double getDigitSum(int digit, int n){
double sum =0;
for(int i=0; i<n; i++){
sum = sum + digit * Math.pow(10, i);
}
return sum;
}
// Here I concat all digit to a string and convert back to double
static double getConcatinatedDigits(int digit, int n)
{
String concatedDigit = "";
for(int i=0; i<n; i++){
concatedDigit = concatedDigit + digit;
}
return Double.parseDouble(concatedDigit);
}