-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmstsrongNumberExample.java
More file actions
49 lines (49 loc) · 1.42 KB
/
ArmstsrongNumberExample.java
File metadata and controls
49 lines (49 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
1. import java.util.Scanner;
2. import java.lang.Math;
3. public class ArmstsrongNumberExample
4. {
5. //function to check if the number is Armstrong or not
6. static boolean isArmstrong(int n)
7. {
8. int temp, digits=0, last=0, sum=0;
9. //assigning n into a temp variable
10. temp=n;
11. //loop execute until the condition becomes false
12. while(temp>0)
13. {
14. temp = temp/10;
15. digits++;
16. }
17. temp = n;
18. while(temp>0)
19. {
20. //determines the last digit from the number
21. last = temp % 10;
22. //calculates the power of a number up to digit times and add the resultant to the sum variable
23. sum += (Math.pow(last, digits));
24. //removes the last digit
25. temp = temp/10;
26. }
27. //compares the sum with n
28. if(n==sum)
29. //returns if sum and n are equal
30. return true;
31. //returns false if sum and n are not equal
32. else return false;
33. }
34. //driver code
35. public static void main(String args[])
36. {
37. int num;
38. Scanner sc= new Scanner(System.in);
39. System.out.print("Enter the limit: ");
40. //reads the limit from the user
41. num=sc.nextInt();
42. System.out.println("Armstrong Number up to "+ num + " are: ");
43. for(int i=0; i<=num; i++)
44. //function calling
45. if(isArmstrong(i))
46. //prints the armstrong numbers
47. System.out.print(i+ ", ");
48. }
49. }