Count Set-bits of number using Recursion
Given a number N. The task is to find the number of set bits in its binary representation using recursion.
Examples:
Input : 21
Output : 3
21 represesnted as 10101 in binray representation
Input : 16
Output : 1
16 represesnted as 10000 in binray representation
Approach:
- First, check the LSB of the number.
- If the LSB is 1, then we add 1 to our answer and divide the number by 2.
- If the LSB is 0, we add 0 to our answer and divide the number by 2.
- 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 <bits/stdc++.h> 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 signifiant 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; } |
chevron_right
filter_none
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 signifiant 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 |
chevron_right
filter_none
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 signifiant bit is set if((n & 1) == 1): return 1 + CountSetBits(n >> 1); # If Least signifiant 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 |
chevron_right
filter_none
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 signifiant 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 |
chevron_right
filter_none
Output:
3
Recommended Posts:
- Sum of digit of a number using recursion
- Decimal to binary number using recursion
- Add the given digit to a number stored in a linked list using recursion
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count number of subsets having a particular XOR value
- Count unset bits of a number
- Count pairs with Bitwise-AND as even number
- Count total bits in a number
- Count pairs with Bitwise XOR as EVEN number
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count pairs with Bitwise AND as ODD number
- Count pairs with Bitwise XOR as ODD number
- Count pairs with Bitwise OR as Even number
- Count ways to express a number as sum of powers
- Count number of ways to arrange first N numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


