Related Articles

# Count Set-bits of number using Recursion

• Last Updated : 08 Sep, 2021

Given a number N. The task is to find the number of set bits in its binary representation using recursion.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input : 21
Output :
21 represented as 10101 in binary representation

Input : 16
Output :
16 represented as 10000 in binary representation

Approach:

1. First, check the LSB of the number.
2. If the LSB is 1, then we add 1 to our answer and divide the number by 2.
3. If the LSB is 0, we add 0 to our answer and divide the number by 2.
4. Then we recursively follow step (1) until the number is greater than 0.

Below is the implementation of the above approach :

## C++

 `// CPP program to find number``// of set bist in a number``#include ``using` `namespace` `std;` `// Recursive function to find``// number of set bist in a number``int` `CountSetBits(``int` `n)``{``    ``// Base condition``    ``if` `(n == 0)``        ``return` `0;``        ` `    ``// If Least significant bit is set``    ``if``((n & 1) == 1)``        ``return` `1 + CountSetBits(n >> 1);``    ` `    ``// If Least significant bit is not set``    ``else``        ``return` `CountSetBits(n >> 1);``}` `// Driver code``int` `main()``{``    ``int` `n = 21;``    ` `    ``// Function call``    ``cout << CountSetBits(n) << endl;` `    ``return` `0;``}`

## Java

 `// Java program to find number``// of set bist in a number``class` `GFG``{``    ``// Recursive function to find``    ``// number of set bist in a number``    ``static` `int` `CountSetBits(``int` `n)``    ``{``        ``// Base condition``        ``if` `(n == ``0``)``            ``return` `0``;``            ` `        ``// If Least significant bit is set``        ``if``((n & ``1``) == ``1``)``            ``return` `1` `+ CountSetBits(n >> ``1``);``        ` `        ``// If Least significant bit is not set``        ``else``            ``return` `CountSetBits(n >> ``1``);``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main (String [] args)``    ``{``        ``int` `n = ``21``;``        ` `        ``// Function call``        ``System.out.println(CountSetBits(n));``    ``}``}` `// This code is contributed by ihritik`

## Python3

 `# Python3 program to find number``# of set bist in a number` `# Recursive function to find``# number of set bist in a number``def` `CountSetBits(n):``    ` `    ``# Base condition``    ``if` `(n ``=``=` `0``):``        ``return` `0``;``        ` `    ``# If Least significant bit is set``    ``if``((n & ``1``) ``=``=` `1``):``        ``return` `1` `+` `CountSetBits(n >> ``1``);``    ` `    ``# If Least significant bit is not set``    ``else``:``        ``return` `CountSetBits(n >> ``1``);` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``n ``=` `21``;``    ` `    ``# Function call``    ``print``(CountSetBits(n));` `# This code is contributed by 29AjayKumar`

## C#

 `// C# program to find number``// of set bist in a number``using` `System;` `class` `GFG``{``    ``// Recursive function to find``    ``// number of set bist in a number``    ``static` `int` `CountSetBits(``int` `n)``    ``{``        ``// Base condition``        ``if` `(n == 0)``            ``return` `0;``            ` `        ``// If Least significant bit is set``        ``if``((n & 1) == 1)``            ``return` `1 + CountSetBits(n >> 1);``        ` `        ``// If Least significant bit is not set``        ``else``            ``return` `CountSetBits(n >> 1);``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main ()``    ``{``        ``int` `n = 21;``        ` `        ``// Function call``        ``Console.WriteLine(CountSetBits(n));``    ``}``}` `// This code is contributed by ihritik`

## Javascript

 ``
Output:
`3`

My Personal Notes arrow_drop_up