Nth term of Ruler Function Series | C ++ program for the above approach ; Function to count the number of set bits in the number N ; Store the number of setbits ; Update the value of n ; Update the count ; Return the total count ; Function to find the Nth term of the Ruler Function Series ; Store the result ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int setBits ( long n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } void findNthTerm ( int N ) { int x = setBits ( N ^ ( N - 1 ) ) ; cout << x ; } int main ( ) { int N = 8 ; findNthTerm ( N ) ; return 0 ; }