Java program to find whether given no. is Armstrong or not

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*Write a program to find whether given no. is Armstrong or not.
  Example :
  Input - 153
  Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no. */
class Armstrong{
	public static void main(String args[]){
		int num = Integer.parseInt(args[0]);
		int n = num; //use to check at last time
		int check=0,remainder;
		while(num > 0){
			remainder = num % 10;
			check = check + (int)Math.pow(remainder,3);
			num = num / 10;
		}
		if(check == n)
			System.out.println(n+" is an Armstrong Number");
		else
			System.out.println(n+" is not a Armstrong Number");
	}
}

14 Responses to “Java program to find whether given no. is Armstrong or not”

  1. iarap sana

    write a java program to create an array of 10 cells, store the values in the area at run time. find the largest and smallest element.

    Reply
  2. googzy

    Java program to find whether given no. is Armstrong or not using (if-else statement
    )

    Reply
  3. Lalit Addiwar

    Can I get the same program without using any functions?
    I need simple program without using any function as I am learning java now.

    Reply
  4. class armstrong
    {public static void accept(int no)
    {int p=no,rem,s=0;
    do
    {rem=p%10;
    s=s*10+rem;
    p=p/10;
    }
    while(p!=0);
    if(s==no)
    System.out.println(“it is an armstrong”);
    else
    System.out.println(“it is not an armstrong”);
    }}

    what is the error in it could anyone please reply

    Reply
    • Suhail

      public class Ams {

      public static void accept(int no){

      int p=no,rem,s=0;
      do
      {
      rem=p%10;
      s=s*10+rem;
      p=p/10;
      }
      while(p!=0);
      if(s==no)
      System.out.println(“it is an ams”);
      else
      System.out.println(“not ams”);
      }
      }

      }

      Reply
  5. Sanjukta

    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();
    }
    }

    Reply
  6. import static com.sun.org.apache.xalan.internal.lib.ExsltMath.power;
    import java.util.Scanner;

    public class amstrongProg {
    public static void main(String[] args) {
    int c, sum=0, temp, r, d=0;

    Scanner ss = new Scanner(System.in);
    System.out.println(“Enter the value :”);
    c = ss.nextInt();
    temp = c;
    while(temp!=0){
    d++;

    temp = temp/10;
    }

    temp=c;
    while(temp !=0){
    r=temp%10;

    sum = sum +(int) Math.pow(r, d);

    temp=temp/10;

    }
    if (c==sum){
    System.out.println(“It is amstrong number”);
    }
    else{
    System.out.println(“It is not an amstrong number”);
    }
    }

    }

    Reply

Leave a Reply