0

Help me with this thing.

Why does this if () condition is not working when we enter input as 371 ?? import java.util.Scanner; class armStrong{ public static void main(String []args){ Scanner sc=new Scanner(System.in); int num,arm=0,a; System.out.println("Enter the num which you want to check which is < 1000= "); num=sc.nextInt(); while(num!=0){ a=num%10; arm=arm+(a*a*a); num=num/10; } System.out.println(arm); if(arm==num){ System.out.println("The integer is an Armstrong number."); } else{ System.out.println("The integer is not an Armstrong number."); } } }

30th May 2021, 1:48 PM
Riya Patel
Riya Patel - avatar
5 Answers
+ 2
Because the value of num is lost in the while loop. Store the value of the num in a variable before the loop and check arm with that variable
30th May 2021, 1:54 PM
Atul [Inactive]
+ 1
Riya Patel just one question I want to ask you. Have you downvoted by answer?
30th May 2021, 1:59 PM
Atul [Inactive]
+ 1
No problem
30th May 2021, 3:00 PM
Atul [Inactive]
0
Thank you. It worked
30th May 2021, 1:56 PM
Riya Patel
Riya Patel - avatar
0
By mistake it happened. Sorry
30th May 2021, 2:18 PM
Riya Patel
Riya Patel - avatar