class Armstrong
{
    public static void main(String[] args)
       {
          int num=Integer.parseInt(args[0]);
          int rem,sum=0,no;
          no=num;
          while(no!=0)
            {
               rem=no%10;
               sum+=rem*rem*rem;
               no/=10;      
            }
          if(sum==num)
              System.out.println(num+" is an Armstrong number");
          else
              System.out.println(num+" is not an Armstrong number");    
       } 
}
5
Contributors
5
Replies
13
Views
6 Years
Discussion Span
Last Post by vinnitro
0

The program is not useful at all. There are only 4 of 3-digit numbers that are Armstrong numbers -- 153, 370, 371, and 407. Besides, if a user does not pass an argument or passing in an argument which is not an integer, the program breaks.

0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int inputNo = Integer.parseInt(br.readLine()),num = inputNo;
        int sum = 0;
        do{
            int rem = num%10;
            sum += rem*rem*rem;
            num /= 10;
        }while(num != 0);
        if(sum == inputNo){
            System.out.println(inputNo + " is an Armstrong Number");
        }else{
            System.out.println(inputNo + " is not an Armstrong Number");
        }
    }
}
0

Again, it is still not robust. It seems that some people don't understand what Integer.parseInt() would throw when the argument string is not an integer...

PS: As I said earlier, the snippet is useless because there are only 4 3-digit Armstrong numbers. The reason there might be an assignment for this is solely for beginner to practice how to code. It is not for show or given...

0

What might be more useful is to make a function that generates all the Armstrong numbers in a given range.

0

This may also be helpful to print armstrong numbers from 1 to 1000

class ArmstrongNos
{
public static void main(String args[])
{
    int n, r, sum;
    System.out.println("Armstrong numbers from 1 to 1000 : ");
    for (n = 0; n < 1000; n++)
                {
                        int n1 = n;
        sum = 0;
                        while (n1 != 0)
                        {
        r = n1 % 10;
        sum += r * r * r;
        n1 /= 10;
        }
        if (sum == n)
            System.out.print(n + " ");
    }
    System.out.println();
    System.out.println("                 Made by Vinod Bhosale");
}
}
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.