Are You Looking For JAVA/J2EE Online Training ?

Fill in your details in below form, we will get back to you

Powered by Blogger.

Hadoop Online Training With Placement in USA

www.itlearnmore.com/special-offer-java-5-for-1-package

Limited Offer


Be an expert at low cost

Core Java and Adv JAVA, Struts, HTML and SQL Server Video courses for $20..... Click here


Thursday, 30 April 2015

Armstrong Number in Java: Armstrong number is a number that is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

Let's try to understand 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  
Let's try to understand 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  
Let's see the java program to check Armstrong Number.

class ArmstrongExample{  
  public static void main(String[] args)  {  
    int c=0,a,temp;  
    int n=153;//It is the number to check armstrong  
    temp=n;  
    while(n>0)  
    {  
    a=n%10;  
    n=n/10;  
    c=c+(a*a*a);  
    }  
    if(temp==c)  
    System.out.println("armstrong number");   
    else  
        System.out.println("Not armstrong number");   
   }  
}  
Output:
armstrong number


Categories:


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque volutpat volutpat nibh nec posuere. Donec auctor arcut pretium consequat. Contact me 123@abc.com

0 comments:

Post a Comment