Skip to main content

Check that if large number is divisible by 999

To check if a large number is divisible by 999, you need to add up its digits and see if the sum is divisible by 999.

For example, let's say we have the number 548769.

To add up its digits, we have:

5 + 4 + 8 + 7 + 6 + 9 = 39

Now, we need to check if 39 is divisible by 999. To do this, we can use the fact that any number is divisible by 999 if and only if it is divisible by 9 and 111.

Since 39 is not divisible by 9, we can conclude that 548769 is not divisible by 999.

Therefore, to check if a large number is divisible by 999, add up its digits and check if the sum is divisible by 999.

Code Solution

// Java program for
// Check that if large number is divisible by 999
public class Divisibility
{
	public void isDivisibleBy999(String num)
	{
		// Auxiliary variables
		boolean result = true;
		String auxiliary = num;
		int length = 0;
		int sum = 0;
		int temp = 0;
		if (num.length() == 1 && num.charAt(0) == '0')
		{
			// Given number is zero
			System.out.print(" Given number (" + 
                             num + ") is divisible by 999\n");
			return;
		}
		while (result == true && num.length() > 0)
		{
			length = auxiliary.length();
			if ((length % 3) == 2)
			{
				// When length divide by 3 remainder is 2
				// Mean need to add one more digit
				auxiliary = "0" + auxiliary;
			}
			else if ((length % 3) == 1)
			{
				// Mean need to add two more digit 
				auxiliary = "00" + auxiliary;
			}
			// Get new length
			length = auxiliary.length();
          
			for (int i = 0; i < length; ++i)
			{  
                // Find three elements
				temp = (auxiliary.charAt(i) - 48) * 100;
				i++;
				temp += (auxiliary.charAt(i) - 48) * 10;
				i++;
				temp += (auxiliary.charAt(i) - 48);
              	// Sum of group of three elements
				sum += temp;
			}
			if (sum == 999)
			{
				System.out.print(" Given number (" + 
                                 num + ") is divisible by 999\n");
				return;
			}
			else if (sum > 1000)
			{
				auxiliary = "" + sum;
			}
			else
			{
				result = false;
			}
			sum = 0;
			temp = 0;
		}
		System.out.print(" Given number (" + num + ") is not divisible by 999\n");
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy999("72348884732423538");
		task.isDivisibleBy999("9999999");
		task.isDivisibleBy999("34965");
		task.isDivisibleBy999("56456423434545645567567567867867");
		task.isDivisibleBy999("9999");
	}
}

Output

 Given number (72348884732423538) is divisible by 999
 Given number (9999999) is not divisible by 999
 Given number (34965) is divisible by 999
 Given number (56456423434545645567567567867867) is divisible by 999
 Given number (9999) is not divisible by 999
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Check that if large number is divisible by 999
class Divisibility
{
	public: void isDivisibleBy999(string num)
	{
		// Auxiliary variables
		bool result = true;
		string auxiliary = num;
		int length = 0;
		int sum = 0;
		int temp = 0;
		if (num.length() == 1 && num[0] == '0')
		{
			// Given number is zero
			cout << " Given number (" 
                 << num << ") is divisible by 999\n";
			return;
		}
		while (result == true && num.length() > 0)
		{
			length = auxiliary.length();
			if ((length % 3) == 2)
			{
				// When length divide by 3 remainder is 2
				// Mean need to add one more digit
				auxiliary = "0"+  auxiliary;
			}
			else if ((length % 3) == 1)
			{
				// Mean need to add two more digit 
				auxiliary = "00" +  auxiliary;
			}
			// Get new length
			length = auxiliary.length();
			for (int i = 0; i < length; ++i)
			{
				// Find three elements
				temp = (auxiliary[i] - 48) *100;
				i++;
				temp += (auxiliary[i] - 48) *10;
				i++;
				temp += (auxiliary[i] - 48);
				// Sum of group of three elements
				sum += temp;
			}
			if (sum == 999)
			{
				cout << " Given number (" 
                     << num << ") is divisible by 999\n";
				return;
			}
			else if (sum > 1000)
			{
				auxiliary = ""
				 +  to_string(sum);
			}
			else
			{
				result = false;
			}
			sum = 0;
			temp = 0;
		}
		cout << " Given number (" 
             << num << ") is not divisible by 999\n";
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test
	task->isDivisibleBy999("72348884732423538");
	task->isDivisibleBy999("9999999");
	task->isDivisibleBy999("34965");
	task->isDivisibleBy999("56456423434545645567567567867867");
	task->isDivisibleBy999("9999");
	return 0;
}

Output

