java - Issues while executing Armstrong number program -


i trying out code finds out whether number entered armstrong or not. here code:

import java.util.*;  public class arm {     int a, b, c;      void m1() {         scanner obj = new scanner(system.in);         system.out.println("enter number");         int number = obj.nextint();         number = (100 * a) + (10 * b) + (1 * c);         if ((a * * a) + (b * b * b) + (c * c * c) == number) {             system.out.println("number armstrong");         } else {             system.out.println("number not armstrong");         }     }      public static void main(string args[]) {         arm obj = new arm();         obj.m1();     } } 

here value of a,b , c comes out zero. not correct result. if enter number 345. a,b , c should 3, 4 , 5 respectively. please guide.

that not how calculate a, b, c.

to find a,b,c repeatedly divide 10 , remainder modulus.

int digit = 0; int sum = 0; while(num > 0) {  digit = num % 10;  sum += math.pow(digit, 3); num = num/10; } 

why use / , %

consider 345.

now last digit can done?

what modulus return? remainder, if perform %10 last digit.

345 % 10 = 5 

now want second last digit.

so divide number 10, quotient

345 / 10 = 34 

now again if can perform modulus 4 , on..

what 100 * + 10 * b + 1 * c do?

that used number if have individual digits.

suppose have 3, 4, 5 know 345 out of how it?

3 * 100 = 300 4 * 10 = 40 5 * 1 = 5 ----------- 300 + 40 + 5 = 345 

now complete whole program.

public boolean isamg(int num) {    int digit = 0;    int sum = 0;    int copynum = num; //used check @ last     while(num > 0)    {       digit = num % 10;       sum += math.pow(digit, 3);       num = num / 10;     }     return sum == copynum; } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -