Q:

Java Program to Check whether Given Number is Armstrong or Not

belongs to collection: Java Basic Solved Programs

0

Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits is equal to the number itself.

For example:
Three Digits Armstrong number is 153, 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153

Four Digits Armstrong number is 1634, 1 ^ 4 + 6 ^ 4 + 3 ^ 4 + 4 ^ 4 + = 1634

and So on……..

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

SOURCE CODE ::

import java.util.Scanner;
import java.lang.Math;
public class Armstrong {

    public static void main(String[] args) {
        
        int a,b,n,arm=0,temp;
        System.out.print("Enter No. which u want to Check :");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        temp=n;
        a=countdigit(n);
        while(n>0)
        {
           b=n%10;
           n=n/10;
          
           arm = (int) (arm + Math.pow(b, a)) ;
           
        }
        if(arm==temp)
        {
            System.out.println("Armstrong No.");
        }
        else
        {
            System.out.println("Not Armstrong No.");
        }
    }
    
    static int countdigit(int n)
    {
      int c=0;
      
      while(n>0)
      {
          n=n/10;
          
          c++;
      }
      return c;
      
    }
    
} 
 
 

OUTPUT ::

 
Enter No. which u want to Check :153
Armstrong No.
-----------------------------------------------------------------------------------

Enter No. which u want to Check :222
Not Armstrong No.
-----------------------------------------------------------------------------------

Enter No. which u want to Check :1741725
Armstrong No.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java Program to find Largest among three N... >>
<< Write a Java Program to find GCD (HCF) of Two Numb...