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");
	}
}
surajk

13 thoughts on “Java program to find whether given no. is Armstrong or not

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

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

  3. 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

  4. 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”);
    }
    }

    }

  5. 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 *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in