Absolute difference between the count of set bits in number and its reverse

Here given code implementation process.

// C Program for
// Absolute difference between the count of set bits in number and its reverse
#include <stdio.h>

int abseValue(int num)
{
	if (num < 0)
	{
		return -num;
	}
	return num;
}
// Reverse the given number
int reverseNumber(int num)
{
	int result = 0;
	int temp = abseValue(num);
	while (temp > 0)
	{
		result = result *10 + (temp % 10);
		temp = temp / 10;
	}
	if (num < 0)
	{
		return -result;
	}
	return result;
}
// Returns the number of set bits
int setBit(int num)
{
	int count = 0;
	while (num != 0)
	{
		if ((num & 1) == 1)
		{
			count++;
		}
		num = num >> 1;
	}
	return count;
}
void absoluteDifference(int num)
{
	printf("\n Given Number : %d", num);
	// Calculate difference of set bits
	int diff = abseValue(setBit(num) - setBit(reverseNumber(num)));
	// Display calculated result
	printf("\n Result       : %d", diff);
}
int main(int argc, char
	const *argv[])
{
	// num     : 431  (110101111) set bit = 7
	// reverse : 134  (010000110) set bit = 3
	// ----------------------------------------
	//                            (7 - 3) = 4
	// Result : 4
	absoluteDifference(431);
	// num     : 123  (001111011) set bit = 6
	// reverse : 321  (101000001) set bit = 3
	// ----------------------------------------
	//                            (6 - 3) = 3
	// Result : 3
	absoluteDifference(123);
	return 0;
}

Output

 Given Number : 431
 Result       : 4
 Given Number : 123
 Result       : 3
