Number divisible by 9 using bitwise

Here given code implementation process.

/*
    C program for
    Number divisible by 9 using bitwise
*/
#include <stdio.h>
#include <stdbool.h>
bool divisibleBy9(int num)
{
	if (num == 9 || num == 0)
	{
		// Base case when number is 0 or 9
		return true;
	}
	if (num < 9)
	{
		return false;
	}
	// Check divisibility of 9 using recursion 
	return divisibleBy9((num >> 3) - ((num & 7)));
}
int main(int argc, char
	const *argv[])
{
	int num = 81;
	if (divisibleBy9(num))
	{
		printf("\n Number %d divisible by 9", num);
	}
	else
	{
		printf("\n Number %d is not divisible by 9", num);
	}
	num = 56;
	if (divisibleBy9(num))
	{
		printf("\n Number %d divisible by 9", num);
	}
	else
	{
		printf("\n Number %d is not divisible by 9", num);
	}
	return 0;
}

Output

 Number 81 divisible by 9
 Number 56 is not divisible by 9
/*
    Java program for
    Number divisible by 9 using bitwise
*/
class Divisibility
{
	public boolean divisibleBy9(int num)
	{
		if (num == 9 || num == 0)
		{
			// Base case when number is 0 or 9
			return true;
		}
		if (num < 9)
		{
			return false;
		}
		// Check divisibility of 9 using recursion 
		return divisibleBy9((num >> 3) - ((num & 7)));
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		int num = 81;
		if (task.divisibleBy9(num))
		{
			System.out.print("\n Number " + num + " divisible by 9");
		}
		else
		{
			System.out.print("\n Number " + num + " is not divisible by 9");
		}
		num = 56;
		if (task.divisibleBy9(num))
		{
			System.out.print("\n Number " + num + " divisible by 9");
		}
		else
		{
			System.out.print("\n Number " + num + " is not divisible by 9");
		}
	}
}

Output

 Number 81 divisible by 9
 Number 56 is not divisible by 9
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program for
    Number divisible by 9 using bitwise
*/
class Divisibility
{
	public: bool divisibleBy9(int num)
	{
		if (num == 9 || num == 0)
		{
			// Base case when number is 0 or 9
			return true;
		}
		if (num < 9)
		{
			return false;
		}
		// Check divisibility of 9 using recursion 
		return this->divisibleBy9((num >> 3) - ((num &7)));
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	int num = 81;
	if (task->divisibleBy9(num))
	{
		cout << "\n Number " << num << " divisible by 9";
	}
	else
	{
		cout << "\n Number " << num << " is not divisible by 9";
	}
	num = 56;
	if (task->divisibleBy9(num))
	{
		cout << "\n Number " << num << " divisible by 9";
	}
	else
	{
		cout << "\n Number " << num << " is not divisible by 9";
	}
	return 0;
}

Output

 Number 81 divisible by 9
 Number 56 is not divisible by 9
// Include namespace system
using System;
/*
    Csharp program for
    Number divisible by 9 using bitwise
*/
public class Divisibility
{
	public Boolean divisibleBy9(int num)
	{
		if (num == 9 || num == 0)
		{
			// Base case when number is 0 or 9
			return true;
		}
		if (num < 9)
		{
			return false;
		}
		// Check divisibility of 9 using recursion 
		return this.divisibleBy9((num >> 3) - ((num & 7)));
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		int num = 81;
		if (task.divisibleBy9(num))
		{
			Console.Write("\n Number " + num + " divisible by 9");
		}
		else
		{
			Console.Write("\n Number " + num + " is not divisible by 9");
		}
		num = 56;
		if (task.divisibleBy9(num))
		{
			Console.Write("\n Number " + num + " divisible by 9");
		}
		else
		{
			Console.Write("\n Number " + num + " is not divisible by 9");
		}
	}
}

Output

 Number 81 divisible by 9
 Number 56 is not divisible by 9
package main
import "fmt"
/*
    Go program for
    Number divisible by 9 using bitwise
*/

func divisibleBy9(num int) bool {
	if num == 9 || num == 0 {
		// Base case when number is 0 or 9
		return true
	}
	if num < 9 {
		return false
	}
	// Check divisibility of 9 using recursion 
	return  divisibleBy9((num >> 3) - ((num & 7)))
}
func main() {
	
	var num int = 81
	if divisibleBy9(num) {
		fmt.Print("\n Number ", num, " divisible by 9")
	} else {
		fmt.Print("\n Number ", num, " is not divisible by 9")
	}
	num = 56
	if divisibleBy9(num) {
		fmt.Print("\n Number ", num, " divisible by 9")
	} else {
		fmt.Print("\n Number ", num, " is not divisible by 9")
	}
}

Output

 Number 81 divisible by 9
 Number 56 is not divisible by 9
<?php
/*
    Php program for
    Number divisible by 9 using bitwise
*/
class Divisibility
{
	public	function divisibleBy9($num)
	{
		if ($num == 9 || $num == 0)
		{
			// Base case when number is 0 or 9
			return true;
		}
		if ($num < 9)
		{
			return false;
		}
		// Check divisibility of 9 using recursion 
		return $this->divisibleBy9(($num >> 3) - (($num & 7)));
	}
}

function main()
{
	$task = new Divisibility();
	$num = 81;
	if ($task->divisibleBy9($num))
	{
		echo("\n Number ".$num.
			" divisible by 9");
	}
	else
	{
		echo("\n Number ".$num.
			" is not divisible by 9");
	}
	$num = 56;
	if ($task->divisibleBy9($num))
	{
		echo("\n Number ".$num.
			" divisible by 9");
	}
	else
	{
		echo("\n Number ".$num.
			" is not divisible by 9");
	}
}
main();

Output

