Posted on by Kalkicode
Code Mathematics

Check that if large number is divisible by 12

To check if a large number is divisible by 12, you need to see if it is evenly divisible by 12 without leaving a remainder. In other words, if you divide the number by 12, the result should be a whole number.

There are different methods to check if a number is divisible by 12, but one common way is to check if it is divisible by both 3 and 4. To do this, you can add up the digits of the number and see if the sum is divisible by 3. If it is, then you can check if the last two digits of the number form a number that is divisible by 4. If both conditions are met, then the number is divisible by 12.

For example, let's say you want to check if the number 1,234,560 is divisible by 12.

Step 1: Add up the digits of the number:

1 + 2 + 3 + 4 + 5 + 6 + 0 = 21

Step 2: Check if the sum is divisible by 3:

21 is divisible by 3, since 21/3 = 7.

Step 3: Check if the last two digits of the number form a number that is divisible by 4:

The last two digits of the number are 60, which is divisible by 4, since 60/4 = 15.

Since both conditions are met, we can conclude that 1,234,560 is divisible by 12.

Code Solution

// Java program for
// Check that if large number is divisible by 12
public class Divisibility
{
	public void isDivisibleBy12(String num)
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 3
		// ➂  Last two digit is divisible by 4
		boolean result = false;
		int n = num.length();
		if (n == 1 && num.charAt(0) == '0')
		{
			// When given number is zero
			result = true;
		}
		if (n > 1)
		{
			if (num.charAt(n - 1) == '0' || 
                num.charAt(n - 1) == '2' || 
                num.charAt(n - 1) == '4' || 
                num.charAt(n - 1) == '6' || 
                num.charAt(n - 1) == '8')
			{
				// Number is divisible by 2
				// Condition 2
				// Check that if number is divisible by 3 or not
				int sum = 0;
				// Calculate sum of all digit
				for (int i = 0; i < n; ++i)
				{
					sum += (num.charAt(i) - 48);
				}
				// Condition 3
				int lastTwo = (num.charAt(n - 2) - 48) * 10 + 
                  (num.charAt(n - 1) - 48);
				if (lastTwo % 4 == 0 && sum % 3 == 0)
				{
					result = true;
				}
			}
		}
		if (result == true)
		{
			System.out.print(" Given number (" + 
                             num + ") is divisible by 12\n");
		}
		else
		{
			System.out.print(" Given number (" + 
                             num + ") is not divisible by 12\n");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy12("324");
		task.isDivisibleBy12("32473248726872398347612");
		task.isDivisibleBy12("234234242");
		task.isDivisibleBy12("34534567567686558438");
	}
}

Output

 Given number (324) is divisible by 12
 Given number (32473248726872398347612) is divisible by 12
 Given number (234234242) is not divisible by 12
 Given number (34534567567686558438) is not divisible by 12
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Check that if large number is divisible by 12
class Divisibility
{
	public: void isDivisibleBy12(string num)
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 3
		// ➂  Last two digit is divisible by 4
		bool result = false;
		int n = num.length();
		if (n == 1 && num[0] == '0')
		{
			// When given number is zero
			result = true;
		}
		if (n > 1)
		{
			if (num[n - 1] == '0' || 
                num[n - 1] == '2' || 
                num[n - 1] == '4' || 
                num[n - 1] == '6' || 
                num[n - 1] == '8')
			{
				// Number is divisible by 2
				// Condition 2
				// Check that if number is divisible by 3 or not
				int sum = 0;
				// Calculate sum of all digit
				for (int i = 0; i < n; ++i)
				{
					sum += (num[i] - 48);
				}
				// Condition 3
				int lastTwo = (num[n - 2] - 48) * 10 + (num[n - 1] - 48);
				if (lastTwo % 4 == 0 && sum % 3 == 0)
				{
					result = true;
				}
			}
		}
		if (result == true)
		{
			cout << " Given number (" << num << ") is divisible by 12\n";
		}
		else
		{
			cout << " Given number (" << num << ") is not divisible by 12\n";
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test
	task->isDivisibleBy12("324");
	task->isDivisibleBy12("32473248726872398347612");
	task->isDivisibleBy12("234234242");
	task->isDivisibleBy12("34534567567686558438");
	return 0;
}

Output

