Question: What is wrong with the following code, which attempts to return the number of factors of a given integer n ? Describe how to fix

What is wrong with the following code, which attempts to return the number of factors of a given integer n? Describe how to fix the code.
public int countFactors(int n)
{
for (int i =1; i <= n; i++)
{
if (n % i ==0)// check if i is a factor of n
{
return i;
}
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!