Count total set bits in all numbers from 1 to n

Here given code implementation process.

// C Program for
// Count total set bits in all numbers from 1 to n
#include <stdio.h>

// Returns the number of active bits
int activeBit(int num)
{
	if (num <= 0)
	{
		return 0;
	}
	if (num % 2 == 0)
	{
		return activeBit(num / 2);
	}
	return activeBit(num / 2) + 1;
}
// Handles the request of counting the number of active bits from 1 to n
void countSetBits(int n)
{
	if (n < 0)
	{
		return;
	}
	int result = 0;
	// Execute loop from 1 to n
	for (int i = 1; i <= n; ++i)
	{
		result += activeBit(i);
	}
	// Display result
	printf("\n Active Bit from (1 to %d) \n", n);
	printf(" Output : %d\n", result);
}
int main()
{
	//Simple test
	countSetBits(7);
	countSetBits(10);
	return 0;
}

Output

 Active Bit from (1 to 7)
 Output : 12

 Active Bit from (1 to 10)
 Output : 17
/*
   Java Program for
   Count total set bits in all numbers from 1 to n
*/
class CountBits
{
	// Returns the number of active bits
	public int activeBit(int num)
	{
		if (num <= 0)
		{
			return 0;
		}
		if (num % 2 == 0)
		{
			return activeBit(num / 2);
		}
		return activeBit(num / 2) + 1;
	}
	// Handles the request of counting the number of active bits from 1 to n
	public void countSetBits(int n)
	{
		if (n < 0)
		{
			return;
		}
		int result = 0;
		// Execute loop from 1 to n
		for (int i = 1; i <= n; ++i)
		{
			result += activeBit(i);
		}
		// Display result
		System.out.print("\n Active Bit from (1 to " + n + ") \n");
		System.out.print(" Output : " + result + "\n");
	}
	public static void main(String[] args)
	{
		CountBits task = new CountBits();
		//Simple test
		task.countSetBits(7);
		task.countSetBits(10);
	}
}

Output

 Active Bit from (1 to 7)
 Output : 12

 Active Bit from (1 to 10)
 Output : 17
// Include header file
#include <iostream>
using namespace std;

/*
   C++ Program for
   Count total set bits in all numbers from 1 to n
*/

class CountBits
{
	public:
		// Returns the number of active bits
		int activeBit(int num)
		{
			if (num <= 0)
			{
				return 0;
			}
			if (num % 2 == 0)
			{
				return this->activeBit(num / 2);
			}
			return this->activeBit(num / 2) + 1;
		}
	// Handles the request of counting the number of active bits from 1 to n
	void countSetBits(int n)
	{
		if (n < 0)
		{
			return;
		}
		int result = 0;
		// Execute loop from 1 to n
		for (int i = 1; i <= n; ++i)
		{
			result += this->activeBit(i);
		}
		// Display result
		cout << "\n Active Bit from (1 to " << n << ") \n";
		cout << " Output : " << result << "\n";
	}
};
int main()
{
	CountBits task = CountBits();
	//Simple test
	task.countSetBits(7);
	task.countSetBits(10);
	return 0;
}

Output

 Active Bit from (1 to 7)
 Output : 12

 Active Bit from (1 to 10)
 Output : 17
// Include namespace system
using System;
/*
   C# Program for
   Count total set bits in all numbers from 1 to n
*/
public class CountBits
{
	// Returns the number of active bits
	public int activeBit(int num)
	{
		if (num <= 0)
		{
			return 0;
		}
		if (num % 2 == 0)
		{
			return activeBit(num / 2);
		}
		return activeBit(num / 2) + 1;
	}
	// Handles the request of counting the number of active bits from 1 to n
	public void countSetBits(int n)
	{
		if (n < 0)
		{
			return;
		}
		int result = 0;
		// Execute loop from 1 to n
		for (int i = 1; i <= n; ++i)
		{
			result += activeBit(i);
		}
		// Display result
		Console.Write("\n Active Bit from (1 to " + n + ") \n");
		Console.Write(" Output : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		CountBits task = new CountBits();
		//Simple test
		task.countSetBits(7);
		task.countSetBits(10);
	}
}

Output

