Monday, 27 April 2020

Armstrong number


A number is said to be Armstrong if the sum of the cubes of its digits is equal to the original number.
Example : N = 153  ,   13 + 53 + 33 = 153    // 407 is an Armstrong no.


import java.util.*;

class Armstrong
{
    public static void main(String args[])
    {
        int  s = 0 ,   rem   ;
        Scanner sk = new Scanner(System.in) ;
        System.out.print("Enter ur number : ") ;
        int n = sk.nextInt() ;
       
        int temp = n ; 
           
        while(n!=0)
        {
            rem = n % 10 ;
            s = s + rem * rem * rem;
            n = n / 10 ;
        }        
        if(temp  ==  s)
        {
            System.out.println(" The given number is Armstrong ");    
        }       
         else
        {
            System.out.println(" The given number is Not Armstrong ");    
        }       
    }
}


==================================================================



An Armstrong Number, the sum of power of individual digits is equal to number itself.
 For example 0, 1, 153, 370, 371 and 407 etc are the Armstrong numbers. That is  :

153=1*1*1 + 5*5*5 + 3*3*3
or
1634 = 1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4

WAP to prove that the sum of cubes of the digits is equal to the given number.


class  Armstrong
{
    public static void main(String args[])
    {
       int num = 1634 ;
       int t1 = num , len=0 ;
      
       while(t1 != 0)
       {
            len = len + 1 ;
            t1 = t1 / 10 ;        
       }
      
       int t2=num , arm=0 ;
      
       while(t2 != 0)
       {
            int mult = 1 ;
            int rem = t2 % 10 ;
           
            for(int i=1 ; i<=len ; i++)
            {
                mult = mult * rem ;
            }
           
            arm = arm + mult ;
            t2 = t2/10 ;
       }
                      
        if(arm==num)
        {
            System.out.println(" The given number is Armstrong ");    
        }
       
         else
        {
            System.out.println(" The given number is NOT an Armstrong ");    
        }
       
    }
}

2 comments: