biohax2015
biohax2015

Reputation: 291

"Magic Number" exercise in java

So my assignment is to find all the "magic numbers" within a range of numbers (input by the user). A magic number is a number whose factors (except itself) sums up to that number. So 6 would be a magic number because it's factors besides itself are 1,2 and 3 which sum up to 6. I have stared at this code for some time now and cannot figure out for the life of me why it won't print out the magic number. Any help would be appreciated.

public class MagicNumber {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.print("What is the top of the range?");
    int range = IO.readInt();
    if (range <= 0 ) {
        IO.reportBadInput();
    }
    int sumOfFactors = 0;
    for (int i = 1 ; i <= range ; i++) {
        for (int m = 1 ; m < i; m++) {
            if (i % m == 0) {
                sumOfFactors = sumOfFactors + m;
            }
            if (sumOfFactors == i) {
                System.out.println(i);
                    }
            }
        }
    }
}

Upvotes: 2

Views: 5878

Answers (5)

Vaibhav Soni
Vaibhav Soni

Reputation: 1

public class Magic {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int b =778393;
        int e=0,c;

        while (b>=10) {
            while (b>0) {
                c=b%10;
                b=b/10;
                e=e+c;
            }
            b=e;
            e=0;
            System.out.println(b);
        }
        if (b==1) {
            System.out.println("It is a magic no."+b);
        } else {
            System.out.println("Not"+b);
        }
    }
}

Upvotes: 0

Hrishikesh
Hrishikesh

Reputation: 1

Just referring to this Magic number program http://getprogramcode.com/2013/11/java-program-to-check-for-a-magic-number/ and thought of writing this.

public class HelloWorld{

     public static void main(String []args){
        static int number=5432;
        while(number>9){
            int tot = calculateSum(number);
            System.out.println(tot);
        }
     }

     private static int calculateSum(int num){
         int sum = 0;
         while (num > 0) {
            int a = num % 10;
            sum = sum+ a;
            num = num / 10;
        }
         number = sum;
         return sum;
     }
}

Upvotes: 0

Ravindra Bagale
Ravindra Bagale

Reputation: 17655

try this:

 public class MagicNumber {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.print("What is the top of the range?");
        int range = IO.readInt();
        if (range <= 0 ) {
            IO.reportBadInput();
        }

        for (int i = 1 ; i <= range ; i++) {
   int sumOfFactors = 0;
            for (int m = 1 ; m < i; m++) {
                if (i % m == 0) {
                    sumOfFactors = sumOfFactors + m;
                }
    }
                if (sumOfFactors == i) {
                    System.out.println(i);
                        }

            }
        }
    }

Upvotes: 0

Yogendra Singh
Yogendra Singh

Reputation: 34367

I I think you need to initialize int sumOfFactors = 0; within first for loop and move the second if, out of second for loops since you need to compare the sum of total factors against the current number as below:

for (int i = 1 ; i <= range ; i++) {
    int sumOfFactors = 0;  //<--Moved inside
    for (int m = 1 ; m < i; m++) {
        if (i % m == 0) {
            sumOfFactors = sumOfFactors + m;
        }
     }
     if (sumOfFactors == i) {  // <-- moveed out of second loop
       System.out.println(i);
     }
}

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234807

You are testing whether sumOfFactors == i while you are still summing factors. You need to move that outside the m loop. Then you need to set sumOfFactors to 0 before starting the m loop each time through the i loop, not just once at the start of the looping.

Upvotes: 2

Related Questions