 Given number (72348884732423538) is divisible by 999
 Given number (9999999) is not divisible by 999
 Given number (34965) is divisible by 999
 Given number (56456423434545645567567567867867) is divisible by 999
 Given number (9999) is not divisible by 999
// Include namespace system
using System;
// Csharp program for
// Check that if large number is divisible by 999
public class Divisibility
{
	public void isDivisibleBy999(String num)
	{
		// Auxiliary variables
		Boolean result = true;
		String auxiliary = num;
		int length = 0;
		int sum = 0;
		int temp = 0;
		if (num.Length == 1 && num[0] == '0')
		{
			// Given number is zero
			Console.Write(" Given number (" + 
                          num + ") is divisible by 999\n");
			return;
		}
		while (result == true && num.Length > 0)
		{
			length = auxiliary.Length;
			if ((length % 3) == 2)
			{
				// When length divide by 3 remainder is 2
				// Mean need to add one more digit
				auxiliary = "0" + auxiliary;
			}
			else if ((length % 3) == 1)
			{
				// Mean need to add two more digit 
				auxiliary = "00" + auxiliary;
			}
			// Get new length
			length = auxiliary.Length;
			for (int i = 0; i < length; ++i)
			{
				// Find three elements
				temp = (auxiliary[i] - 48) * 100;
				i++;
				temp += (auxiliary[i] - 48) * 10;
				i++;
				temp += (auxiliary[i] - 48);
				// Sum of group of three elements
				sum += temp;
			}
			if (sum == 999)
			{
				Console.Write(" Given number (" + 
                              num + ") is divisible by 999\n");
				return;
			}
			else if (sum > 1000)
			{
				auxiliary = "" + sum;
			}
			else
			{
				result = false;
			}
			sum = 0;
			temp = 0;
		}
		Console.Write(" Given number (" + 
                      num + ") is not divisible by 999\n");
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy999("72348884732423538");
		task.isDivisibleBy999("9999999");
		task.isDivisibleBy999("34965");
		task.isDivisibleBy999("56456423434545645567567567867867");
		task.isDivisibleBy999("9999");
	}
}

Output

 Given number (72348884732423538) is divisible by 999
 Given number (9999999) is not divisible by 999
 Given number (34965) is divisible by 999
 Given number (56456423434545645567567567867867) is divisible by 999
 Given number (9999) is not divisible by 999
package main
import "fmt"
import "strconv"
// Go program for
// Check that if large number is divisible by 999

func isDivisibleBy999(num string) {
	// Auxiliary variables
	var result bool = true
	var auxiliary string = num
	var length int = 0
	var sum int = 0
	var temp int = 0
	if len(num) == 1 && num[0] == '0' {
		// Given number is zero
		fmt.Println(" Given number (", 
                    num, ") is divisible by 999")
		return
	}
	for (result == true && len(auxiliary) > 0) {
		length = len(auxiliary)
		if (length % 3) == 2 {
			// When length divide by 3 remainder is 2
			// Mean need to add one more digit
			auxiliary = "0" + auxiliary
		} else if (length % 3) == 1 {
			// Mean need to add two more digit 
			auxiliary = "00" + auxiliary
		}
		// Get new length
		length = len(auxiliary)
		for i := 0 ; i < length ; i++ {
			// Find three elements
			temp = (int(auxiliary[i]) - 48) * 100
			i++
			temp += (int(auxiliary[i]) - 48) * 10
			i++
			temp += (int(auxiliary[i]) - 48)
			// Sum of group of three elements
			sum += temp
		}
		if sum == 999 {
			fmt.Println(" Given number (", 
                        num, ") is divisible by 999")
			return
		} else if sum > 1000 {
			auxiliary = strconv.Itoa(sum)
		} else {
			result = false
		}
		sum = 0

	}
	fmt.Println(" Given number (", 
                num, ") is not divisible by 999")
}
func main() {

	// Test
	isDivisibleBy999("72348884732423538")
	isDivisibleBy999("9999999")
	isDivisibleBy999("34965")
	isDivisibleBy999("56456423434545645567567567867867")
	isDivisibleBy999("9999")
}

Output

