Program that randomly generates a number and checks whether it is Armstrong or not

Check number is armstrong or not

Program that randomly generates a number and checks whether it is Armstrong or not

In Armstrong Number, the sum of the power of individual digits is equal to the number itself. A positive integer is called an armstrong number of order n if-
abcd… = an + bn + cn + dn + …

We take an example and try to check whether a generated integer is an Armstrong number or not.

import java.io.*;
importjava.util.*;
class Armstrong
 {
  public static void main(String args[])
   {
    intnum,r,sum=0,temp;
    Random rd=new Random();
    num=10+rd.nextInt(89);
    System.out.println("Generated Number is: " +num);
    temp=num;
    while(num>0)
      {
        r=num%10;
        sum=sum+(r*r*r);
        num=num/10;
      }
 if(temp==sum)
  {
   System.out.println("Number is Armstrong.");
  }
 else
  {
   System.out.println("Number is not Armstrong");
  }
  }
}

Program Output:

Generated Number is: 39
Number is not Armstrong

I hope this post helps you to understand Armstrong Number and its implementation in Java programming language.

Keep coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *