Number of factors of a number


Problem:
Find the number of factors of a given number.
eg. The number of factors of 10 are (2,5,10) = 3
The number of factors of 18 are (2,3,6,9,18) = 5

Solution:

//14. Number of Factors
	public int numberOfFactors(int N) {
		if(N==1){
			return 1;
		}

		int count = 0;
		for(int i=1; i<=(N/2); i++){
			if(N%i==0){
				count++;
			}
		}

	    return count+1;
	}

One thought on “Number of factors of a number

Comments are closed.