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)
{
BitDifference task = new BitDifference();
// Test Case
int n = 8;
int result = task.findBitDifference(n);
// Display given n
System.out.print("\n Given N : " + n);
// Display calculated result
System.out.print("\n Result : " + result);
n = 15;
result = task.findBitDifference(n);
// 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()
{
BitDifference task = BitDifference();
// Test Case
int n = 8;
int result = task.findBitDifference(n);
// Display given n
cout << "\n Given N : " << n;
// Display calculated result
cout << "\n Result : " << result;
n = 15;
result = task.findBitDifference(n);
// 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)
{
BitDifference task = new BitDifference();
// Test Case
int n = 8;
int result = task.findBitDifference(n);
// Display given n
Console.Write("\n Given N : " + n);
// Display calculated result
Console.Write("\n Result : " + result);
n = 15;
result = task.findBitDifference(n);
// 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()
{
$task = new BitDifference();
// Test Case
$n = 8;
$result = $task->findBitDifference($n);
// Display given n
echo "\n Given N : ". $n;
// Display calculated result
echo "\n Result : ". $result;
$n = 15;
$result = $task->findBitDifference($n);
// 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()
{
var task = new BitDifference();
// Test Case
var n = 8;
var result = task.findBitDifference(n);
// Display given n
process.stdout.write("\n Given N : " + n);
// Display calculated result
process.stdout.write("\n Result : " + result);
n = 15;
result = task.findBitDifference(n);
// 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() :
task = BitDifference()
# Test Case
n = 8
result = task.findBitDifference(n)
# Display given n
print("\n Given N : ", n, end = "")
# Display calculated result
print("\n Result : ", result, end = "")
n = 15
result = task.findBitDifference(n)
# 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()
task = BitDifference.new()
# Test Case
n = 8
result = task.findBitDifference(n)
# Display given n
print("\n Given N : ", n)
# Display calculated result
print("\n Result : ", result)
n = 15
result = task.findBitDifference(n)
# 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;
var result: Int = task.findBitDifference(n);
// Display given n
print("\n Given N : " + n);
// Display calculated result
print("\n Result : " + result);
n = 15;
result = task.findBitDifference(n);
// 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()
{
let task: BitDifference = BitDifference();
// Test Case
var n: Int = 8;
var result: Int = task.findBitDifference(n);
// Display given n
print("\n Given N : ", n, terminator: "");
// Display calculated result
print("\n Result : ", result, terminator: "");
n = 15;
result = task.findBitDifference(n);
// 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
{
var task: BitDifference = BitDifference();
// Test Case
var n: Int = 8;
var result: Int = task.findBitDifference(n);
// Display given n
print("\n Given N : " + n);
// Display calculated result
print("\n Result : " + result);
n = 15;
result = task.findBitDifference(n);
// 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
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.
New Comment