program to check Armstrong number in java

Learn Java Programming for Beginners

Armstrong number is a number which is equal to sum of digits raise to the power total number of digits in the number.
example :
2=2^1
153=1^3+5^3+3^3=(1+125+27=153)

Program to check whether the number is an Armstrong number in java

class armstrong
{
public static void main(String args[])throws IOException
{
int sum=0,ch,i,j,num,no;

DataInputStream in=new DataInputStream(System.in);

no= Integer.parseInt(in.readLine());
num=no;

do
{
sum=sum+(no%10)*(no%10)*(no%10);
no=no/10;
}
while(no!=0);

if(sum==num)
System.out.println("The number "+num+" is an Armstrong number ");
else
System.out.println("The number "+num+" is not an Armstrong number ");
}
}

output:

armstrong number in java

armstrong number in java

You may also like...

2 Responses

  1. Sanjukta says:

    Another way to check if a number is an Armstrong number by taking user input..

    package com.practice.java;

    import java.util.Scanner;

    class Armstrong{
    public static void main(String args[]){
    System.out.println("Enter a number");

    Scanner sc = new Scanner(System.in);
    String a = sc.nextLine();
    int size = a.length();

    System.out.println("Length of the input number is = " + size);

    int num = Integer.parseInt(a);
    int n = num;
    int check=0,remainder;

    while(num > 0){
    remainder = num % 10;
    check = check + (int)Math.pow(remainder,size);
    num = num / 10;
    }
    if(check == n)
    System.out.println(n+" is an Armstrong Number");
    else
    System.out.println(n+" is not an Armstrong Number");

    sc.close();
    }
    }

Leave a Reply

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