 Given number (72348884732423538) is divisible by 999
 Given number (9999999) is not divisible by 999
 Given number (34965) is divisible by 999
 Given number (56456423434545645567567567867867) is divisible by 999
 Given number (9999) is not divisible by 999
<?php
// Php program for
// Check that if large number is divisible by 999
class Divisibility
{
	public	function isDivisibleBy999($num)
	{
		// Auxiliary variables
		$result = true;
		$auxiliary = $num;
		$length = 0;
		$sum = 0;
		$temp = 0;
		if (strlen($num) == 1 && $num[0] == '0')
		{
			// Given number is zero
			echo(" Given number (".$num.
				") is divisible by 999\n");
			return;
		}
		while ($result == true && strlen($num) > 0)
		{
			$length = strlen($auxiliary);
			if (($length % 3) == 2)
			{
				// When length divide by 3 remainder is 2
				// Mean need to add one more digit
				$auxiliary = "0".$auxiliary;
			}
			else if (($length % 3) == 1)
			{
				// Mean need to add two more digit 
				$auxiliary = "00".$auxiliary;
			}
			// Get new length
			$length = strlen($auxiliary);
			for ($i = 0; $i < $length; ++$i)
			{
				// Find three elements
				$temp = (ord($auxiliary[$i]) - 48) * 100;
				$i++;
				$temp += (ord($auxiliary[$i]) - 48) * 10;
				$i++;
				$temp += (ord($auxiliary[$i]) - 48);
				// Sum of group of three elements
				$sum += $temp;
			}
			if ($sum == 999)
			{
				echo(" Given number (".$num.
					") is divisible by 999\n");
				return;
			}
			else if ($sum > 1000)
			{
				$auxiliary = "".strval($sum);
			}
			else
			{
				$result = false;
			}
			$sum = 0;
			$temp = 0;
		}
		echo(" Given number (".$num.
			") is not divisible by 999\n");
	}
}

function main()
{
	$task = new Divisibility();
	// Test
	$task->isDivisibleBy999("72348884732423538");
	$task->isDivisibleBy999("9999999");
	$task->isDivisibleBy999("34965");
	$task->isDivisibleBy999("56456423434545645567567567867867");
	$task->isDivisibleBy999("9999");
}
main();

Output

 Given number (72348884732423538) is divisible by 999
 Given number (9999999) is not divisible by 999
 Given number (34965) is divisible by 999
 Given number (56456423434545645567567567867867) is divisible by 999
 Given number (9999) is not divisible by 999
// Node JS program for
// Check that if large number is divisible by 999
class Divisibility
{
	isDivisibleBy999(num)
	{
		// Auxiliary variables
		var result = true;
		var auxiliary = num;
		var length = 0;
		var sum = 0;
		var temp = 0;
		if (num.length == 1 && num.charAt(0) == '0')
		{
			// Given number is zero
			console.log(" Given number (" + 
                        num + ") is divisible by 999");
			return;
		}
		while (result == true && num.length > 0)
		{
			length = auxiliary.length;
			if ((length % 3) == 2)
			{
				// When length divide by 3 remainder is 2
				// Mean need to add one more digit
				auxiliary = "0" + auxiliary;
			}
			else if ((length % 3) == 1)
			{
				// Mean need to add two more digit 
				auxiliary = "00" + auxiliary;
			}
			// Get new length
			length = auxiliary.length;
			for (var i = 0; i < length; ++i)
			{
				// Find three elements
				temp = (auxiliary.charCodeAt(i)  - 48) * 100;
				i++;
				temp += (auxiliary.charCodeAt(i) - 48) * 10;
				i++;
				temp += (auxiliary.charCodeAt(i) - 48);
				// Sum of group of three elements
				sum += temp;
			}
			if (sum == 999)
			{
				console.log(" Given number (" + num + ") is divisible by 999");
				return;
			}
			else if (sum > 1000)
			{
				auxiliary = "" + sum;
			}
			else
			{
				result = false;
			}
			sum = 0;
			temp = 0;
		}
		console.log(" Given number (" + num + ") is not divisible by 999");
	}
}

function main()
{
	var task = new Divisibility();
	// Test
	task.isDivisibleBy999("72348884732423538");
	task.isDivisibleBy999("9999999");
	task.isDivisibleBy999("34965");
	task.isDivisibleBy999("56456423434545645567567567867867");
	task.isDivisibleBy999("9999");
}
main();

Output

 Given number (72348884732423538) is divisible by 999
 Given number (9999999) is not divisible by 999
 Given number (34965) is divisible by 999
 Given number (56456423434545645567567567867867) is divisible by 999
 Given number (9999) is not divisible by 999
#  Python 3 program for
#  Check that if large number is divisible by 999
class Divisibility :
	def isDivisibleBy999(self, num) :
		#  Auxiliary variables
		result = True
		auxiliary = num
		length = 0
		sum = 0
		temp = 0
		if (len(num) == 1 and num[0] == '0') :
			#  Given number is zero
			print(" Given number (", num ,") is divisible by 999")
			return
		
		while (result == True and len(num) > 0) :
			length = len(auxiliary)
			if ((length % 3) == 2) :
				#  When length divide by 3 remainder is 2
				#  Mean need to add one more digit
				auxiliary = "0" + auxiliary
			elif ((length % 3) == 1) :
				#  Mean need to add two more digit 
				auxiliary = "00" + auxiliary
			
			#  Get new length
			length = len(auxiliary)
			i = 0
			while (i < length) :
				#  Find three elements
				temp = (ord(auxiliary[i]) - 48) * 100
				i += 1
				temp += (ord(auxiliary[i]) - 48) * 10
				i += 1
				temp += (ord(auxiliary[i]) - 48)
				#  Sum of group of three elements
				sum += temp
				i += 1
			
			if (sum == 999) :
				print(" Given number (", num ,") is divisible by 999")
				return
			elif (sum > 1000) :
				auxiliary = ""+ str(sum)
			else :
				result = False
			
			sum = 0
			temp = 0
		
		print(" Given number (", num ,") is not divisible by 999")
	

def main() :
	task = Divisibility()
	#  Test
	task.isDivisibleBy999("72348884732423538")
	task.isDivisibleBy999("9999999")
	task.isDivisibleBy999("34965")
	task.isDivisibleBy999("56456423434545645567567567867867")
	task.isDivisibleBy999("9999")

if __name__ == "__main__": main()

Output

 Given number ( 72348884732423538 ) is divisible by 999
 Given number ( 9999999 ) is not divisible by 999
 Given number ( 34965 ) is divisible by 999
 Given number ( 56456423434545645567567567867867 ) is divisible by 999
 Given number ( 9999 ) is not divisible by 999
#  Ruby program for
#  Check that if large number is divisible by 999
class Divisibility 
	def isDivisibleBy999(num) 
		#  Auxiliary variables
		result = true
		auxiliary = num
		length = 0
		sum = 0
		temp = 0
		if (num.length == 1 && num[0] == '0') 
			#  Given number is zero
			print(" Given number (", 
                  num ,") is divisible by 999", "\n")
			return
		end

		while (result == true && num.length > 0) 
			length = auxiliary.length
			if ((length % 3) == 2) 
				#  When length divide by 3 remainder is 2
				#  Mean need to add one more digit
				auxiliary = "0" + auxiliary
			elsif ((length % 3) == 1) 
				#  Mean need to add two more digit 
				auxiliary = "00" + auxiliary
			end

