Armstrong Number in Java


Armstrong Number in Java:

Armstrong number is a number that is equal to the sum of cubes of its digits for example o, 1, 153, 370, 371, 407 etc.

 

Why 153 is an Armstrong number.

153 = (1*1*1)+(5*5*5)+(3*3*3)

where:  

(1*1*1)=1

(5*5*5)=125

(3*3*3)=27

So:  

1+125+27=153

 

Why 371 is an Armstrong number.

371 = (3*3*3)+(7*7*7)+(1*1*1)

where:  

(3*3*3)=27

(7*7*7)=343

(1*1*1)=1

So:

27+343+1=371

153, 371, 9474 and 54748 are few Armstrong numbers.

153 = 13 + 53 + 33
371 = 33 +73 +13
9474 = 94 + 44 +74 + 44
54748 = 55 + 45 + 75 + 45 + 85

Java Program to check whether a given number is an Armstrong number

 

import java.util.*;

import java.io.*;

class arm

{

public static void main(String arg[])

{

Scanner ob = new Scanner(System.in);

System.out.println(“Enter any number to check whether it is armstrong or not:”);

int n = ob.nextInt();

int r,sum=0,temp = n;

while(n>0)

{

r=n%10;

n=n/10;

sum=sum+(r*r*r);

}

if(sum==temp)

System.out.print(“Given number ” + temp +” is Armstrong”);

else

System.out.println(“Given number ” + temp +” is not Armstrong”);

}

}

 

output:

arm

Related Posts

Comments are closed.