How to find sum of Digits of Given Number and Number is Armstrong Number or Not

How to find sum of Digits of Given Number and Number is Armstrong Number or Not
SOURCE : SumOfDigits.java

public class SumOfDigits {

static int sum = 0;
static int sum1 = 0;

public static void main(String[] args) {

SumOfDigits sdm = new SumOfDigits();
int n = 371;
System.out.println("sum of digits :" + sdm.sumofdig(371));

sdm.armstrong(371);

if (sum1 == n) {
System.out.println("given number is armstrong ");
} else
System.out.println("given number is not armstrong");

}

public int armstrong(int numb) {

if (numb == 0) {
return sum1;
} else {

sum1 += ((numb % 10) * (numb % 10) * (numb % 10));

armstrong(numb / 10);

}

return sum1;
}

public int sumofdig(int num) {

if (num == 0) {

return sum;
} else {
sum += (num % 10);
sumofdig(num / 10);

}

return sum;
}

}


INPUT

371



OUTPUT

sum of digits :11
given number is armstrong







Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment