RE: AP Computer Science - The Armstrong Numbers

You are viewing a single comment's thread from:

AP Computer Science - The Armstrong Numbers

in tutorials •  7 years ago 

I have just created a Java version:

import java.util.Vector;

public class ArmstrongNumbers {

    Vector<Integer> numbers = new Vector<Integer>();
    
    public ArmstrongNumbers(int from, int to) {
        for (int i=from; i<to; i++) {
            if (isArmstrongNumbers(i)) {
                numbers.add(i);
            }
        }
    }
    
    public static boolean isArmstrongNumbers(int number) {
        int len = (""+number).length();
        int workingNumber = number;
        int sum = 0;
        for (int i=0; i<len; i++) {
            sum += Math.pow(workingNumber%10, len);
            workingNumber /= 10;
        }
        return sum == number;
    }
    
    public String toString() {
        return numbers.toString();
    }
    
    public static void main(String[] args) {
        System.out.println(new ArmstrongNumbers(1,  10000000));
    }

}

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!