			#  Get new length
			length = auxiliary.length
			i = 0
			while (i < length) 
				#  Find three elements
				temp = (auxiliary[i].ord - 48) * 100
				i += 1
				temp += (auxiliary[i].ord - 48) * 10
				i += 1
				temp += (auxiliary[i].ord - 48)
				#  Sum of group of three elements
				sum += temp
				i += 1
			end

			if (sum == 999) 
				print(" Given number (", 
                      num ,") is divisible by 999", "\n")
				return
			elsif (sum > 1000) 
				auxiliary =  sum.to_s
			else
 
				result = false
			end

			sum = 0
			temp = 0
		end

		print(" Given number (", 
              num ,") is not divisible by 999", "\n")
	end

end

def main() 
	task = Divisibility.new()
	#  Test
	task.isDivisibleBy999("72348884732423538")
	task.isDivisibleBy999("9999999")
	task.isDivisibleBy999("34965")
	task.isDivisibleBy999("56456423434545645567567567867867")
	task.isDivisibleBy999("9999")
end

main()

Output

 Given number (72348884732423538) is divisible by 999
 Given number (9999999) is not divisible by 999
 Given number (34965) is divisible by 999
 Given number (56456423434545645567567567867867) is divisible by 999
 Given number (9999) is not divisible by 999
// Scala program for
// Check that if large number is divisible by 999
class Divisibility()
{
	def isDivisibleBy999(num: String): Unit = {
		// Auxiliary variables
		var result: Boolean = true;
		var auxiliary: String = num;
		var length: Int = 0;
		var sum: Int = 0;
		var temp: Int = 0;
		if (num.length() == 1 && num.charAt(0) == '0')
		{
			// Given number is zero
			println(" Given number (" + num + ") is divisible by 999");
			return;
		}
		while (result == true && num.length() > 0)
		{
			length = auxiliary.length();
			if ((length % 3) == 2)
			{
				// When length divide by 3 remainder is 2
				// Mean need to add one more digit
				auxiliary = "0" + auxiliary;
			}
			else if ((length % 3) == 1)
			{
				// Mean need to add two more digit 
				auxiliary = "00" + auxiliary;
			}
			// Get new length
			length = auxiliary.length();
			var i: Int = 0;
			while (i < length)
			{
				// Find three elements
				temp = (auxiliary.charAt(i).toInt - 48) * 100;
				i += 1;
				temp += (auxiliary.charAt(i).toInt - 48) * 10;
				i += 1;
				temp += (auxiliary.charAt(i).toInt - 48);
				// Sum of group of three elements
				sum += temp;
				i += 1;
			}
			if (sum == 999)
			{
				println(" Given number (" + num + ") is divisible by 999");
				return;
			}
			else if (sum > 1000)
			{
				auxiliary = sum.toString();
			}
			else
			{
				result = false;
			}
			sum = 0;
			temp = 0;
		}
		println(" Given number (" + num + ") is not divisible by 999");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test
		task.isDivisibleBy999("72348884732423538");
		task.isDivisibleBy999("9999999");
		task.isDivisibleBy999("34965");
		task.isDivisibleBy999("56456423434545645567567567867867");
		task.isDivisibleBy999("9999");
	}
}

Output

