Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | Java program for the above approach ; Function to count the occurrences of X in the generated square matrix ; Stores the required result ; Iterate upto square root of X ; Check if i divides X ; Store the quotient obtained on dividing X by i ; If both the numbers fall in the range , update count ; Return the result ; Driver code ; Given N and X ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int countOccurrences ( int N , int X ) { int count = 0 ; for ( int i = 1 ; i < Math . sqrt ( X ) ; i ++ ) { if ( X % i == 0 ) { int a = i ; int b = X / i ; if ( a <= N && b <= N ) { if ( a == b ) count += 1 ; else count += 2 ; } } } return count ; } public static void main ( String [ ] args ) { int N = 7 ; int X = 12 ; System . out . println ( countOccurrences ( N , X ) ) ; } }