# Find the sum of bit differences of numbers using recursion

Here given code implementation process.

``````// C Program
// Find the sum of bit differences of numbers using recursion
#include <stdio.h>

// Find the bits difference of given number from 0 to n
int findBitDifference(int n)
{
if (n == 0)
{
return 0;
}
return findBitDifference(n / 2) + n;
}
int main(int argc, char
const *argv[])
{
// Test Case
int n = 8;
int result = findBitDifference(n);
// Display given n
printf("\n Given N : %d", n);
// Display calculated result
printf("\n Result : %d", result);
n = 15;
result = findBitDifference(n);
// Display given n
printf("\n Given N : %d", n);
// Display calculated result
printf("\n Result : %d", result);
return 0;
}``````

#### Output

`````` Given N : 8
Result : 15
Given N : 15
Result : 26``````
``````/*
Java program
Find the sum of bit differences of numbers using recursion
*/
public class BitDifference
{
// Find the bits difference of given number from 0 to n
public int findBitDifference(int n)
{
if (n == 0)
{
return 0;
}
return findBitDifference(n / 2) + n;
}
public static void main(String[] args)
{
// Test Case
int n = 8;
// Display given n
System.out.print("\n Given N : " + n);
// Display calculated result
System.out.print("\n Result : " + result);
n = 15;
// Display given
System.out.print("\n Given N : " + n);
// Display calculated result
System.out.print("\n Result : " + result);
}
}``````

#### Output

`````` Given N : 8
Result : 15
Given N : 15
Result : 26``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program
Find the sum of bit differences of numbers using recursion
*/
class BitDifference
{
public:
// Find the bits difference of given number from 0 to n
int findBitDifference(int n)
{
if (n == 0)
{
return 0;
}
return this->findBitDifference(n / 2) + n;
}
};
int main()
{
// Test Case
int n = 8;
// Display given n
cout << "\n Given N : " << n;
// Display calculated result
cout << "\n Result : " << result;
n = 15;
// Display given
cout << "\n Given N : " << n;
// Display calculated result
cout << "\n Result : " << result;
return 0;
}``````

#### Output

`````` Given N : 8
Result : 15
Given N : 15
Result : 26``````
``````// Include namespace system
using System;
/*
C# program
Find the sum of bit differences of numbers using recursion
*/
public class BitDifference
{
// Find the bits difference of given number from 0 to n
public int findBitDifference(int n)
{
if (n == 0)
{
return 0;
}
return findBitDifference(n / 2) + n;
}
public static void Main(String[] args)
{
// Test Case
int n = 8;
// Display given n
Console.Write("\n Given N : " + n);
// Display calculated result
Console.Write("\n Result : " + result);
n = 15;
// Display given
Console.Write("\n Given N : " + n);
// Display calculated result
Console.Write("\n Result : " + result);
}
}``````

#### Output

`````` Given N : 8
Result : 15
Given N : 15
Result : 26``````
``````<?php
/*
Php program
Find the sum of bit differences of numbers using recursion
*/
class BitDifference
{
// Find the bits difference of given number from 0 to n
public	function findBitDifference(\$n)
{
if (\$n == 0)
{
return 0;
}
return \$this->findBitDifference(intval(\$n / 2)) + \$n;
}
}

function main()
{
// Test Case
\$n = 8;
// Display given n
echo "\n Given N : ". \$n;
// Display calculated result
echo "\n Result : ". \$result;
\$n = 15;
// Display given
echo "\n Given N : ". \$n;
// Display calculated result
echo "\n Result : ". \$result;
}
main();``````

#### Output

`````` Given N : 8
Result : 15
Given N : 15
Result : 26``````
``````/*
Node Js program
Find the sum of bit differences of numbers using recursion
*/
class BitDifference
{
// Find the bits difference of given number from 0 to n
findBitDifference(n)
{
if (n == 0)
{
return 0;
}
return this.findBitDifference(parseInt(n / 2)) + n;
}
}

