Number divisible by 8 using bitwise

Here given code implementation process.

/*
    C program for
    Number divisible by 8 using bitwise
*/
#include <stdio.h>

void divisibleBy8(int num)
{
	// Shift right by 3
	int auxiliary = num >> 3;
	// Shift left by 3
	auxiliary = auxiliary << 3;
	// Check if auxiliary is equal to number or not
	if (auxiliary == num)
	{
		printf("\n Number %d divisible by 8", num);
	}
	else
	{
		printf("\n Number %d is not divisible by 8", num);
	}
}
int main(int argc, char const *argv[])
{
	// Test Inputs
	divisibleBy8(56);
	divisibleBy8(31);
	divisibleBy8(32);
	divisibleBy8(72);
  	divisibleBy8(78);
	return 0;
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
/*
    Java program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
	public void divisibleBy8(int num)
	{
		// Shift right by 3
		int auxiliary = num >> 3;
		// Shift left by 3
		auxiliary = auxiliary << 3;
		// Check if auxiliary is equal to number or not
		if (auxiliary == num)
		{
			System.out.print("\n Number " + num + " divisible by 8");
		}
		else
		{
			System.out.print("\n Number " + num + " is not divisible by 8");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test Inputs
		task.divisibleBy8(56);
		task.divisibleBy8(31);
		task.divisibleBy8(32);
		task.divisibleBy8(72);
		task.divisibleBy8(78);
	}
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
	public: void divisibleBy8(int num)
	{
		// Shift right by 3
		int auxiliary = num >> 3;
		// Shift left by 3
		auxiliary = auxiliary << 3;
		// Check if auxiliary is equal to number or not
		if (auxiliary == num)
		{
			cout << "\n Number " << num << " divisible by 8";
		}
		else
		{
			cout << "\n Number " << num << " is not divisible by 8";
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test Inputs
	task->divisibleBy8(56);
	task->divisibleBy8(31);
	task->divisibleBy8(32);
	task->divisibleBy8(72);
	task->divisibleBy8(78);
	return 0;
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
// Include namespace system
using System;
/*
    Csharp program for
    Number divisible by 8 using bitwise
*/
public class Divisibility
{
	public void divisibleBy8(int num)
	{
		// Shift right by 3
		int auxiliary = num >> 3;
		// Shift left by 3
		auxiliary = auxiliary << 3;
		// Check if auxiliary is equal to number or not
		if (auxiliary == num)
		{
			Console.Write("\n Number " + num + " divisible by 8");
		}
		else
		{
			Console.Write("\n Number " + num + " is not divisible by 8");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test Inputs
		task.divisibleBy8(56);
		task.divisibleBy8(31);
		task.divisibleBy8(32);
		task.divisibleBy8(72);
		task.divisibleBy8(78);
	}
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
package main
import "fmt"
/*
    Go program for
    Number divisible by 8 using bitwise
*/

func divisibleBy8(num int) {
	// Shift right by 3
	var auxiliary int = num >> 3
	// Shift left by 3
	auxiliary = auxiliary << 3
	// Check if auxiliary is equal to number or not
	if auxiliary == num {
		fmt.Print("\n Number ", num, " divisible by 8")
	} else {
		fmt.Print("\n Number ", num, " is not divisible by 8")
	}
}
func main() {
	
	// Test Inputs
	divisibleBy8(56)
	divisibleBy8(31)
	divisibleBy8(32)
	divisibleBy8(72)
	divisibleBy8(78)
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
<?php
/*
    Php program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
	public	function divisibleBy8($num)
	{
		// Shift right by 3
		$auxiliary = $num >> 3;
		// Shift left by 3
		$auxiliary = $auxiliary << 3;
		// Check if auxiliary is equal to number or not
		if ($auxiliary == $num)
		{
			echo("\n Number ".$num.
				" divisible by 8");
		}
		else
		{
			echo("\n Number ".$num.
				" is not divisible by 8");
		}
	}
}

function main()
{
	$task = new Divisibility();
	// Test Inputs
	$task->divisibleBy8(56);
	$task->divisibleBy8(31);
	$task->divisibleBy8(32);
	$task->divisibleBy8(72);
	$task->divisibleBy8(78);
}
main();

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
/*
    Node JS program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
	divisibleBy8(num)
	{
		// Shift right by 3
		var auxiliary = num >> 3;
		// Shift left by 3
		auxiliary = auxiliary << 3;
		// Check if auxiliary is equal to number or not
		if (auxiliary == num)
		{
			process.stdout.write("\n Number " + num + " divisible by 8");
		}
		else
		{
			process.stdout.write("\n Number " + num + " is not divisible by 8");
		}
	}
}

function main()
{
	var task = new Divisibility();
	// Test Inputs
	task.divisibleBy8(56);
	task.divisibleBy8(31);
	task.divisibleBy8(32);
	task.divisibleBy8(72);
	task.divisibleBy8(78);
}
main();

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
#    Python 3 program for
#    Number divisible by 8 using bitwise
class Divisibility :
	def divisibleBy8(self, num) :
		#  Shift right by 3
		auxiliary = num >> 3
		#  Shift left by 3
		auxiliary = auxiliary << 3
		#  Check if auxiliary is equal to number or not
		if (auxiliary == num) :
			print("\n Number", num ,"divisible by 8", end = "")
		else :
			print("\n Number", num ,"is not divisible by 8", end = "")
		
	

def main() :
	task = Divisibility()
	#  Test Inputs
	task.divisibleBy8(56)
	task.divisibleBy8(31)
	task.divisibleBy8(32)
	task.divisibleBy8(72)
	task.divisibleBy8(78)

if __name__ == "__main__": main()

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
#    Ruby program for
#    Number divisible by 8 using bitwise
class Divisibility 
	def divisibleBy8(num) 
		#  Shift right by 3
		auxiliary = num >> 3
		#  Shift left by 3
		auxiliary = auxiliary << 3
		#  Check if auxiliary is equal to number or not
		if (auxiliary == num) 
			print("\n Number ", num ," divisible by 8")
		else
 
			print("\n Number ", num ," is not divisible by 8")
		end

	end

end

def main() 
	task = Divisibility.new()
	#  Test Inputs
	task.divisibleBy8(56)
	task.divisibleBy8(31)
	task.divisibleBy8(32)
	task.divisibleBy8(72)
	task.divisibleBy8(78)
end

main()

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
/*
    Scala program for
    Number divisible by 8 using bitwise
*/
class Divisibility()
{
	def divisibleBy8(num: Int): Unit = {
		// Shift right by 3
		var auxiliary: Int = num >> 3;
		// Shift left by 3
		auxiliary = auxiliary << 3;
		// Check if auxiliary is equal to number or not
		if (auxiliary == num)
		{
			print("\n Number " + num + " divisible by 8");
		}
		else
		{
			print("\n Number " + num + " is not divisible by 8");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test Inputs
		task.divisibleBy8(56);
		task.divisibleBy8(31);
		task.divisibleBy8(32);
		task.divisibleBy8(72);
		task.divisibleBy8(78);
	}
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8
/*
    Swift 4 program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
	func divisibleBy8(_ num: Int)
	{
		// Shift right by 3
		var auxiliary: Int = num >> 3;
		// Shift left by 3
		auxiliary = auxiliary << 3;
		// Check if auxiliary is equal to number or not
		if (auxiliary == num)
		{
			print("\n Number ", num ," divisible by 8", terminator: "");
		}
		else
		{
			print("\n Number ", num ," is not divisible by 8", terminator: "");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test Inputs
	task.divisibleBy8(56);
	task.divisibleBy8(31);
	task.divisibleBy8(32);
	task.divisibleBy8(72);
	task.divisibleBy8(78);
}
main();

Output

 Number  56  divisible by 8
 Number  31  is not divisible by 8
 Number  32  divisible by 8
 Number  72  divisible by 8
 Number  78  is not divisible by 8
/*
    Kotlin program for
    Number divisible by 8 using bitwise
*/
class Divisibility
{
	fun divisibleBy8(num: Int): Unit
	{
		// Shift right by 3
		var auxiliary: Int = num shr 3;
		// Shift left by 3
		auxiliary = auxiliary shl 3;
		// Check if auxiliary is equal to number or not
		if (auxiliary == num)
		{
			print("\n Number " + num + " divisible by 8");
		}
		else
		{
			print("\n Number " + num + " is not divisible by 8");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test Inputs
	task.divisibleBy8(56);
	task.divisibleBy8(31);
	task.divisibleBy8(32);
	task.divisibleBy8(72);
	task.divisibleBy8(78);
}

Output

 Number 56 divisible by 8
 Number 31 is not divisible by 8
 Number 32 divisible by 8
 Number 72 divisible by 8
 Number 78 is not divisible by 8


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