Search

Armstrong Number in Java || Java Programs

Armstrong Number in Java: 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.


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  

Program -


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


3 views0 comments

Related Posts

See All

How to Print Pattern in Java || Java Programs

Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program

How to Generate Random Number in Java || Java Programs

In real world projects, we often required to generate random numbers while we develop applications. Many applications have the feature to generate numbers randomly, such as to verify the user many app

Factorial Program in Java || Java Programs

Factorial Program in Java: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example: 4! = 4*3*2*1 = 24 5! = 5*4*3*2*1 = 120 Factorial Program us

  • Facebook
  • Instagram
  • LinkedIn
  • 1024px-Telegram_logo.svg
  • download
  • YouTube
  • Google Play