Armstrong Numbers

raj26061990 1 Tallied Votes 445 Views Share

Eg.407
4*4*4=64
0*0*0=0
7*7*7=343 now 64+0+343=407.
Any number which satisfies the above condition is called as Armstrong

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");    
       } 
}
Taywin 312 Posting Virtuoso

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.

javalover -4 Newbie Poster
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");
        }
    }
}
Taywin 312 Posting Virtuoso

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

Nutster 58 Newbie Poster

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

vinnitro 12 Newbie Poster

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");
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.