Thursday, 24 May 2018

ARMSTRONG NUMBER PROGRAM IN JAVA

 ARMSTRONG NUMBER:

A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.153 = (1*1*1)+(5*5*5)+(3*3*3)  =153 The sum of square of given number is equal to that number then number is armstrong otherwise number is not armstrong.

  • LETS UNDERSTAND 153 IS ARMSTRONG NUMBER OR NOT 

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

(1*1*1)=1  

(5*5*5)=125  (

3*3*3)=27  

So:  1+125+27=153 (VALUE IS SAME THEN THE NUMBER IS ARMSTRONG

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 


PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT:


import java.util.*;


Armstrong{  


  public static void main(String[] args)  {  


    int c=0,a,temp;  


 System.out.println("enter the number to check number is armstrong");


int n=in.nextInt();//or int a=153;


    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;

enter the number to check number is armstrong 371

armstrong number


No comments:

Post a Comment