/*
    Java Program
    Absolute difference between the count of set bits in number and its reverse
*/
public class Difference
{
	public int abseValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Reverse the given number
	public int reverseNumber(int num)
	{
		int result = 0;
		int temp = abseValue(num);
		while (temp > 0)
		{
			result = result * 10 + (temp % 10);
			temp = temp / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	// Returns the number of set bits
	public int setBit(int num)
	{
		int count = 0;
		while (num != 0)
		{
			if ((num & 1) == 1)
			{
				count++;
			}
			num = num >> 1;
		}
		return count;
	}
	public void absoluteDifference(int num)
	{
		System.out.print("\n Given Number : " + num);
		// Calculate difference of set bits
		int diff = abseValue(setBit(num) - setBit(reverseNumber(num)));
		// Display calculated result
		System.out.print("\n Result       : " + diff);
	}
	public static void main(String[] args)
	{
		Difference task = new Difference();
		// num     : 431  (110101111) set bit = 7
		// reverse : 134  (010000110) set bit = 3
		// ----------------------------------------
		//                            (7 - 3) = 4
		// Result : 4
		task.absoluteDifference(431);
		// num     : 123  (001111011) set bit = 6
		// reverse : 321  (101000001) set bit = 3
		// ----------------------------------------
		//                            (6 - 3) = 3
		// Result : 3
		task.absoluteDifference(123);
	}
}

Output

 Given Number : 431
 Result       : 4
 Given Number : 123
 Result       : 3
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program for
    Absolute difference between the count of set bits in 
    number and its reverse
*/
class Difference
{
	public: int abseValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Reverse the given number
	int reverseNumber(int num)
	{
		int result = 0;
		int temp = this->abseValue(num);
		while (temp > 0)
		{
			result = result *10 + (temp % 10);
			temp = temp / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	// Returns the number of set bits
	int setBit(int num)
	{
		int count = 0;
		while (num != 0)
		{
			if ((num &1) == 1)
			{
				count++;
			}
			num = num >> 1;
		}
		return count;
	}
	void absoluteDifference(int num)
	{
		cout << "\n Given Number : " << num;
		// Calculate difference of set bits
		int diff = this->abseValue(
          this->setBit(num) - this->setBit(
            this->reverseNumber(num)));
		// Display calculated result
		cout << "\n Result       : " << diff;
	}
};
int main()
{
	Difference *task = new Difference();
	// num     : 431  (110101111) set bit = 7
	// reverse : 134  (010000110) set bit = 3
	// ----------------------------------------
	//                            (7 - 3) = 4
	// Result : 4
	task->absoluteDifference(431);
	// num     : 123  (001111011) set bit = 6
	// reverse : 321  (101000001) set bit = 3
	// ----------------------------------------
	//                            (6 - 3) = 3
	// Result : 3
	task->absoluteDifference(123);
	return 0;
}

Output

 Given Number : 431
 Result       : 4
 Given Number : 123
 Result       : 3
// Include namespace system
using System;
/*
    Csharp Program for
    Absolute difference between the count of set bits in 
    number and its reverse
*/
public class Difference
{
	public int abseValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Reverse the given number
	public int reverseNumber(int num)
	{
		int result = 0;
		int temp = this.abseValue(num);
		while (temp > 0)
		{
			result = result * 10 + (temp % 10);
			temp = temp / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	// Returns the number of set bits
	public int setBit(int num)
	{
		int count = 0;
		while (num != 0)
		{
			if ((num & 1) == 1)
			{
				count++;
			}
			num = num >> 1;
		}
		return count;
	}
	public void absoluteDifference(int num)
	{
		Console.Write("\n Given Number : " + num);
		// Calculate difference of set bits
		int diff = this.abseValue(
          this.setBit(num) - this.setBit(this.reverseNumber(num)));
		// Display calculated result
		Console.Write("\n Result       : " + diff);
	}
	public static void Main(String[] args)
	{
		Difference task = new Difference();
		// num     : 431  (110101111) set bit = 7
		// reverse : 134  (010000110) set bit = 3
		// ----------------------------------------
		//                            (7 - 3) = 4
		// Result : 4
		task.absoluteDifference(431);
		// num     : 123  (001111011) set bit = 6
		// reverse : 321  (101000001) set bit = 3
		// ----------------------------------------
		//                            (6 - 3) = 3
		// Result : 3
		task.absoluteDifference(123);
	}
}

Output

 Given Number : 431
 Result       : 4
 Given Number : 123
 Result       : 3
package main
import "fmt"
/*
    Go Program for
    Absolute difference between the count of set bits in 
    number and its reverse
*/
type Difference struct {}
func getDifference() * Difference {
	var me *Difference = &Difference {}
	return me
}
func(this Difference) abseValue(num int) int {
	if num < 0 {
		return -num
	}
	return num
}
// Reverse the given number
func(this Difference) reverseNumber(num int) int {
	var result int = 0
	var temp int = this.abseValue(num)
	for (temp > 0) {
		result = result * 10 + (temp % 10)
		temp = temp / 10
	}
	if num < 0 {
		return -result
	}
	return result
}
// Returns the number of set bits
func(this Difference) setBit(num int) int {
	var count int = 0
	for (num != 0) {
		if (num & 1) == 1 {
			count++
		}
		num = num >> 1
	}
	return count
}
func(this Difference) absoluteDifference(num int) {
	fmt.Print("\n Given Number : ", num)
	// Calculate difference of set bits
	var diff int = this.abseValue(
		this.setBit(num) - this.setBit(this.reverseNumber(num)))
	// Display calculated result
	fmt.Print("\n Result       : ", diff)
}
func main() {
	var task * Difference = getDifference()
	// num     : 431  (110101111) set bit = 7
	// reverse : 134  (010000110) set bit = 3
	// ----------------------------------------
	//                            (7 - 3) = 4
	// Result : 4
	task.absoluteDifference(431)
	// num     : 123  (001111011) set bit = 6
	// reverse : 321  (101000001) set bit = 3
	// ----------------------------------------
	//                            (6 - 3) = 3
	// Result : 3
	task.absoluteDifference(123)
}

Output

 Given Number : 431
 Result       : 4
 Given Number : 123
 Result       : 3
<?php
/*
    Php Program for
    Absolute difference between the count of set bits in 
    number and its reverse
*/
class Difference
{
	public	function abseValue($num)
	{
		if ($num < 0)
		{
			return -$num;
		}
		return $num;
	}
	// Reverse the given number
	public	function reverseNumber($num)
	{
		$result = 0;
		$temp = $this->abseValue($num);
		while ($temp > 0)
		{
			$result = $result * 10 + ($temp % 10);
			$temp = (int)($temp / 10);
		}
		if ($num < 0)
		{
			return -$result;
		}
		return $result;
	}
	// Returns the number of set bits
	public	function setBit($num)
	{
		$count = 0;
		while ($num != 0)
		{
			if (($num & 1) == 1)
			{
				$count++;
			}
			$num = $num >> 1;
		}
		return $count;
	}
	public	function absoluteDifference($num)
	{
		echo("\n Given Number : ".$num);
		// Calculate difference of set bits
		$diff = $this->abseValue(
          $this->setBit($num) - 
          $this->setBit($this->reverseNumber($num)));
		// Display calculated result
		echo("\n Result       : ".$diff);
	}
}

function main()
{
	$task = new Difference();
	// num     : 431  (110101111) set bit = 7
	// reverse : 134  (010000110) set bit = 3
	// ----------------------------------------
	//                            (7 - 3) = 4
	// Result : 4
	$task->absoluteDifference(431);
	// num     : 123  (001111011) set bit = 6
	// reverse : 321  (101000001) set bit = 3
	// ----------------------------------------
	//                            (6 - 3) = 3
	// Result : 3
	$task->absoluteDifference(123);
}
main();

Output

 Given Number : 431
 Result       : 4
 Given Number : 123
 Result       : 3
/*
    Node JS Program for
    Absolute difference between the count of set bits in 
    number and its reverse
*/
class Difference
{
	abseValue(num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Reverse the given number
	reverseNumber(num)
	{
		var result = 0;
		var temp = this.abseValue(num);
		while (temp > 0)
		{
			result = result * 10 + (temp % 10);
			temp = parseInt(temp / 10);
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	// Returns the number of set bits
	setBit(num)
	{
		var count = 0;
		while (num != 0)
		{
			if ((num & 1) == 1)
			{
				count++;
			}
			num = num >> 1;
		}
		return count;
	}
	absoluteDifference(num)
	{
		process.stdout.write("\n Given Number : " + num);
		// Calculate difference of set bits
		var diff = this.abseValue(
          this.setBit(num) - 
          this.setBit(this.reverseNumber(num)));
		// Display calculated result
		process.stdout.write("\n Result       : " + diff);
	}
}

function main()
{
	var task = new Difference();
	// num     : 431  (110101111) set bit = 7
	// reverse : 134  (010000110) set bit = 3
	// ----------------------------------------
	//                            (7 - 3) = 4
	// Result : 4
	task.absoluteDifference(431);
	// num     : 123  (001111011) set bit = 6
	// reverse : 321  (101000001) set bit = 3
	// ----------------------------------------
	//                            (6 - 3) = 3
	// Result : 3
	task.absoluteDifference(123);
}
main();

Output

 Given Number : 431
 Result       : 4
 Given Number : 123
 Result       : 3
#    Python 3 Program for
#    Absolute difference between the count of set bits in 
#    number and its reverse
class Difference :
	def abseValue(self, num) :
		if (num < 0) :
			return -num
		
		return num
	
	#  Reverse the given number
	def reverseNumber(self, num) :
		result = 0
		temp = self.abseValue(num)
		while (temp > 0) :
			result = result * 10 + (temp % 10)
			temp = int(temp / 10)
		
		if (num < 0) :
			return -result
		
		return result
	
	#  Returns the number of set bits
	def setBit(self, num) :
		count = 0
		while (num != 0) :
			if ((num & 1) == 1) :
				count += 1
			
			num = num >> 1
		
		return count
	
	def absoluteDifference(self, num) :
		print("\n Given Number : ", num, end = "")
		#  Calculate difference of set bits
		diff = self.abseValue(
          self.setBit(num) - self.setBit(self.reverseNumber(num)))
		#  Display calculated result
		print("\n Result       : ", diff, end = "")
	

def main() :
	task = Difference()
	#  num     : 431  (110101111) set bit = 7
	#  reverse : 134  (010000110) set bit = 3
	#  ----------------------------------------
	#                             (7 - 3) = 4
	#  Result : 4
	task.absoluteDifference(431)
	#  num     : 123  (001111011) set bit = 6
	#  reverse : 321  (101000001) set bit = 3
	#  ----------------------------------------
	#                             (6 - 3) = 3
	#  Result : 3
	task.absoluteDifference(123)

if __name__ == "__main__": main()

Output

 Given Number :  431
 Result       :  4
 Given Number :  123
 Result       :  3
#    Ruby Program for
#    Absolute difference between the count of set bits in 
#    number and its reverse
class Difference 
	def abseValue(num) 
		if (num < 0) 
			return -num
		end

		return num
	end

	#  Reverse the given number
	def reverseNumber(num) 
		result = 0
		temp = self.abseValue(num)
		while (temp > 0) 
			result = result * 10 + (temp % 10)
			temp = temp / 10
		end

		if (num < 0) 
			return -result
		end

		return result
	end

	#  Returns the number of set bits
	def setBit(num) 
		count = 0
		while (num != 0) 
			if ((num & 1) == 1) 
				count += 1
			end

			num = num >> 1
		end

		return count
	end

	def absoluteDifference(num) 
		print("\n Given Number : ", num)
		#  Calculate difference of set bits
		diff = self.abseValue(
          self.setBit(num) - self.setBit(self.reverseNumber(num)))
		#  Display calculated result
		print("\n Result       : ", diff)
	end

end

def main() 
	task = Difference.new()
	#  num     : 431  (110101111) set bit = 7
	#  reverse : 134  (010000110) set bit = 3
	#  ----------------------------------------
	#                             (7 - 3) = 4
	#  Result : 4
	task.absoluteDifference(431)
	#  num     : 123  (001111011) set bit = 6
	#  reverse : 321  (101000001) set bit = 3
	#  ----------------------------------------
	#                             (6 - 3) = 3
	#  Result : 3
	task.absoluteDifference(123)
end

main()

Output

 Given Number : 431
 Result       : 4
 Given Number : 123
 Result       : 3
/*
    Scala Program for
    Absolute difference between the count of set bits in 
    number and its reverse
*/
class Difference()
{
	def abseValue(num: Int): Int = {
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Reverse the given number
	def reverseNumber(num: Int): Int = {
		var result: Int = 0;
		var temp: Int = abseValue(num);
		while (temp > 0)
		{
			result = result * 10 + (temp % 10);
			temp = temp / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	// Returns the number of set bits
	def setBit(n: Int): Int = {
		var count: Int = 0;
      	var num = n;
		while (num != 0)
		{
			if ((num & 1) == 1)
			{
				count += 1;
			}
			num = num >> 1;
		}
		return count;
	}
	def absoluteDifference(num: Int): Unit = {
		print("\n Given Number : " + num);
		// Calculate difference of set bits
		var diff: Int = abseValue(
      		setBit(num) - setBit(reverseNumber(num)));
		// Display calculated result
		print("\n Result       : " + diff);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Difference = new Difference();
		// num     : 431  (110101111) set bit = 7
		// reverse : 134  (010000110) set bit = 3
		// ----------------------------------------
		//                            (7 - 3) = 4
		// Result : 4
		task.absoluteDifference(431);
		// num     : 123  (001111011) set bit = 6
		// reverse : 321  (101000001) set bit = 3
		// ----------------------------------------
		//                            (6 - 3) = 3
		// Result : 3
		task.absoluteDifference(123);
	}
}

Output

 Given Number : 431
 Result       : 4
 Given Number : 123
 Result       : 3
/*
    Swift 4 Program for
    Absolute difference between the count of set bits in 
    number and its reverse
*/
class Difference
{
	func abseValue(_ num: Int) -> Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Reverse the given number
	func reverseNumber(_ num: Int) -> Int
	{
		var result: Int = 0;
		var temp: Int = self.abseValue(num);
		while (temp > 0)
		{
			result = result * 10 + (temp % 10);
			temp = temp / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	// Returns the number of set bits
	func setBit(_ n: Int) -> Int
	{
		var count: Int = 0;
      	var num: Int = n;
		while (num  != 0)
		{
			if ((num & 1) == 1)
			{
				count += 1;
			}
			num = num >> 1;
		}
		return count;
	}
	func absoluteDifference(_ num: Int)
	{
		print("\n Given Number : ", num, terminator: "");
		// Calculate difference of set bits
		let diff: Int = self.abseValue(
          self.setBit(num) - self.setBit(self.reverseNumber(num)));
		// Display calculated result
		print("\n Result       : ", diff, terminator: "");
	}
}
func main()
{
	let task: Difference = Difference();
	// num     : 431  (110101111) set bit = 7
	// reverse : 134  (010000110) set bit = 3
	// ----------------------------------------
	//                            (7 - 3) = 4
	// Result : 4
	task.absoluteDifference(431);
	// num     : 123  (001111011) set bit = 6
	// reverse : 321  (101000001) set bit = 3
	// ----------------------------------------
	//                            (6 - 3) = 3
	// Result : 3
	task.absoluteDifference(123);
}
main();

Output

 Given Number :  431
 Result       :  4
 Given Number :  123
 Result       :  3
/*
    Kotlin Program for
    Absolute difference between the count of set bits in 
    number and its reverse
*/
class Difference
{
	fun abseValue(num: Int): Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Reverse the given number
	fun reverseNumber(num: Int): Int
	{
		var result: Int = 0;
		var temp: Int = this.abseValue(num);
		while (temp > 0)
		{
			result = result * 10 + (temp % 10);
			temp = temp / 10;
		}
		if (num < 0)
		{
			return -result;
		}
		return result;
	}
	// Returns the number of set bits
	fun setBit(n: Int): Int
	{
		var count: Int = 0;
      	var num = n;
		while (num != 0)
		{
			if ((num and 1) == 1)
			{
				count += 1;
			}
			num = num shr 1;
		}
		return count;
	}
	fun absoluteDifference(num: Int): Unit
	{
		print("\n Given Number : " + num);
		// Calculate difference of set bits
		val diff: Int = this.abseValue(
          this.setBit(num) - this.setBit(this.reverseNumber(num)));
		// Display calculated result
		print("\n Result       : " + diff);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Difference = Difference();
	// num     : 431  (110101111) set bit = 7
	// reverse : 134  (010000110) set bit = 3
	// ----------------------------------------
	//                            (7 - 3) = 4
	// Result : 4
	task.absoluteDifference(431);
	// num     : 123  (001111011) set bit = 6
	// reverse : 321  (101000001) set bit = 3
	// ----------------------------------------
	//                            (6 - 3) = 3
	// Result : 3
	task.absoluteDifference(123);
}

Output

 Given Number : 431
 Result       : 4
 Given Number : 123
 Result       : 3


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