Nth term of Ruler Function Series | Java 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
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int setBits ( long n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static void findNthTerm ( int N ) { int x = setBits ( N ^ ( N - 1 ) ) ; System . out . println ( x ) ; } public static void main ( String [ ] args ) { int N = 8 ; findNthTerm ( N ) ; } }