 Number 81 divisible by 9
 Number 56 is not divisible by 9
/*
    Node JS program for
    Number divisible by 9 using bitwise
*/
class Divisibility
{
	divisibleBy9(num)
	{
		if (num == 9 || num == 0)
		{
			// Base case when number is 0 or 9
			return true;
		}
		if (num < 9)
		{
			return false;
		}
		// Check divisibility of 9 using recursion 
		return this.divisibleBy9((num >> 3) - ((num & 7)));
	}
}

function main()
{
	var task = new Divisibility();
	var num = 81;
	if (task.divisibleBy9(num))
	{
		process.stdout.write("\n Number " + num + " divisible by 9");
	}
	else
	{
		process.stdout.write("\n Number " + num + " is not divisible by 9");
	}
	num = 56;
	if (task.divisibleBy9(num))
	{
		process.stdout.write("\n Number " + num + " divisible by 9");
	}
	else
	{
		process.stdout.write("\n Number " + num + " is not divisible by 9");
	}
}
main();

Output

 Number 81 divisible by 9
 Number 56 is not divisible by 9
#    Python 3 program for
#    Number divisible by 9 using bitwise
class Divisibility :
	def divisibleBy9(self, num) :
		if (num == 9 or num == 0) :
			#  Base case when number is 0 or 9
			return True
		
		if (num < 9) :
			return False
		
		#  Check divisibility of 9 using recursion 
		return self.divisibleBy9((num >> 3) - ((num & 7)))
	

def main() :
	task = Divisibility()
	num = 81
	if (task.divisibleBy9(num)) :
		print("\n Number", num ,"divisible by 9", end = "")
	else :
		print("\n Number", num ,"is not divisible by 9", end = "")
	
	num = 56
	if (task.divisibleBy9(num)) :
		print("\n Number ", num ," divisible by 9", end = "")
	else :
		print("\n Number ", num ," is not divisible by 9", end = "")
	

if __name__ == "__main__": main()

Output

 Number 81 divisible by 9
 Number  56  is not divisible by 9
#    Ruby program for
#    Number divisible by 9 using bitwise
class Divisibility 
	def divisibleBy9(num) 
		if (num == 9 || num == 0) 
			#  Base case when number is 0 or 9
			return true
		end
		if (num < 9) 
			return false
		end

		#  Check divisibility of 9 using recursion 
		return self.divisibleBy9((num >> 3) - ((num & 7)))
	end

end

def main() 
	task = Divisibility.new()
	num = 81
	if (task.divisibleBy9(num)) 
		print("\n Number ", num ," divisible by 9")
	else
 
		print("\n Number ", num ," is not divisible by 9")
	end

	num = 56
	if (task.divisibleBy9(num)) 
		print("\n Number ", num ," divisible by 9")
	else
 
		print("\n Number ", num ," is not divisible by 9")
	end

end

main()

Output

 Number 81 divisible by 9
 Number 56 is not divisible by 9
/*
    Scala program for
    Number divisible by 9 using bitwise
*/
class Divisibility()
{
	def divisibleBy9(num: Int): Boolean = {
		if (num == 9 || num == 0)
		{
			// Base case when number is 0 or 9
			return true;
		}
		if (num < 9)
		{
			return false;
		}
		// Check divisibility of 9 using recursion 
		return divisibleBy9((num >> 3) - ((num & 7)));
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		var num: Int = 81;
		if (task.divisibleBy9(num))
		{
			print("\n Number " + num + " divisible by 9");
		}
		else
		{
			print("\n Number " + num + " is not divisible by 9");
		}
		num = 56;
		if (task.divisibleBy9(num))
		{
			print("\n Number " + num + " divisible by 9");
		}
		else
		{
			print("\n Number " + num + " is not divisible by 9");
		}
	}
}

Output

 Number 81 divisible by 9
 Number 56 is not divisible by 9
/*
    Swift 4 program for
    Number divisible by 9 using bitwise
*/
class Divisibility
{
	func divisibleBy9(_ num: Int) -> Bool
	{
		if (num == 9 || num == 0)
		{
			// Base case when number is 0 or 9
			return true;
		}
		if (num < 9)
		{
			return false;
		}
		// Check divisibility of 9 using recursion 
		return self.divisibleBy9((num >> 3) - ((num & 7)));
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	var num: Int = 81;
	if (task.divisibleBy9(num))
	{
		print("\n Number", num ,"divisible by 9", terminator: "");
	}
	else
	{
		print("\n Number", num ,"is not divisible by 9", terminator: "");
	}
	num = 56;
	if (task.divisibleBy9(num))
	{
		print("\n Number", num ,"divisible by 9", terminator: "");
	}
	else
	{
		print("\n Number", num ,"is not divisible by 9", terminator: "");
	}
}
main();

Output

 Number 81 divisible by 9
 Number 56 is not divisible by 9
/*
    Kotlin program for
    Number divisible by 9 using bitwise
*/
class Divisibility
{
	fun divisibleBy9(num: Int): Boolean
	{
		if (num == 9 || num == 0)
		{
			// Base case when number is 0 or 9
			return true;
		}
		if (num < 9)
		{
			return false;
		}
		// Check divisibility of 9 using recursion 
		return this.divisibleBy9((num shr 3) - ((num and 7)));
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	var num: Int = 81;
	if (task.divisibleBy9(num))
	{
		print("\n Number " + num + " divisible by 9");
	}
	else
	{
		print("\n Number " + num + " is not divisible by 9");
	}
	num = 56;
	if (task.divisibleBy9(num))
	{
		print("\n Number " + num + " divisible by 9");
	}
	else
	{
		print("\n Number " + num + " is not divisible by 9");
	}
}

Output

 Number 81 divisible by 9
 Number 56 is not divisible by 9


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