Armstrong Program in Java
Advertisements
Armstrong number Program in Java
Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits is equal to the number itself. In java programming you can easily write Armstrong Program in Java just follow same concept of C programming, only need to use syntax of java programming language.
For example:
Three Digits Armstrong number is 153, 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
Four Digits Armstrong number is 1634, 1 ^ 4 + 6 ^ 4 + 3 ^ 4 + 4 ^ 4 + = 1634
Armstrong Program in Java
import java.util.Scanner; class Armstrong { public static void main(String[] args) { int arm=0,a,b,c,d,no; Scanner s=new Scanner(System.in); System.out.println("Enter any num :"); no=s.nextInt(); d=no; while(no>0) { a=no%10; no=no/10; arm=arm+a*a*a; } if(arm==d) { System.out.println("Armstrong :"); } else { System.out.println("not Armstrong"); } } }
Output
Enter any num : 153 Armstrong
Syntax to compile and run java program
Syntax
for compile -> c:/>javac Armstrong for run -> c:/>java Armstrong
Explanation of Code
- Scanner s=new Scanner(System.in): are used for receive input from keyboard.
- nextInt(): method are used for get integer type value from keyboard.
- System.out.println("....."): are used for display message on screen or console.
Google Advertisment