function main()
{
// Test Case
var n = 8;
// Display given n
process.stdout.write("\n Given N : " + n);
// Display calculated result
process.stdout.write("\n Result : " + result);
n = 15;
// Display given
process.stdout.write("\n Given N : " + n);
// Display calculated result
process.stdout.write("\n Result : " + result);
}
main();``````

#### Output

`````` Given N : 8
Result : 15
Given N : 15
Result : 26``````
``````#   Python 3 program
#   Find the sum of bit differences of numbers using recursion

class BitDifference :
#  Find the bits difference of given number from 0 to n
def findBitDifference(self, n) :
if (n == 0) :
return 0

return self.findBitDifference(int(n / 2)) + n

def main() :
#  Test Case
n = 8
#  Display given n
print("\n Given N : ", n, end = "")
#  Display calculated result
print("\n Result : ", result, end = "")
n = 15
#  Display given
print("\n Given N : ", n, end = "")
#  Display calculated result
print("\n Result : ", result, end = "")

if __name__ == "__main__": main()``````

#### Output

`````` Given N :  8
Result :  15
Given N :  15
Result :  26``````
``````#   Ruby program
#   Find the sum of bit differences of numbers using recursion

class BitDifference
#  Find the bits difference of given number from 0 to n
def findBitDifference(n)
if (n == 0)
return 0
end

return self.findBitDifference(n / 2) + n
end

end

def main()
#  Test Case
n = 8
#  Display given n
print("\n Given N : ", n)
#  Display calculated result
print("\n Result : ", result)
n = 15
#  Display given
print("\n Given N : ", n)
#  Display calculated result
print("\n Result : ", result)
end

main()``````

#### Output

`````` Given N : 8
Result : 15
Given N : 15
Result : 26``````
``````/*
Scala program
Find the sum of bit differences of numbers using recursion
*/
class BitDifference
{
// Find the bits difference of given number from 0 to n
def findBitDifference(n: Int): Int = {
if (n == 0)
{
return 0;
}
return this.findBitDifference((n / 2).toInt) + n;
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BitDifference = new BitDifference();
// Test Case
var n: Int = 8;
// Display given n
print("\n Given N : " + n);
// Display calculated result
print("\n Result : " + result);
n = 15;
// Display given
print("\n Given N : " + n);
// Display calculated result
print("\n Result : " + result);
}
}``````

#### Output

`````` Given N : 8
Result : 15
Given N : 15
Result : 26``````
``````/*
Swift 4 program
Find the sum of bit differences of numbers using recursion
*/
class BitDifference
{
// Find the bits difference of given number from 0 to n
func findBitDifference(_ n: Int)->Int
{
if (n == 0)
{
return 0;
}
return self.findBitDifference(n / 2) + n;
}
}
func main()
{
// Test Case
var n: Int = 8;
// Display given n
print("\n Given N : ", n, terminator: "");
// Display calculated result
print("\n Result : ", result, terminator: "");
n = 15;
// Display given
print("\n Given N : ", n, terminator: "");
// Display calculated result
print("\n Result : ", result, terminator: "");
}
main();``````

#### Output

`````` Given N :  8
Result :  15
Given N :  15
Result :  26``````
``````/*
Kotlin program
Find the sum of bit differences of numbers using recursion
*/
class BitDifference
{
// Find the bits difference of given number from 0 to n
fun findBitDifference(n: Int): Int
{
if (n == 0)
{
return 0;
}
return this.findBitDifference(n / 2) + n;
}
}
fun main(args: Array < String > ): Unit
{
// Test Case
var n: Int = 8;
// Display given n
print("\n Given N : " + n);
// Display calculated result
print("\n Result : " + result);
n = 15;
// Display given
print("\n Given N : " + n);
// Display calculated result
print("\n Result : " + result);
}``````

#### Output

`````` Given N : 8
Result : 15
Given N : 15
Result : 26``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.