Java program to check if a number is Armstrong : Find Armstrong Number In Java

Blogs ❯❯ Java

what is Armstrong Numbers ?

वो numbers जिसमे हर digit के cubes का total उस number के बराबर होता है , उन्हें Armstrong numbers कहते हैं।

Image could not load

Image on pixabay

Armstrong Numbers List

कुछ Armstrong Numbers इस प्रकार हैं -

1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834 ये सभी Armstrong numbers हैं।

Check if a number is Armstrong

सबसे पहले ऊपर दिए गए numbers को check कर लेते हैं कि ये number Armstrong हैं या नहीं। तो जैसा कि अभी आपने ऊपर पढ़ा कि अगर number , इसमें present हर digit के cubes के total के बराबर होना चाहिए।

11 = 1
21 = 2

इसी तरह से 153 , हालाँकि इसमें 3 digit हैं इसीलिए हर digit पर 3 की घात होगी।

33 = 27
73 = 343
00 = 0
Total : 27 + 343 +0 = 370

इस article में Java language में हम Armstrong number check करने के लिए एक program बनाएंगे।

Java program to check if a number is Armstrong

// import package. import java.util.*; public class ArmstrongNumber { public static boolean isArmstrong(int num) { int originalNum, remainder, n = 0, result = 0; originalNum = num; for (;originalNum != 0; originalNum /= 10, ++n); originalNum = num; for (;originalNum != 0; originalNum /= 10) { remainder = originalNum % 10; result += Math.pow(remainder, n); } if(result == num) return true; else return false; } public static void main(String[] args) { // get number from user number. Scanner sc = new Scanner(System.in); System.out.println("Enter a number: "); int num = sc.nextInt(); // now call function. boolean result = isArmstrong(num); if(result) System.out.println(num + " is an Armstrong number."); else System.out.println(num + " is not an Armstrong number."); } }

Output :

Enter a number: 370
370 is an Armstrong number.

Enter a number: 9
9 is an Armstrong number.

Enter a number: 10
10 is not an Armstrong number.
Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook