Check if number is multiple of 3

Here given code implementation process.

// C program
// Check if number is multiple of 3
#include <stdio.h>

//  Returns a absolute value
int absValue(int num)
{
	if (num < 0)
	{
		return -num;
	}
	return num;
}
// Check that given number is a Multiple of 3 or not
int isMultipleOfThree(int num)
{
	int n = absValue(num);
	if (n == 1)
	{
		// When n is one
		return 0;
	}
	else if (n == 0)
	{
		// When n is zero
		return 1;
	}
	else
	{
		// Use to count active even and odd position bits
		int activeEven = 0;
		int activeOdd = 0;
		// Execute loop until n is not zero
		while (n != 0)
		{
			if ((n & 1) == 1)
			{
				// When of least significant bit active
				activeOdd++;
			}
			if ((n & 2) == 2)
			{
				// When of second bits is active from left side
				activeEven++;
			}
			// shift bit left side by 2
			n = n >> 2;
		}
		// Recursively find active bits from of even and odd position
		return isMultipleOfThree(activeOdd - activeEven);
	}
}
void multipleOfThree(int num)
{
	printf("\n Given number : %d", num);
	if (isMultipleOfThree(num) == 1)
	{
		printf("\n Is multiple of 3\n");
	}
	else
	{
		printf("\n Is Not multiple of 3\n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test Case
	multipleOfThree(99);
	multipleOfThree(124);
	multipleOfThree(21);
	multipleOfThree(13445);
	return 0;
}

Output

 Given number : 99
 Is multiple of 3

 Given number : 124
 Is Not multiple of 3

 Given number : 21
 Is multiple of 3

 Given number : 13445
 Is Not multiple of 3
// Java program
// Check if number is multiple of 3
public class Multiple
{
	//  Returns a absolute value
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Check that given number is a Multiple of 3 or not
	public boolean isMultipleOfThree(int num)
	{
		int n = absValue(num);
		if (n == 1)
		{
			// When n is one
			return false;
		}
		else if (n == 0)
		{
			// When n is zero
			return true;
		}
		else
		{
			// Use to count active even and odd position bits
			int activeEven = 0;
			int activeOdd = 0;
			// Execute loop until n is not zero
			while (n != 0)
			{
				if ((n & 1) == 1)
				{
					// When of least significant bit active
					activeOdd++;
				}
				if ((n & 2) == 2)
				{
					// When of second bits is active from left side
					activeEven++;
				}
				// shift bit left side by 2
				n = n >> 2;
			}
			// Recursively find active bits from of even and odd position
			return isMultipleOfThree(activeOdd - activeEven);
		}
	}
	public void multipleOfThree(int num)
	{
		System.out.print("\n Given number : " + num);
		if (isMultipleOfThree(num))
		{
			System.out.print("\n Is multiple of 3\n");
		}
		else
		{
			System.out.print("\n Is Not multiple of 3\n");
		}
	}
	public static void main(String[] args)
	{
		Multiple task = new Multiple();
		// Test Case
		task.multipleOfThree(99);
		task.multipleOfThree(124);
		task.multipleOfThree(21);
		task.multipleOfThree(13445);
	}
}

Output

 Given number : 99
 Is multiple of 3

 Given number : 124
 Is Not multiple of 3

 Given number : 21
 Is multiple of 3

 Given number : 13445
 Is Not multiple of 3
// Include header file
#include <iostream>
using namespace std;

// C++ program
// Check if number is multiple of 3

class Multiple
{
	public:
		//  Returns a absolute value
		int absValue(int num)
		{
			if (num < 0)
			{
				return -num;
			}
			return num;
		}
	// Check that given number is a Multiple of 3 or not
	bool isMultipleOfThree(int num)
	{
		int n = this->absValue(num);
		if (n == 1)
		{
			// When n is one
			return false;
		}
		else if (n == 0)
		{
			// When n is zero
			return true;
		}
		else
		{
			// Use to count active even and odd position bits
			int activeEven = 0;
			int activeOdd = 0;
			// Execute loop until n is not zero
			while (n != 0)
			{
				if ((n &1) == 1)
				{
					// When of least significant bit active
					activeOdd++;
				}
				if ((n &2) == 2)
				{
					// When of second bits is active from left side
					activeEven++;
				}
				// shift bit left side by 2
				n = n >> 2;
			}
			// Recursively find active bits from of even and odd position
			return this->isMultipleOfThree(activeOdd - activeEven);
		}
	}
	void multipleOfThree(int num)
	{
		cout << "\n Given number : " << num;
		if (this->isMultipleOfThree(num))
		{
			cout << "\n Is multiple of 3\n";
		}
		else
		{
			cout << "\n Is Not multiple of 3\n";
		}
	}
};
int main()
{
	Multiple task = Multiple();
	// Test Case
	task.multipleOfThree(99);
	task.multipleOfThree(124);
	task.multipleOfThree(21);
	task.multipleOfThree(13445);
	return 0;
}

Output

 Given number : 99
 Is multiple of 3

 Given number : 124
 Is Not multiple of 3

 Given number : 21
 Is multiple of 3

 Given number : 13445
 Is Not multiple of 3
// Include namespace system
using System;
// C# program
// Check if number is multiple of 3
public class Multiple
{
	//  Returns a absolute value
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Check that given number is a Multiple of 3 or not
	public Boolean isMultipleOfThree(int num)
	{
		int n = absValue(num);
		if (n == 1)
		{
			// When n is one
			return false;
		}
		else if (n == 0)
		{
			// When n is zero
			return true;
		}
		else
		{
			// Use to count active even and odd position bits
			int activeEven = 0;
			int activeOdd = 0;
			// Execute loop until n is not zero
			while (n != 0)
			{
				if ((n & 1) == 1)
				{
					// When of least significant bit active
					activeOdd++;
				}
				if ((n & 2) == 2)
				{
					// When of second bits is active from left side
					activeEven++;
				}
				// shift bit left side by 2
				n = n >> 2;
			}
			// Recursively find active bits from of even and odd position
			return isMultipleOfThree(activeOdd - activeEven);
		}
	}
	public void multipleOfThree(int num)
	{
		Console.Write("\n Given number : " + num);
		if (isMultipleOfThree(num))
		{
			Console.Write("\n Is multiple of 3\n");
		}
		else
		{
			Console.Write("\n Is Not multiple of 3\n");
		}
	}
	public static void Main(String[] args)
	{
		Multiple task = new Multiple();
		// Test Case
		task.multipleOfThree(99);
		task.multipleOfThree(124);
		task.multipleOfThree(21);
		task.multipleOfThree(13445);
	}
}

Output

 Given number : 99
 Is multiple of 3

 Given number : 124
 Is Not multiple of 3

 Given number : 21
 Is multiple of 3

 Given number : 13445
 Is Not multiple of 3
<?php
// Php program
// Check if number is multiple of 3
class Multiple
{
	//  Returns a absolute value
	public	function absValue($num)
	{
		if ($num < 0)
		{
			return -$num;
		}
		return $num;
	}
	// Check that given number is a Multiple of 3 or not
	public	function isMultipleOfThree($num)
	{
		$n = $this->absValue($num);
		if ($n == 1)
		{
			// When n is one
			return false;
		}
		else if ($n == 0)
		{
			// When n is zero
			return true;
		}
		else
		{
			// Use to count active even and odd position bits
			$activeEven = 0;
			$activeOdd = 0;
			// Execute loop until n is not zero
			while ($n != 0)
			{
				if (($n & 1) == 1)
				{
					// When of least significant bit active
					$activeOdd++;
				}
				if (($n & 2) == 2)
				{
					// When of second bits is active from left side
					$activeEven++;
				}
				// shift bit left side by 2
				$n = $n >> 2;
			}
			// Recursively find active bits from of even and odd position
			return $this->isMultipleOfThree($activeOdd - $activeEven);
		}
	}
	public	function multipleOfThree($num)
	{
		echo "\n Given number : ". $num;
		if ($this->isMultipleOfThree($num))
		{
			echo "\n Is multiple of 3\n";
		}
		else
		{
			echo "\n Is Not multiple of 3\n";
		}
	}
}

function main()
{
	$task = new Multiple();
	$task->multipleOfThree(99);
	$task->multipleOfThree(124);
	$task->multipleOfThree(21);
	$task->multipleOfThree(13445);
}
main();

Output

 Given number : 99
 Is multiple of 3

 Given number : 124
 Is Not multiple of 3

 Given number : 21
 Is multiple of 3

 Given number : 13445
 Is Not multiple of 3
// Node Js program
// Check if number is multiple of 3
class Multiple
{
	//  Returns a absolute value
	absValue(num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Check that given number is a Multiple of 3 or not
	isMultipleOfThree(num)
	{
		var n = this.absValue(num);
		if (n == 1)
		{
			// When n is one
			return false;
		}
		else if (n == 0)
		{
			// When n is zero
			return true;
		}
		else
		{
			// Use to count active even and odd position bits
			var activeEven = 0;
			var activeOdd = 0;
			// Execute loop until n is not zero
			while (n != 0)
			{
				if ((n & 1) == 1)
				{
					// When of least significant bit active
					activeOdd++;
				}
				if ((n & 2) == 2)
				{
					// When of second bits is active from left side
					activeEven++;
				}
				// shift bit left side by 2
				n = n >> 2;
			}
			// Recursively find active bits from of even and odd position
			return this.isMultipleOfThree(activeOdd - activeEven);
		}
	}
	multipleOfThree(num)
	{
		process.stdout.write("\n Given number : " + num);
		if (this.isMultipleOfThree(num))
		{
			process.stdout.write("\n Is multiple of 3\n");
		}
		else
		{
			process.stdout.write("\n Is Not multiple of 3\n");
		}
	}
}

function main()
{
	var task = new Multiple();
	// Test Case
	task.multipleOfThree(99);
	task.multipleOfThree(124);
	task.multipleOfThree(21);
	task.multipleOfThree(13445);
}
main();

Output

 Given number : 99
 Is multiple of 3

 Given number : 124
 Is Not multiple of 3

 Given number : 21
 Is multiple of 3

 Given number : 13445
 Is Not multiple of 3
#  Python 3 program
#  Check if number is multiple of 3
class Multiple :
	#   Returns a absolute value
	def absValue(self, num) :
		if (num < 0) :
			return -num
		
		return num
	
	#  Check that given number is a Multiple of 3 or not
	def isMultipleOfThree(self, num) :
		n = self.absValue(num)
		if (n == 1) :
			#  When n is one
			return False
		
		elif(n == 0) :
			#  When n is zero
			return True
		else :
			#  Use to count active even and odd position bits
			activeEven = 0
			activeOdd = 0
			#  Execute loop until n is not zero
			while (n != 0) :
				if ((n & 1) == 1) :
					#  When of least significant bit active
					activeOdd += 1
				
				if ((n & 2) == 2) :
					#  When of second bits is active from left side
					activeEven += 1
				
				#  shift bit left side by 2
				n = n >> 2
			
			#  Recursively find active bits from of even and odd position
			return self.isMultipleOfThree(activeOdd - activeEven)
		
	
	def multipleOfThree(self, num) :
		print("\n Given number : ", num, end = "")
		if (self.isMultipleOfThree(num)) :
			print("\n Is multiple of 3")
		else :
			print("\n Is Not multiple of 3")
		
	

def main() :
	task = Multiple()
	#  Test Case
	task.multipleOfThree(99)
	task.multipleOfThree(124)
	task.multipleOfThree(21)
	task.multipleOfThree(13445)

if __name__ == "__main__": main()

Output

 Given number :  99
 Is multiple of 3

 Given number :  124
 Is Not multiple of 3

 Given number :  21
 Is multiple of 3

 Given number :  13445
 Is Not multiple of 3
#  Ruby program
#  Check if number is multiple of 3
class Multiple 
	#   Returns a absolute value
	def absValue(num) 
		if (num < 0) 
			return -num
		end

		return num
	end

	#  Check that given number is a Multiple of 3 or not
	def isMultipleOfThree(num) 
		n = self.absValue(num)
		if (n == 1) 
			#  When n is one
			return false
		elsif(n == 0) 
			#  When n is zero
			return true
		else 
			#  Use to count active even and odd position bits
			activeEven = 0
			activeOdd = 0
			#  Execute loop until n is not zero
			while (n != 0) 
				if ((n & 1) == 1) 
					#  When of least significant bit active
					activeOdd += 1
				end

				if ((n & 2) == 2) 
					#  When of second bits is active from left side
					activeEven += 1
				end

				#  shift bit left side by 2
				n = n >> 2
			end

			#  Recursively find active bits from of even and odd position
			return self.isMultipleOfThree(activeOdd - activeEven)
		end

	end

	def multipleOfThree(num) 
		print("\n Given number : ", num)
		if (self.isMultipleOfThree(num)) 
			print("\n Is multiple of 3\n")
		else 
			print("\n Is Not multiple of 3\n")
		end

	end

end

def main() 
	task = Multiple.new()
	#  Test Case
	task.multipleOfThree(99)
	task.multipleOfThree(124)
	task.multipleOfThree(21)
	task.multipleOfThree(13445)
end

main()

Output

 Given number : 99
 Is multiple of 3

 Given number : 124
 Is Not multiple of 3

 Given number : 21
 Is multiple of 3

 Given number : 13445
 Is Not multiple of 3
// Scala program
// Check if number is multiple of 3
class Multiple
{
	//  Returns a absolute value
	def absValue(num: Int): Int = {
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Check that given number is a Multiple of 3 or not
	def isMultipleOfThree(num: Int): Boolean = {
		var n: Int = this.absValue(num);
		if (n == 1)
		{
			// When n is one
			return false;
		}
		else if (n == 0)
		{
			// When n is zero
			return true;
		}
		else
		{
			// Use to count active even and odd position bits
			var activeEven: Int = 0;
			var activeOdd: Int = 0;
			// Execute loop until n is not zero
			while (n != 0)
			{
				if ((n & 1) == 1)
				{
					// When of least significant bit active
					activeOdd += 1;
				}
				if ((n & 2) == 2)
				{
					// When of second bits is active from left side
					activeEven += 1;
				}
				// shift bit left side by 2
				n = n >> 2;
			}
			// Recursively find active bits from of even and odd position
			return this.isMultipleOfThree(activeOdd - activeEven);
		}
	}
	def multipleOfThree(num: Int): Unit = {
		print("\n Given number : " + num);
		if (this.isMultipleOfThree(num))
		{
			print("\n Is multiple of 3\n");
		}
		else
		{
			print("\n Is Not multiple of 3\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Multiple = new Multiple();
		// Test Case
		task.multipleOfThree(99);
		task.multipleOfThree(124);
		task.multipleOfThree(21);
		task.multipleOfThree(13445);
	}
}

Output

 Given number : 99
 Is multiple of 3

 Given number : 124
 Is Not multiple of 3

 Given number : 21
 Is multiple of 3

 Given number : 13445
 Is Not multiple of 3
// Swift 4 program
// Check if number is multiple of 3
class Multiple
{
	//  Returns a absolute value
	func absValue(_ num: Int)->Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Check that given number is a Multiple of 3 or not
	func isMultipleOfThree(_ num: Int)->Bool
	{
		var n: Int = self.absValue(num);
		if (n == 1)
		{
			// When n is one
			return false;
		}
		else if (n == 0)
		{
			// When n is zero
			return true;
		}
		else
		{
			// Use to count active even and odd position bits
			var activeEven: Int = 0;
			var activeOdd: Int = 0;
			// Execute loop until n is not zero
			while (n  != 0)
			{
				if ((n & 1) == 1)
				{
					// When of least significant bit active
					activeOdd += 1;
				}
				if ((n & 2) == 2)
				{
					// When of second bits is active from left side
					activeEven += 1;
				}
				// shift bit left side by 2
				n = n >> 2;
			}
			// Recursively find active bits from of even and odd position
			return self.isMultipleOfThree(activeOdd - activeEven);
		}
	}
	func multipleOfThree(_ num: Int)
	{
		print("\n Given number : ", num, terminator: "");
		if (self.isMultipleOfThree(num))
		{
			print("\n Is multiple of 3");
		}
		else
		{
			print("\n Is Not multiple of 3");
		}
	}
}
func main()
{
	let task: Multiple = Multiple();
	// Test Case
	task.multipleOfThree(99);
	task.multipleOfThree(124);
	task.multipleOfThree(21);
	task.multipleOfThree(13445);
}
main();

Output

 Given number :  99
 Is multiple of 3

 Given number :  124
 Is Not multiple of 3

 Given number :  21
 Is multiple of 3

 Given number :  13445
 Is Not multiple of 3
// Kotlin program
// Check if number is multiple of 3
class Multiple
{
	//  Returns a absolute value
	fun absValue(num: Int): Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	// Check that given number is a Multiple of 3 or not
	fun isMultipleOfThree(num: Int): Boolean
	{
		var n: Int = this.absValue(num);
		if (n == 1)
		{
			// When n is one
			return false;
		}
		else if (n == 0)
		{
			// When n is zero
			return true;
		}
		else
		{
			// Use to count active even and odd position bits
			var activeEven: Int = 0;
			var activeOdd: Int = 0;
			// Execute loop until n is not zero
			while (n != 0)
			{
				if ((n and 1) == 1)
				{
					// When of least significant bit active
					activeOdd += 1;
				}
				if ((n and 2) == 2)
				{
					// When of second bits is active from left side
					activeEven += 1;
				}
				// shift bit left side by 2
				n = n shr 2;
			}
			
			// Recursively find active bits from of even and odd position
			return this.isMultipleOfThree(activeOdd - activeEven);
		}
	}
	fun multipleOfThree(num: Int): Unit
	{
		print("\n Given number : " + num);
		if (this.isMultipleOfThree(num))
		{
			print("\n Is multiple of 3\n");
		}
		else
		{
			print("\n Is Not multiple of 3\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Multiple = Multiple();
	// Test Case
	task.multipleOfThree(99);
	task.multipleOfThree(124);
	task.multipleOfThree(21);
	task.multipleOfThree(13445);
}

Output

 Given number : 99
 Is multiple of 3

 Given number : 124
 Is Not multiple of 3

 Given number : 21
 Is multiple of 3

 Given number : 13445
 Is Not multiple of 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