 Active Bit from (1 to 7)
 Output : 12

 Active Bit from (1 to 10)
 Output : 17
<?php
/*
   Php Program for
   Count total set bits in all numbers from 1 to n
*/
class CountBits
{
	// Returns the number of active bits
	public	function activeBit($num)
	{
		if ($num <= 0)
		{
			return 0;
		}
		if ($num % 2 == 0)
		{
			return $this->activeBit(intval($num / 2));
		}
		return $this->activeBit(intval($num / 2)) + 1;
	}
	// Handles the request of counting the number of active bits from 1 to n
	public	function countSetBits($n)
	{
		if ($n < 0)
		{
			return;
		}
		$result = 0;
		// Execute loop from 1 to n
		for ($i = 1; $i <= $n; ++$i)
		{
			$result += $this->activeBit($i);
		}
		// Display result
		echo "\n Active Bit from (1 to ". $n .") \n";
		echo " Output : ". $result ."\n";
	}
}

function main()
{
	$task = new CountBits();
	//Simple test
	$task->countSetBits(7);
	$task->countSetBits(10);
}
main();

Output

 Active Bit from (1 to 7)
 Output : 12

 Active Bit from (1 to 10)
 Output : 17
/*
   Node Js Program for
   Count total set bits in all numbers from 1 to n
*/
class CountBits
{
	// Returns the number of active bits
	activeBit(num)
	{
		if (num <= 0)
		{
			return 0;
		}
		if (num % 2 == 0)
		{
			return this.activeBit(parseInt(num / 2));
		}
		return this.activeBit(parseInt(num / 2)) + 1;
	}
	// Handles the request of counting the number of active bits from 1 to n
	countSetBits(n)
	{
		if (n < 0)
		{
			return;
		}
		var result = 0;
		// Execute loop from 1 to n
		for (var i = 1; i <= n; ++i)
		{
			result += this.activeBit(i);
		}
		// Display result
		process.stdout.write("\n Active Bit from (1 to " + n + ") \n");
		process.stdout.write(" Output : " + result + "\n");
	}
}

function main()
{
	var task = new CountBits();
	//Simple test
	task.countSetBits(7);
	task.countSetBits(10);
}
main();

Output

 Active Bit from (1 to 7)
 Output : 12

 Active Bit from (1 to 10)
 Output : 17
#    Python 3 Program for
#    Count total set bits in all numbers from 1 to n

class CountBits :
	#  Returns the number of active bits
	def activeBit(self, num) :
		if (num <= 0) :
			return 0
		
		if (num % 2 == 0) :
			return self.activeBit(int(num / 2))
		
		return self.activeBit(int(num / 2)) + 1
	
	#  Handles the request of counting the number of active bits from 1 to n
	def countSetBits(self, n) :
		if (n < 0) :
			return
		
		result = 0
		#  Execute loop from 1 to n
		i = 1
		while (i <= n) :
			result += self.activeBit(i)
			i += 1
		
		#  Display result
		print("\n Active Bit from (1 to ", n ,") ")
		print(" Output : ", result )
	

def main() :
	task = CountBits()
	# Simple test
	task.countSetBits(7)
	task.countSetBits(10)

if __name__ == "__main__": main()

Output

 Active Bit from (1 to  7 )
 Output :  12

 Active Bit from (1 to  10 )
 Output :  17
#    Ruby Program for
#    Count total set bits in all numbers from 1 to n

class CountBits 
	#  Returns the number of active bits
	def activeBit(num) 
		if (num <= 0) 
			return 0
		end

		if (num % 2 == 0) 
			return self.activeBit(num / 2)
		end

		return self.activeBit(num / 2) + 1
	end

