+ 2

why this program is showing error? armstrong number checking

class MyClass { public static void main(String[ ] args) { int n=145,r,x=0,result=0,temp; temp=n; while(n!=0) { r=n%10; x++; } while(n!=0) { r=n%10; result=result+Math.pow(r,x); n=n/10; } if(result==temp) System.out.println("amstrng"); else System.out.println("not amstrng"); } }

1st Jul 2019, 7:44 AM
paramita banik
1 Resposta
+ 4
You need to convert double Math.pow(r,x) to int, like this: result= result+ (int) Math. pow(r, x) Narrowing must be explicit. https://www.javatpoint.com/java-variables Or change type of result variable into double
1st Jul 2019, 7:52 AM
voja
voja - avatar