 Given number (324) is divisible by 12
 Given number (32473248726872398347612) is divisible by 12
 Given number (234234242) is not divisible by 12
 Given number (34534567567686558438) is not divisible by 12
// Include namespace system
using System;
// Csharp program for
// Check that if large number is divisible by 12
public class Divisibility
{
	public void isDivisibleBy12(String num)
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 3
		// ➂  Last two digit is divisible by 4
		Boolean result = false;
		int n = num.Length;
		if (n == 1 && num[0] == '0')
		{
			// When given number is zero
			result = true;
		}
		if (n > 1)
		{
			if (num[n - 1] == '0' || 
                num[n - 1] == '2' || 
                num[n - 1] == '4' || 
                num[n - 1] == '6' || 
                num[n - 1] == '8')
			{
				// Number is divisible by 2
				// Condition 2
				// Check that if number is divisible by 3 or not
				int sum = 0;
				// Calculate sum of all digit
				for (int i = 0; i < n; ++i)
				{
					sum += (num[i] - 48);
				}
				// Condition 3
				int lastTwo = (num[n - 2] - 48) * 10 + (num[n - 1] - 48);
				if (lastTwo % 4 == 0 && sum % 3 == 0)
				{
					result = true;
				}
			}
		}
		if (result == true)
		{
			Console.Write(" Given number (" + num + ") is divisible by 12\n");
		}
		else
		{
			Console.Write(" Given number (" + num + ") is not divisible by 12\n");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy12("324");
		task.isDivisibleBy12("32473248726872398347612");
		task.isDivisibleBy12("234234242");
		task.isDivisibleBy12("34534567567686558438");
	}
}

Output

 Given number (324) is divisible by 12
 Given number (32473248726872398347612) is divisible by 12
 Given number (234234242) is not divisible by 12
 Given number (34534567567686558438) is not divisible by 12
package main
import "fmt"
// Go program for
// Check that if large number is divisible by 12

func isDivisibleBy12(num string) {
	// Condition
	// ➀  Number should be divisible by 2
	// ➁  Number should be divisible by 3
	// ➂  Last two digit is divisible by 4
	var result bool = false
	var n int = len(num)
	if n == 1 && num[0] == '0' {
		// When given number is zero
		result = true
	}
	if n > 1 {
		if num[n - 1] == '0' || 
		   num[n - 1] == '2' || 
		   num[n - 1] == '4' || 
		   num[n - 1] == '6' || 
		   num[n - 1] == '8' {
			// Number is divisible by 2
			// Condition 2
			// Check that if number is divisible by 3 or not
			var sum int = 0
			// Calculate sum of all digit
			for i := 0 ; i < n ; i++ {
				sum += (int(num[i]) - 48)
			}
			// Condition 3
			var lastTwo int = (int(num[n - 2]) - 48) * 10 + (int(num[n - 1]) - 48)
			if lastTwo % 4 == 0 && sum % 3 == 0 {
				result = true
			}
		}
	}
	if result == true {
		fmt.Print(" Given number (", num, ") is divisible by 12\n")
	} else {
		fmt.Print(" Given number (", num, ") is not divisible by 12\n")
	}
}
func main() {

	// Test
	isDivisibleBy12("324")
	isDivisibleBy12("32473248726872398347612")
	isDivisibleBy12("234234242")
	isDivisibleBy12("34534567567686558438")
}

Output

