Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOccurrences ( int N , int X ) { int count = 0 ; for ( int i = 1 ; i < 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 ; } int main ( ) { int N = 7 ; int X = 12 ; cout << countOccurrences ( N , X ) ; return 0 ; }