## Wednesday, August 19, 2009

### Armstrong Number Java Program

Hello, welcome back to Java Code Online. Today I would be discussing an important mathematical Java Code. This Java Code is for Armstrong Number. The code asks the user to enter a number, and then checks that whether it is Armstrong Number or not.

An Armstrong Number is one in which the sum of the individual numbers raised to the power of the number of words in that number is equal to that particular number itself. For example 1 raised to the power 1 is equal to 1, so it is an Armstrong Number. Again 153, implies 1 raised to the power 3, 5 raised to the power 3, and then 3 raised to the power 3 are added, then it gives 153 again, so it is also an Armstrong Number.

The Java Code is given below:-

////////////////////////////////////

package developer;

import java.util.Scanner;

public class ArmstrongNumber {

static double finalValue = 0;

public static void main(String[] args) {

int valueTaker = 0;

System.out.println("Enter a number to be checked for Armstrong Number: ");

Scanner armstScan = new Scanner(System.in);

if(armstScan.hasNextInt())
{
valueTaker = armstScan.nextInt();
}

StringBuffer sb = new StringBuffer().append(valueTaker);

double lengthSBKeeper = sb.length();

for(int lengthKeeper = 0; lengthKeeper < sb.length(); lengthKeeper++)
{
double zxc = Integer.parseInt(sb.substring(lengthKeeper, lengthKeeper+1));

finalValue = Math.pow(zxc, lengthSBKeeper) + finalValue;

}

if(finalValue == valueTaker)
{
System.out.println("The entered number "+valueTaker+" is an Armstrong Number");
}
else
{
System.out.println("The entered number is not an Armstrong Number");
}
}
}

///////////////////////////////////////

I hope the code above was helpful to all of you. Just copy the code in your IDE, and change the package name accordingly, and you are set to roll. Keep buzzing Java Code Online for more Java information.

Other Java Programs that might be of some interest:-
Factorial Program in Java
Palindrome Program in Java
Prime Number Program in Java
Fibonacci Series Program in Java

#### 1 comment:

1. import java.util.Scanner;
class ams
{
public static void main(String Arg[])
{
int sum=0,no,a,b=0,c=0;
Scanner number=new Scanner(System.in);
System.out.printf("enter the number");
no=number.nextInt();
a=no%10;
b=(no/10)%10;
c=c/100;
sum=(a*a*a)+(b*b*b)+(c*c*c);

if(sum==no)
{

System.out.printf(" amstron %d\n",sum);
}
else
{
System.out.printf(" not amstron %d\n",sum);
}
}
}