 Given number (324) is divisible by 12
 Given number (32473248726872398347612) is divisible by 12
 Given number (234234242) is not divisible by 12
 Given number (34534567567686558438) is not divisible by 12
<?php
// Php program for
// Check that if large number is divisible by 12
class Divisibility
{
	public	function isDivisibleBy12($num)
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 3
		// ➂  Last two digit is divisible by 4
		$result = false;
		$n = strlen($num);
		if ($n == 1 && $num[0] == '0')
		{
			// When given number is zero
			$result = true;
		}
		if ($n > 1)
		{
			if ($num[$n - 1] == '0' || 
                $num[$n - 1] == '2' || 
                $num[$n - 1] == '4' || 
                $num[$n - 1] == '6' || 
                $num[$n - 1] == '8')
			{
				// Number is divisible by 2
				// Condition 2
				// Check that if number is divisible by 3 or not
				$sum = 0;
				// Calculate sum of all digit
				for ($i = 0; $i < $n; ++$i)
				{
					$sum += (ord($num[$i]) - 48);
				}
				// Condition 3
				$lastTwo = (ord($num[$n - 2]) - 48) * 10 + 
                  (ord($num[$n - 1]) - 48);
				if ($lastTwo % 4 == 0 && $sum % 3 == 0)
				{
					$result = true;
				}
			}
		}
		if ($result == true)
		{
			echo(" Given number (".$num.
				") is divisible by 12\n");
		}
		else
		{
			echo(" Given number (".$num.
				") is not divisible by 12\n");
		}
	}
}

function main()
{
	$task = new Divisibility();
	// Test
	$task->isDivisibleBy12("324");
	$task->isDivisibleBy12("32473248726872398347612");
	$task->isDivisibleBy12("234234242");
	$task->isDivisibleBy12("34534567567686558438");
}
main();

Output

 Given number (324) is divisible by 12
 Given number (32473248726872398347612) is divisible by 12
 Given number (234234242) is not divisible by 12
 Given number (34534567567686558438) is not divisible by 12
// Node JS program for
// Check that if large number is divisible by 12
class Divisibility
{
	isDivisibleBy12(num)
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 3
		// ➂  Last two digit is divisible by 4
		var result = false;
		var n = num.length;
		if (n == 1 && num.charAt(0) == '0')
		{
			// When given number is zero
			result = true;
		}
		if (n > 1)
		{
			if (num.charAt(n - 1) == '0' || 
                num.charAt(n - 1) == '2' || 
                num.charAt(n - 1) == '4' || 
                num.charAt(n - 1) == '6' || 
                num.charAt(n - 1) == '8')
			{
				// Number is divisible by 2
				// Condition 2
				// Check that if number is divisible by 3 or not
				var sum = 0;
				// Calculate sum of all digit
				for (var i = 0; i < n; ++i)
				{
					sum += (num.charAt(i).charCodeAt(0) - 48);
				}
				// Condition 3
				var lastTwo = (num.charCodeAt(n - 2) - 48) * 10 +
                    (num.charCodeAt(n - 1) - 48);
				if (lastTwo % 4 == 0 && sum % 3 == 0)
				{
					result = true;
				}
			}
		}
		if (result == true)
		{
			console.log(" Given number (" + num 
                                 + ") is divisible by 12");
		}
		else
		{
			console.log(" Given number (" + num 
                                 + ") is not divisible by 12");
		}
	}
}

function main()
{
	var task = new Divisibility();
	// Test
	task.isDivisibleBy12("324");
	task.isDivisibleBy12("32473248726872398347612");
	task.isDivisibleBy12("234234242");
	task.isDivisibleBy12("34534567567686558438");
}
main();

Output

 Given number (324) is divisible by 12
 Given number (32473248726872398347612) is divisible by 12
 Given number (234234242) is not divisible by 12
 Given number (34534567567686558438) is not divisible by 12
#  Python 3 program for
#  Check that if large number is divisible by 12
class Divisibility :
	def isDivisibleBy12(self, num) :
		#  Condition
		#  ➀  Number should be divisible by 2
		#  ➁  Number should be divisible by 3
		#  ➂  Last two digit is divisible by 4
		result = False
		n = len(num)
		if (n == 1 and num[0] == '0') :
			#  When given number is zero
			result = True
		
		if (n > 1) :
			if (num[n - 1] == '0'
				or num[n - 1] == '2'
				or num[n - 1] == '4'
				or num[n - 1] == '6'
				or num[n - 1] == '8') :
				#  Number is divisible by 2
				#  Condition 2
				#  Check that if number is divisible by 3 or not
				sum = 0
				i = 0
				#  Calculate sum of all digit
				while (i < n) :
					sum += (ord(num[i]) - 48)
					i += 1
				
				#  Condition 3
				lastTwo = (ord(num[n - 2]) - 48) * 10 + (ord(num[n - 1]) - 48)
				if (lastTwo % 4 == 0 and sum % 3 == 0) :
					result = True
				
			
		
		if (result == True) :
			print(" Given number (", num ,") is divisible by 12")
		else :
			print(" Given number (", num ,") is not divisible by 12")
		
	

