Java program for Armstrong number



Java program for Armstrong number. Armstrong numbers are the sum of their own digits to the power of the number of digits. This java application find whether the entered number by user is Armstrong number or not. Here is the program

Java program for Armstrong number

import java.util.*;

class Armstrong
{
    public static void main(String args[])
    { 
         int sum=0, b,a, i,n;
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter the number to find armstrong number");
         b = sc.nextInt();

         System.out.println("Your entered number is of how many digits");
         n = sc.nextInt();
         i = b;

         while(i!=0) 
         {
              a=i%10;
              i=i/10;
              sum = sum + power(a,n);
         }

         if(b==sum)
         {
               System.out.println(b +" is armstrong number"); 
         } 
         else 
         {
               System.out.println(b +" is not an armstrong number");
         }
    }

    static int power(int c, int d)
    {
         int pow=1;
         int i=1;
         while(i<=d) 
         {
              pow=pow*c;
              i++;
         }
         return pow;
     }
}

Output of Java program for Armstrong number:

Java program for Armstrong number

Java program for Armstrong number




"Please Do Like Facebook Page and follow us on Twitter so that you can actively participate and develop skills in programming. If you find above post interesting do share the webpage."
This entry was posted in java and tagged , . Bookmark the permalink.

2 Responses to Java program for Armstrong number

  1. Pooja.B.M. says:

    Here there is no need to create a class to find power………
    …………………..because it is found in “Math.pow(int n)”………..just check it once and clarify it.

    • admin says:

      Right pooja. Thanks for the suggestion, but i thought to provide a pure basic function, so that we can even look for how Math.pow function works.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>