Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | Set | C ++ program for the above approach ; Function to count the occurrences of X in the generated square matrix ; Store the required result ; Iterate over the range [ 1 , N ] ; Check if x is a multiple of i or not ; Check if the other multiple exists in the range or not ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOccurrences ( int n , int x ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( x % i == 0 ) { if ( x / i <= n ) count ++ ; } } cout << count ; } int main ( ) { int N = 7 , X = 12 ; countOccurrences ( N , X ) ; return 0 ; }