def main() :
	task = Divisibility()
	#  Test
	task.isDivisibleBy12("324")
	task.isDivisibleBy12("32473248726872398347612")
	task.isDivisibleBy12("234234242")
	task.isDivisibleBy12("34534567567686558438")

if __name__ == "__main__": main()

Output

 Given number ( 324 ) is divisible by 12
 Given number ( 32473248726872398347612 ) is divisible by 12
 Given number ( 234234242 ) is not divisible by 12
 Given number ( 34534567567686558438 ) is not divisible by 12
#  Ruby program for
#  Check that if large number is divisible by 12
class Divisibility 
	def isDivisibleBy12(num) 
		#  Condition
		#  ➀  Number should be divisible by 2
		#  ➁  Number should be divisible by 3
		#  ➂  Last two digit is divisible by 4
		result = false
		n = num.length
		if (n == 1 && num[0] == '0') 
			#  When given number is zero
			result = true
		end

		if (n > 1) 
			if (num[n - 1] == '0' || 
                num[n - 1] == '2' || 
                num[n - 1] == '4' || 
                num[n - 1] == '6' || 
                num[n - 1] == '8') 
				#  Number is divisible by 2
				#  Condition 2
				#  Check that if number is divisible by 3 or not
				sum = 0
				i = 0
				#  Calculate sum of all digit
				while (i < n) 
					sum += (num[i].ord - 48)
					i += 1
				end

				#  Condition 3
				lastTwo = (num[n - 2].ord - 48) * 10 + 
                  (num[n - 1].ord - 48)
				if (lastTwo % 4 == 0 && sum % 3 == 0) 
					result = true
				end

			end

		end

		if (result == true) 
			print(" Given number (", num ,") is divisible by 12\n")
		else
 
			print(" Given number (", num ,") is not divisible by 12\n")
		end

	end

end

def main() 
	task = Divisibility.new()
	#  Test
	task.isDivisibleBy12("324")
	task.isDivisibleBy12("32473248726872398347612")
	task.isDivisibleBy12("234234242")
	task.isDivisibleBy12("34534567567686558438")
end

main()

Output

 Given number (324) is divisible by 12
 Given number (32473248726872398347612) is divisible by 12
 Given number (234234242) is not divisible by 12
 Given number (34534567567686558438) is not divisible by 12