 Given number (72348884732423538) is divisible by 999
 Given number (9999999) is not divisible by 999
 Given number (34965) is divisible by 999
 Given number (56456423434545645567567567867867) is divisible by 999
 Given number (9999) is not divisible by 999
import Foundation;
// Swift 4 program for
// Check that if large number is divisible by 999
class Divisibility
{
	func isDivisibleBy999(_ v: String)
	{
      	let num = Array(v);
		// Auxiliary variables
		var result: Bool = true;
		var auxiliary: String = v;
		var length: Int = 0;
		var sum: Int = 0;
		var temp: Int = 0;
		if (num.count == 1 && num[0] == "0")
		{
			// Given number is zero
			print(" Given number (", v ,") is divisible by 999");
			return;
		}
		while (result == true && num.count > 0)
		{
			length = auxiliary.count;
			if ((length % 3) == 2)
			{
				// When length divide by 3 remainder is 2
				// Mean need to add one more digit
				auxiliary = "0" + auxiliary;
			}
			else if ((length % 3) == 1)
			{
				// Mean need to add two more digit 
				auxiliary = "00" + auxiliary;
			}
			// Get new length
			length = auxiliary.count;
			var i: Int = 0;
          	let check = Array(auxiliary);
			while (i < length)
			{
				// Find three elements
				temp = (Int(UnicodeScalar(String(check[i]))!.value) - 48) * 
                  100;
				i += 1;
				temp += (Int(UnicodeScalar(String(check[i]))!.value) - 48) *
                  10;
				i += 1;
				temp += (Int(UnicodeScalar(String(check[i]))!.value) - 48);
				// Sum of group of three elements
				sum += temp;
				i += 1;
			}
			if (sum == 999)
			{
				print(" Given number (", v ,") is divisible by 999");
				return;
			}
			else if (sum > 1000)
			{
				auxiliary = String(sum);
			}
			else
			{
				result = false;
			}
			sum = 0;
			temp = 0;
		}
		print(" Given number (", v ,") is not divisible by 999");
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy999("72348884732423538");
	task.isDivisibleBy999("9999999");
	task.isDivisibleBy999("34965");
	task.isDivisibleBy999("56456423434545645567567567867867");
	task.isDivisibleBy999("9999");
}
main();

Output

 Given number ( 72348884732423538 ) is divisible by 999
 Given number ( 9999999 ) is not divisible by 999
 Given number ( 34965 ) is divisible by 999
 Given number ( 56456423434545645567567567867867 ) is divisible by 999
 Given number ( 9999 ) is not divisible by 999
// Kotlin program for
// Check that if large number is divisible by 999
class Divisibility
{
	fun isDivisibleBy999(num: String): Unit
	{
		// Auxiliary variables
		var result: Boolean = true;
		var auxiliary: String = num;
		var length: Int ;
		var sum: Int = 0;
		var temp: Int  ;
		if (num.length == 1 && num.get(0) == '0')
		{
			// Given number is zero
			println(" Given number (" + num + ") is divisible by 999");
			return;
		}
		while (result == true && num.length > 0)
		{
			length = auxiliary.length;
			if ((length % 3) == 2)
			{
				// When length divide by 3 remainder is 2
				// Mean need to add one more digit
				auxiliary = "0" + auxiliary;
			}
			else if ((length % 3) == 1)
			{
				// Mean need to add two more digit 
				auxiliary = "00" + auxiliary;
			}
			// Get new length
			length = auxiliary.length;
			var i: Int = 0;
			while (i < length)
			{
				// Find three elements
				temp = (auxiliary.get(i).toInt() - 48) * 100;
				i += 1;
				temp += (auxiliary.get(i).toInt() - 48) * 10;
				i += 1;
				temp += (auxiliary.get(i).toInt() - 48);
				// Sum of group of three elements
				sum += temp;
				i += 1;
			}
			if (sum == 999)
			{
				println(" Given number (" + num + ") is divisible by 999");
				return;
			}
			else if (sum > 1000)
			{
				auxiliary = sum.toString();
			}
			else
			{
				result = false;
			}
			sum = 0;
		}
		println(" Given number (" + num + ") is not divisible by 999");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy999("72348884732423538");
	task.isDivisibleBy999("9999999");
	task.isDivisibleBy999("34965");
	task.isDivisibleBy999("56456423434545645567567567867867");
	task.isDivisibleBy999("9999");
}

Output

 Given number (72348884732423538) is divisible by 999
 Given number (9999999) is not divisible by 999
 Given number (34965) is divisible by 999
 Given number (56456423434545645567567567867867) is divisible by 999
 Given number (9999) is not divisible by 999




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