Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am creating a simple program that finds the factors of a number. I then add th

ID: 3528245 • Letter: I

Question

I am creating a simple program that finds the factors of a number. I then add the factors up to get a sum. I then need to get the average. I am supposed to us the div (integer instruction) to calculate the average to four decimal places. After I do the first division and use mfhi to move the the remainder to a temporary register $t9, I then branch to a loop if the remainder is not zero. In the loop I then divide $t9 to and transfer mfhi to $t9 again, but it does not seem to work. Is there any way around this? It appears that the HIGH register is not updating when I do the second division, or any subsequent division of the loop.

Explanation / Answer

Something like the following should work. You'd have to add error handling and all that, but this should get you started.

import java.util.Scanner;

public class FactorFinder
{
private static void main(String[] args)
{
kbd = new Scanner(System.in);
System.out.println("Enter a number:");
int theNum = kbd.nextInt();
boolean[] isFactor = new boolean[theNum]; //don't have to do this, but i figured i'd put it in

System.out.println(" The factors of " + theNum + " are:");
for(i=1; i <= theNum/2; i++)
if(theNum % i == 0)
{
isFactor[i-1] = true;
System.out.println(i);
}

}

}