import scala.collection.mutable._;
// Scala program for
// Check that if large number is divisible by 12
class Divisibility()
{
	def isDivisibleBy12(num: String): Unit = {
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 3
		// ➂  Last two digit is divisible by 4
		var result: Boolean = false;
		var n: Int = num.length();
		if (n == 1 && num.charAt(0) == '0')
		{
			// When given number is zero
			result = true;
		}
		if (n > 1)
		{
			if (num.charAt(n - 1) == '0' || 
                num.charAt(n - 1) == '2' || 
                num.charAt(n - 1) == '4' || 
                num.charAt(n - 1) == '6' || 
                num.charAt(n - 1) == '8')
			{
				// Number is divisible by 2
				// Condition 2
				// Check that if number is divisible by 3 or not
				var sum: Int = 0;
				var i: Int = 0;
				// Calculate sum of all digit
				while (i < n)
				{
					sum += (num.charAt(i).toInt - 48);
					i += 1;
				}
				// Condition 3
				var lastTwo: Int = (num.charAt(n - 2).toInt - 48) * 10 +
                  (num.charAt(n - 1).toInt - 48);
				if (lastTwo % 4 == 0 && sum % 3 == 0)
				{
					result = true;
				}
			}
		}
		if (result == true)
		{
			print(" Given number (" + num + ") is divisible by 12\n");
		}
		else
		{
			print(" Given number (" + num + ") is not divisible by 12\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test
		task.isDivisibleBy12("324");
		task.isDivisibleBy12("32473248726872398347612");
		task.isDivisibleBy12("234234242");
		task.isDivisibleBy12("34534567567686558438");
	}
}

Output

 Given number (324) is divisible by 12
 Given number (32473248726872398347612) is divisible by 12
 Given number (234234242) is not divisible by 12
 Given number (34534567567686558438) is not divisible by 12
import Foundation;
// Swift 4 program for
// Check that if large number is divisible by 12
class Divisibility
{
	func isDivisibleBy12(_ v: String)
	{
      	let num = Array(v);
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 3
		// ➂  Last two digit is divisible by 4
		var result: Bool = false;
		let n: Int = num.count;
		if (n == 1 && num[0] == "0")
		{
			// When given number is zero
			result = true;
		}
		if (n > 1)
		{
			if (num[n - 1] == "0" || 
                num[n - 1] == "2" || 
                num[n - 1] == "4" || 
                num[n - 1] == "6" || 
                num[n - 1] == "8")
			{
				// Number is divisible by 2
				// Condition 2
				// Check that if number is divisible by 3 or not
				var sum: Int = 0;
				var i: Int = 0;
				// Calculate sum of all digit
				while (i < n)
				{
					sum += (Int(UnicodeScalar(String(num[i]))!.value) - 48);
					i += 1;
				}
				// Condition 3
				let lastTwo: Int = (Int(UnicodeScalar(String(num[n - 2]))!.value)
                                    - 48) * 10 + 
                  (Int(UnicodeScalar(String(num[n - 1]))!.value) - 48);
				if (lastTwo % 4 == 0 && sum % 3 == 0)
				{
					result = true;
				}
			}
		}
		if (result == true)
		{
			print(" Given number (", v ,") is divisible by 12");
		}
		else
		{
			print(" Given number (", v ,") is not divisible by 12");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy12("324");
	task.isDivisibleBy12("32473248726872398347612");
	task.isDivisibleBy12("234234242");
	task.isDivisibleBy12("34534567567686558438");
}
main();

Output

 Given number ( 324 ) is divisible by 12
 Given number ( 32473248726872398347612 ) is divisible by 12
 Given number ( 234234242 ) is not divisible by 12
 Given number ( 34534567567686558438 ) is not divisible by 12
// Kotlin program for
// Check that if large number is divisible by 12
class Divisibility
{
	fun isDivisibleBy12(num: String): Unit
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 3
		// ➂  Last two digit is divisible by 4
		var result: Boolean = false;
		val n: Int = num.length;
		if (n == 1 && num.get(0) == '0')
		{
			// When given number is zero
			result = true;
		}
		if (n > 1)
		{
			if (num.get(n - 1) == '0' || 
                num.get(n - 1) == '2' || 
                num.get(n - 1) == '4' || 
                num.get(n - 1) == '6' || 
                num.get(n - 1) == '8')
			{
				// Number is divisible by 2
				// Condition 2
				// Check that if number is divisible by 3 or not
				var sum: Int = 0;
				var i: Int = 0;
				// Calculate sum of all digit
				while (i < n)
				{
					sum += (num.get(i).toInt() - 48);
					i += 1;
				}
				// Condition 3
				val lastTwo: Int = (num.get(n - 2).toInt() - 48) * 10 + 
                  (num.get(n - 1).toInt() - 48);
				if (lastTwo % 4 == 0 && sum % 3 == 0)
				{
					result = true;
				}
			}
		}
		if (result == true)
		{
			print(" Given number (" + num + ") is divisible by 12\n");
		}
		else
		{
			print(" Given number (" + num + ") is not divisible by 12\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy12("324");
	task.isDivisibleBy12("32473248726872398347612");
	task.isDivisibleBy12("234234242");
	task.isDivisibleBy12("34534567567686558438");
}

Output

 Given number (324) is divisible by 12
 Given number (32473248726872398347612) is divisible by 12
 Given number (234234242) is not divisible by 12
 Given number (34534567567686558438) is not divisible by 12

Comment

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