Skip to main content

Check that if large number is divisible by 16

If a large number is divisible by 16, it means that it can be evenly divided by 16 without leaving a remainder. In other words, if you divide the large number by 16, the result should be a whole number, without any fractional or decimal part.

To check whether a large number is divisible by 16, you can perform the following steps:

  • Look at the last four digits of the number. If they form a multiple of 16 (i.e., 16, 32, 48, 64, 80, 96), then the entire number is divisible by 16.
  • If the last four digits are not a multiple of 16, then the entire number is not divisible by 16.
  • If the number has fewer than four digits, you can simply check whether it is a multiple of 16 by dividing it by 16 and checking if the result is a whole number.

Code Solution

// Java program for
// Check that if large number is divisible by 16
public class Divisibility
{
	public void isDivisibleBy16(String num)
	{
		boolean result = false;
		int length = num.length();
		if (length == 1 && num.charAt(0) == '0')
		{
			result = true;
		}
		else if (length > 1)
		{
			int n = 0;
			if (length == 2)
			{
				// Get 2 digit given number
				n = (num.charAt(length - 2) - 48) * 10 + 
                  (num.charAt(length - 1) - 48);
			}
			else if (length == 3)
			{
				// Get 3 digit number
				n = ((num.charAt(length - 3) - 48) * 10 + 
                     (num.charAt(length - 2) - 48)) * 10 + 
                  (num.charAt(length - 1) - 48);
			}
			else
			{
				// Get last 4 digit number
				n = (((num.charAt(length - 4) - 48) * 10 + 
                      (num.charAt(length - 3) - 48)) * 10 + 
                     (num.charAt(length - 2) - 48)) * 10 + 
                  (num.charAt(length - 1) - 48);
			}
			if (n % 16 == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			System.out.println(" Given number (" + 
                             num + ") is divisible by 16");
		}
		else
		{
			System.out.println(" Given number (" + 
                             num + ") is not divisible by 16");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy16("16");
		task.isDivisibleBy16("1242");
		task.isDivisibleBy16("5645642343454564556756753444344367867872");
		task.isDivisibleBy16("4345345343456564564");
		task.isDivisibleBy16("65756754674560");
	}
}

Output

 Given number (16) is divisible by 16
 Given number (1242) is not divisible by 16
 Given number (5645642343454564556756753444344367867872) is divisible by 16
 Given number (4345345343456564564) is not divisible by 16
 Given number (65756754674560) is divisible by 16
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Check that if large number is divisible by 16
class Divisibility
{
	public: void isDivisibleBy16(string num)
	{
		bool result = false;
		int length = num.length();
		if (length == 1 && num[0] == '0')
		{
			result = true;
		}
		else if (length > 1)
		{
			int n = 0;
			if (length == 2)
			{
				// Get 2 digit given number
				n = (num[length - 2] - 48) *10 + (num[length - 1] - 48);
			}
			else if (length == 3)
			{
				// Get 3 digit number
				n = ((num[length - 3] - 48) * 10 + 
                     (num[length - 2] - 48)) * 10 + 
                  (num[length - 1] - 48);
			}
			else
			{
				// Get last 4 digit number
				n = (((num[length - 4] - 48) * 10 + 
                      (num[length - 3] - 48)) * 10 + 
                     (num[length - 2] - 48)) * 10 + 
                    (num[length - 1] - 48);
			}
			if (n % 16 == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			cout << " Given number (" 
                 << num << ") is divisible by 16" << endl;
		}
		else
		{
			cout << " Given number (" 
                 << num << ") is not divisible by 16" << endl;
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test
	task->isDivisibleBy16("16");
	task->isDivisibleBy16("1242");
	task->isDivisibleBy16("5645642343454564556756753444344367867872");
	task->isDivisibleBy16("4345345343456564564");
	task->isDivisibleBy16("65756754674560");
	return 0;
}

Output

 Given number (16) is divisible by 16
 Given number (1242) is not divisible by 16
 Given number (5645642343454564556756753444344367867872) is divisible by 16
 Given number (4345345343456564564) is not divisible by 16
 Given number (65756754674560) is divisible by 16
// Include namespace system
using System;
// Csharp program for
// Check that if large number is divisible by 16
public class Divisibility
{
	public void isDivisibleBy16(String num)
	{
		Boolean result = false;
		int length = num.Length;
		if (length == 1 && num[0] == '0')
		{
			result = true;
		}
		else if (length > 1)
		{
			int n = 0;
			if (length == 2)
			{
				// Get 2 digit given number
				n = (num[length - 2] - 48) * 10 + 
                  (num[length - 1] - 48);
			}
			else if (length == 3)
			{
				// Get 3 digit number
				n = ((num[length - 3] - 48) * 10 + 
                     (num[length - 2] - 48)) * 10 + 
                  (num[length - 1] - 48);
			}
			else
			{
				// Get last 4 digit number
				n = (((num[length - 4] - 48) * 10 + 
                      (num[length - 3] - 48)) * 10 + 
                     (num[length - 2] - 48)) * 10 + 
                  (num[length - 1] - 48);
			}
			if (n % 16 == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			Console.WriteLine(" Given number (" + 
                              num + ") is divisible by 16");
		}
		else
		{
			Console.WriteLine(" Given number (" + 
                              num + ") is not divisible by 16");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy16("16");
		task.isDivisibleBy16("1242");
		task.isDivisibleBy16("5645642343454564556756753444344367867872");
		task.isDivisibleBy16("4345345343456564564");
		task.isDivisibleBy16("65756754674560");
	}
}

Output

 Given number (16) is divisible by 16
 Given number (1242) is not divisible by 16
 Given number (5645642343454564556756753444344367867872) is divisible by 16
 Given number (4345345343456564564) is not divisible by 16
 Given number (65756754674560) is divisible by 16
package main
import "fmt"
// Go program for
// Check that if large number is divisible by 16
type Divisibility struct {}
func getDivisibility() * Divisibility {
	var me *Divisibility = &Divisibility {}
	return me
}
func(this Divisibility) isDivisibleBy16(num string) {
	var result bool = false
	var length int = len(num)
	if length == 1 && num[0] == '0' {
		result = true
	} else if length > 1 {
		var n int = 0
		if length == 2 {
			// Get 2 digit given number
			n = (int(num[length - 2]) - 48) * 10 + 
			(int(num[length - 1]) - 48)
		} else if length == 3 {
			// Get 3 digit number
			n = ((int(num[length - 3]) - 48) * 10 + 
				(int(num[length - 2]) - 48)) * 10 + 
			(int(num[length - 1]) - 48)
		} else {
			// Get last 4 digit number
			n = (((int(num[length - 4]) - 48) * 10 + 
				(int(num[length - 3]) - 48)) * 10 + 
				(int(num[length - 2]) - 48)) * 10 + 
			(int(num[length - 1]) - 48)
		}
		if n % 16 == 0 {
			result = true
		}
	}
	if result {
		fmt.Println(" Given number (", 
			num, ") is divisible by 16")
	} else {
		fmt.Println(" Given number (", 
			num, ") is not divisible by 16")
	}
}
func main() {
	var task * Divisibility = getDivisibility()
	// Test
	task.isDivisibleBy16("16")
	task.isDivisibleBy16("1242")
	task.isDivisibleBy16("5645642343454564556756753444344367867872")
	task.isDivisibleBy16("4345345343456564564")
	task.isDivisibleBy16("65756754674560")
}

Output

 Given number (16) is divisible by 16
 Given number (1242) is not divisible by 16
 Given number (5645642343454564556756753444344367867872) is divisible by 16
 Given number (4345345343456564564) is not divisible by 16
 Given number (65756754674560) is divisible by 16
<?php
// Php program for
// Check that if large number is divisible by 16
class Divisibility
{
	public	function isDivisibleBy16($num)
	{
		$result = false;
		$length = strlen($num);
		if ($length == 1 && $num[0] == '0')
		{
			$result = true;
		}
		else if ($length > 1)
		{
			$n = 0;
			if ($length == 2)
			{
				// Get 2 digit given number
				$n = (ord($num[$length - 2]) - 48) * 10 + 
                  (ord($num[$length - 1]) - 48);
			}
			else if ($length == 3)
			{
				// Get 3 digit number
				$n = ((ord($num[$length - 3]) - 48) * 10 + 
                      (ord($num[$length - 2]) - 48)) * 10 + 
                  (ord($num[$length - 1]) - 48);
			}
			else
			{
				// Get last 4 digit number
				$n = (((ord($num[$length - 4]) - 48) * 10 + 
                       (ord($num[$length - 3]) - 48)) * 10 + 
                      (ord($num[$length - 2]) - 48)) * 10 + 
                  (ord($num[$length - 1]) - 48);
			}
			if ($n % 16 == 0)
			{
				$result = true;
			}
		}
		if ($result)
		{
			echo(" Given number (".$num.
				") is divisible by 16".
				"\n");
		}
		else
		{
			echo(" Given number (".$num.
				") is not divisible by 16".
				"\n");
		}
	}
}

function main()
{
	$task = new Divisibility();
	// Test
	$task->isDivisibleBy16("16");
	$task->isDivisibleBy16("1242");
	$task->isDivisibleBy16("5645642343454564556756753444344367867872");
	$task->isDivisibleBy16("4345345343456564564");
	$task->isDivisibleBy16("65756754674560");
}
main();

Output

 Given number (16) is divisible by 16
 Given number (1242) is not divisible by 16
 Given number (5645642343454564556756753444344367867872) is divisible by 16
 Given number (4345345343456564564) is not divisible by 16
 Given number (65756754674560) is divisible by 16
// Node JS program for
// Check that if large number is divisible by 16
class Divisibility
{
	isDivisibleBy16(num)
	{
		var result = false;
		var length = num.length;
		if (length == 1 && num.charAt(0) == '0')
		{
			result = true;
		}
		else if (length > 1)
		{
			var n = 0;
			if (length == 2)
			{
				// Get 2 digit given number
				n = (num.charAt(length - 2).charCodeAt(0) - 48) * 10 +
                  (num.charAt(length - 1).charCodeAt(0) - 48);
			}
			else if (length == 3)
			{
				// Get 3 digit number
				n = ((num.charAt(length - 3).charCodeAt(0) - 48) * 10 + 
                     (num.charAt(length - 2).charCodeAt(0) - 48)) * 10 + 
                  (num.charAt(length - 1).charCodeAt(0) - 48);
			}
			else
			{
				// Get last 4 digit number
				n = (((num.charAt(length - 4).charCodeAt(0) - 48) * 10 + 
                      (num.charAt(length - 3).charCodeAt(0) - 48)) * 10 + 
                     (num.charAt(length - 2).charCodeAt(0) - 48)) * 10 + 
                  (num.charAt(length - 1).charCodeAt(0) - 48);
			}
			if (n % 16 == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			console.log(" Given number (" + num + ") is divisible by 16");
		}
		else
		{
			console.log(" Given number (" + num + ") is not divisible by 16");
		}
	}
}

function main()
{
	var task = new Divisibility();
	// Test
	task.isDivisibleBy16("16");
	task.isDivisibleBy16("1242");
	task.isDivisibleBy16("5645642343454564556756753444344367867872");
	task.isDivisibleBy16("4345345343456564564");
	task.isDivisibleBy16("65756754674560");
}
main();

Output

 Given number (16) is divisible by 16
 Given number (1242) is not divisible by 16
 Given number (5645642343454564556756753444344367867872) is divisible by 16
 Given number (4345345343456564564) is not divisible by 16
 Given number (65756754674560) is divisible by 16
#  Python 3 program for
#  Check that if large number is divisible by 16
class Divisibility :
	def isDivisibleBy16(self, num) :
		result = False
		length = len(num)
		if (length == 1 and num[0] == '0') :
			result = True
		elif (length > 1) :
			n = 0
			if (length == 2) :
				#  Get 2 digit given number
				n = (ord(num[length - 2]) - 48) * 10 + (
                  ord(num[length - 1]) - 48)
			elif (length == 3) :
				#  Get 3 digit number
				n = ((ord(num[length - 3]) - 48) * 10 + (
                  ord(num[length - 2]) - 48)) * 10 + (
                  ord(num[length - 1]) - 48)
			else :
				#  Get last 4 digit number
				n = (((ord(num[length - 4]) - 48) * 10 + (
                  ord(num[length - 3]) - 48)) * 10 + (
                  ord(num[length - 2]) - 48)) * 10 + (
                  ord(num[length - 1]) - 48)
			
			if (n % 16 == 0) :
				result = True
			
		
		if (result) :
			print(" Given number (", num ,") is divisible by 16")
		else :
			print(" Given number (", num ,") is not divisible by 16")
		
	

def main() :
	task = Divisibility()
	#  Test
	task.isDivisibleBy16("16")
	task.isDivisibleBy16("1242")
	task.isDivisibleBy16("5645642343454564556756753444344367867872")
	task.isDivisibleBy16("4345345343456564564")
	task.isDivisibleBy16("65756754674560")

if __name__ == "__main__": main()

Output

 Given number ( 16 ) is divisible by 16
 Given number ( 1242 ) is not divisible by 16
 Given number ( 5645642343454564556756753444344367867872 ) is divisible by 16
 Given number ( 4345345343456564564 ) is not divisible by 16
 Given number ( 65756754674560 ) is divisible by 16
#  Ruby program for
#  Check that if large number is divisible by 16
class Divisibility 
	def isDivisibleBy16(num) 
		result = false
		length = num.length
		if (length == 1 && num[0] == '0') 
			result = true
		elsif (length > 1) 
			n = 0
			if (length == 2) 
				#  Get 2 digit given number
				n = (num[length - 2].ord - 48) * 10 + 
                  (num[length - 1].ord - 48)
			elsif (length == 3) 
				#  Get 3 digit number
				n = ((num[length - 3].ord - 48) * 10 + 
                     (num[length - 2].ord - 48)) * 10 + 
                  (num[length - 1].ord - 48)
			else
 
				#  Get last 4 digit number
				n = (((num[length - 4].ord - 48) * 10 + 
                      (num[length - 3].ord - 48)) * 10 + 
                     (num[length - 2].ord - 48)) * 10 + 
                  (num[length - 1].ord - 48)
			end

			if (n % 16 == 0) 
				result = true
			end

		end

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

	end

end

def main() 
	task = Divisibility.new()
	#  Test
	task.isDivisibleBy16("16")
	task.isDivisibleBy16("1242")
	task.isDivisibleBy16("5645642343454564556756753444344367867872")
	task.isDivisibleBy16("4345345343456564564")
	task.isDivisibleBy16("65756754674560")
end

main()

Output

 Given number (16) is divisible by 16
 Given number (1242) is not divisible by 16
 Given number (5645642343454564556756753444344367867872) is divisible by 16
 Given number (4345345343456564564) is not divisible by 16
 Given number (65756754674560) is divisible by 16
import scala.collection.mutable._;
// Scala program for
// Check that if large number is divisible by 16
class Divisibility()
{
	def isDivisibleBy16(num: String): Unit = {
		var result: Boolean = false;
		var length: Int = num.length();
		if (length == 1 && num.charAt(0) == '0')
		{
			result = true;
		}
		else if (length > 1)
		{
			var n: Int = 0;
			if (length == 2)
			{
				// Get 2 digit given number
				n = (num.charAt(length - 2).toInt - 48) * 10 + 
                  (num.charAt(length - 1).toInt - 48);
			}
			else if (length == 3)
			{
				// Get 3 digit number
				n = ((num.charAt(length - 3).toInt - 48) * 10 +
                     (num.charAt(length - 2).toInt - 48)) * 10 + (
                  num.charAt(length - 1).toInt - 48);
			}
			else
			{
				// Get last 4 digit number
				n = (((num.charAt(length - 4).toInt - 48) * 10 + 
                      (num.charAt(length - 3).toInt - 48)) * 10 + 
                     (num.charAt(length - 2).toInt - 48)) * 10 + 
                  (num.charAt(length - 1).toInt - 48);
			}
			if (n % 16 == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			println(" Given number (" + num + ") is divisible by 16");
		}
		else
		{
			println(" Given number (" + num + ") is not divisible by 16");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test
		task.isDivisibleBy16("16");
		task.isDivisibleBy16("1242");
		task.isDivisibleBy16("5645642343454564556756753444344367867872");
		task.isDivisibleBy16("4345345343456564564");
		task.isDivisibleBy16("65756754674560");
	}
}

Output

 Given number (16) is divisible by 16
 Given number (1242) is not divisible by 16
 Given number (5645642343454564556756753444344367867872) is divisible by 16
 Given number (4345345343456564564) is not divisible by 16
 Given number (65756754674560) is divisible by 16
import Foundation;
// Swift 4 program for
// Check that if large number is divisible by 16
class Divisibility
{
	func isDivisibleBy16(_ x: String)
	{
      	let num = Array(x);
		var result: Bool = false;
		let length: Int = num.count;
		if (length == 1 && num[0] == "0")
		{
			result = true;
		}
		else if (length > 1)
		{
			var n: Int = 0;
			if (length == 2)
			{
				// Get 2 digit given number
				n = (Int(UnicodeScalar(
                  String(num[length - 2]))!.value) - 48) * 10 + 
                  (Int(UnicodeScalar(String(num[length - 1]))!.value) - 48);
			}
			else if (length == 3)
			{
				// Get 3 digit number
				n = ((Int(UnicodeScalar(
                  String(num[length - 3]))!.value) - 48) * 10 + 
                     (Int(UnicodeScalar(
                  String(num[length - 2]))!.value) - 48)) * 10 + 
                  (Int(UnicodeScalar(
                  String(num[length - 1]))!.value) - 48);
			}
			else
			{
				// Get last 4 digit number
				n = (((Int(UnicodeScalar(
                  String(num[length - 4]))!.value) - 48) * 10 + 
                      (Int(UnicodeScalar(
                  String(num[length - 3]))!.value) - 48)) * 10 + 
                     (Int(UnicodeScalar(
                  String(num[length - 2]))!.value) - 48)) * 10 + 
                  (Int(UnicodeScalar(
                  String(num[length - 1]))!.value) - 48);
			}
			if (n % 16 == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			print(" Given number (", x ,") is divisible by 16");
		}
		else
		{
			print(" Given number (", x ,") is not divisible by 16");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy16("16");
	task.isDivisibleBy16("1242");
	task.isDivisibleBy16("5645642343454564556756753444344367867872");
	task.isDivisibleBy16("4345345343456564564");
	task.isDivisibleBy16("65756754674560");
}
main();

Output

 Given number ( 16 ) is divisible by 16
 Given number ( 1242 ) is not divisible by 16
 Given number ( 5645642343454564556756753444344367867872 ) is divisible by 16
 Given number ( 4345345343456564564 ) is not divisible by 16
 Given number ( 65756754674560 ) is divisible by 16
// Kotlin program for
// Check that if large number is divisible by 16
class Divisibility
{
	fun isDivisibleBy16(num: String): Unit
	{
		var result: Boolean = false;
		val length: Int = num.length;
		if (length == 1 && num.get(0) == '0')
		{
			result = true;
		}
		else if (length > 1)
		{
			var n: Int ;
			if (length == 2)
			{
				// Get 2 digit given number
				n = (num.get(length - 2).toInt() - 48) * 10 + 
                  (num.get(length - 1).toInt() - 48);
			}
			else if (length == 3)
			{
				// Get 3 digit number
				n = ((num.get(length - 3).toInt() - 48) * 10 + 
                     (num.get(length - 2).toInt() - 48)) * 10 + 
                  (num.get(length - 1).toInt() - 48);
			}
			else
			{
				// Get last 4 digit number
				n = (((num.get(length - 4).toInt() - 48) * 10 + 
                      (num.get(length - 3).toInt() - 48)) * 10 + 
                     (num.get(length - 2).toInt() - 48)) * 10 + 
                  (num.get(length - 1).toInt() - 48);
			}
			if (n % 16 == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			println(" Given number (" + num + ") is divisible by 16");
		}
		else
		{
			println(" Given number (" + num + ") is not divisible by 16");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy16("16");
	task.isDivisibleBy16("1242");
	task.isDivisibleBy16("5645642343454564556756753444344367867872");
	task.isDivisibleBy16("4345345343456564564");
	task.isDivisibleBy16("65756754674560");
}

Output

 Given number (16) is divisible by 16
 Given number (1242) is not divisible by 16
 Given number (5645642343454564556756753444344367867872) is divisible by 16
 Given number (4345345343456564564) is not divisible by 16
 Given number (65756754674560) is divisible by 16




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