	#  Handles the request of counting the number of active bits from 1 to n
	def countSetBits(n) 
		if (n < 0) 
			return
		end

		result = 0
		#  Execute loop from 1 to n
		i = 1
		while (i <= n) 
			result += self.activeBit(i)
			i += 1
		end

		#  Display result
		print("\n Active Bit from (1 to ", n ,") \n")
		print(" Output : ", result ,"\n")
	end

end

def main() 
	task = CountBits.new()
	# Simple test
	task.countSetBits(7)
	task.countSetBits(10)
end

main()

Output

 Active Bit from (1 to 7) 
 Output : 12

 Active Bit from (1 to 10) 
 Output : 17
/*
   Scala Program for
   Count total set bits in all numbers from 1 to n
*/
class CountBits
{
	// Returns the number of active bits
	def activeBit(num: Int): Int = {
		if (num <= 0)
		{
			return 0;
		}
		if (num % 2 == 0)
		{
			return this.activeBit((num / 2).toInt);
		}
		return this.activeBit((num / 2).toInt) + 1;
	}
	// Handles the request of counting the number of active bits from 1 to n
	def countSetBits(n: Int): Unit = {
		if (n < 0)
		{
			return;
		}
		var result: Int = 0;
		// Execute loop from 1 to n
		var i: Int = 1;
		while (i <= n)
		{
			result += this.activeBit(i);
			i += 1;
		}
		// Display result
		print("\n Active Bit from (1 to " + n + ") \n");
		print(" Output : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CountBits = new CountBits();
		//Simple test
		task.countSetBits(7);
		task.countSetBits(10);
	}
}

Output

 Active Bit from (1 to 7)
 Output : 12

 Active Bit from (1 to 10)
 Output : 17
/*
   Swift 4 Program for
   Count total set bits in all numbers from 1 to n
*/
class CountBits
{
	// Returns the number of active bits
	func activeBit(_ num: Int)->Int
	{
		if (num <= 0)
		{
			return 0;
		}
		if (num % 2 == 0)
		{
			return self.activeBit(num / 2);
		}
		return self.activeBit(num / 2) + 1;
	}
	// Handles the request of counting the number of active bits from 1 to n
	func countSetBits(_ n: Int)
	{
		if (n < 0)
		{
			return;
		}
		var result: Int = 0;
		// Execute loop from 1 to n
		var i: Int = 1;
		while (i <= n)
		{
			result += self.activeBit(i);
			i += 1;
		}
		// Display result
		print("\n Active Bit from (1 to ", n ,") ");
		print(" Output : ", result );
	}
}
func main()
{
	let task: CountBits = CountBits();
	//Simple test
	task.countSetBits(7);
	task.countSetBits(10);
}
main();

Output

 Active Bit from (1 to  7 )
 Output :  12

 Active Bit from (1 to  10 )
 Output :  17
/*
   Kotlin Program for
   Count total set bits in all numbers from 1 to n
*/
class CountBits
{
	// Returns the number of active bits
	fun activeBit(num: Int): Int
	{
		if (num <= 0)
		{
			return 0;
		}
		if (num % 2 == 0)
		{
			return this.activeBit(num / 2);
		}
		return this.activeBit(num / 2) + 1;
	}
	// Handles the request of counting the number of active bits from 1 to n
	fun countSetBits(n: Int): Unit
	{
		if (n < 0)
		{
			return;
		}
		var result: Int = 0;
		// Execute loop from 1 to n
		var i: Int = 1;
		while (i <= n)
		{
			result += this.activeBit(i);
			i += 1;
		}
		// Display result
		print("\n Active Bit from (1 to " + n + ") \n");
		print(" Output : " + result + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: CountBits = CountBits();
	//Simple test
	task.countSetBits(7);
	task.countSetBits(10);
}

Output

 Active Bit from (1 to 7)
 Output : 12

 Active Bit from (1 to 10)
 Output : 17

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







© 2021, kalkicode.com, All rights reserved