Check that if large number is divisible by 13

Here given code implementation process.

// Java program for
// Check that if large number is divisible by 13
public class Divisibility
{
	public int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public void isDivisibleBy13(String num)
	{
		boolean result = false;
		int sum = 0;
		int sign = 1;
		int temp = 0;
		String auxiliary = num;
		int length = auxiliary.length();
		if (length == 1 && num.charAt(0) == '0')
		{
            // When number is zero
			result = true;
		}
		else if(length > 1)
		{
			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 = length - 1; i >= 0; --i)
			{
				temp = (auxiliary.charAt(i) - 48);
				i--;
				temp += (auxiliary.charAt(i) - 48) * 10;
				i--;
				temp += (auxiliary.charAt(i) - 48) * 100;
				sum += (temp * sign);
				sign = sign * -1;
			}
			if ((absValue(sum) % 13) == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			System.out.println(" Given number (" + 
                             num + ") is divisible by 13");
		}
		else
		{
			System.out.println(" Given number (" + 
                             num + ") is not divisible by 13");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy13("45645645");
		task.isDivisibleBy13("6576534533439");
		task.isDivisibleBy13("34965");
		task.isDivisibleBy13("56456423434545645567567567867867");
		task.isDivisibleBy13("9999912");
	}
}

Output

 Given number (45645645) is not divisible by 13
 Given number (6576534533439) is divisible by 13
 Given number (34965) is not divisible by 13
 Given number (56456423434545645567567567867867) is not divisible by 13
 Given number (9999912) is divisible by 13
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Check that if large number is divisible by 13
class Divisibility
{
	public: int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	void isDivisibleBy13(string num)
	{
		bool result = false;
		int sum = 0;
		int sign = 1;
		int temp = 0;
		string auxiliary = num;
		int length = auxiliary.length();
		if (length == 1 && num[0] == '0')
		{
			// When number is zero
			result = true;
		}
		else if (length > 1)
		{
			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 = length - 1; i >= 0; --i)
			{
				temp = (auxiliary[i] - 48);
				i--;
				temp += (auxiliary[i] - 48) *10;
				i--;
				temp += (auxiliary[i] - 48) *100;
				sum += (temp *sign);
				sign = sign *-1;
			}
			if ((this->absValue(sum) % 13) == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			cout << " Given number (" 
                 << num << ") is divisible by 13" << endl;
		}
		else
		{
			cout << " Given number (" 
                 << num << ") is not divisible by 13" << endl;
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test
	task->isDivisibleBy13("45645645");
	task->isDivisibleBy13("6576534533439");
	task->isDivisibleBy13("34965");
	task->isDivisibleBy13("56456423434545645567567567867867");
	task->isDivisibleBy13("9999912");
	return 0;
}

Output

 Given number (45645645) is not divisible by 13
 Given number (6576534533439) is divisible by 13
 Given number (34965) is not divisible by 13
 Given number (56456423434545645567567567867867) is not divisible by 13
 Given number (9999912) is divisible by 13
// Include namespace system
using System;
// Csharp program for
// Check that if large number is divisible by 13
public class Divisibility
{
	public int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public void isDivisibleBy13(String num)
	{
		Boolean result = false;
		int sum = 0;
		int sign = 1;
		int temp = 0;
		String auxiliary = num;
		int length = auxiliary.Length;
		if (length == 1 && num[0] == '0')
		{
			// When number is zero
			result = true;
		}
		else if (length > 1)
		{
			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 = length - 1; i >= 0; --i)
			{
				temp = (auxiliary[i] - 48);
				i--;
				temp += (auxiliary[i] - 48) * 10;
				i--;
				temp += (auxiliary[i] - 48) * 100;
				sum += (temp * sign);
				sign = sign * -1;
			}
			if ((this.absValue(sum) % 13) == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			Console.WriteLine(" Given number (" + 
                              num + ") is divisible by 13");
		}
		else
		{
			Console.WriteLine(" Given number (" + 
                              num + ") is not divisible by 13");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy13("45645645");
		task.isDivisibleBy13("6576534533439");
		task.isDivisibleBy13("34965");
		task.isDivisibleBy13("56456423434545645567567567867867");
		task.isDivisibleBy13("9999912");
	}
}

Output

 Given number (45645645) is not divisible by 13
 Given number (6576534533439) is divisible by 13
 Given number (34965) is not divisible by 13
 Given number (56456423434545645567567567867867) is not divisible by 13
 Given number (9999912) is divisible by 13
package main
import "fmt"
// Go program for
// Check that if large number is divisible by 13
type Divisibility struct {}
func getDivisibility() * Divisibility {
	var me *Divisibility = &Divisibility {}
	return me
}
func (this Divisibility)absValue(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func(this Divisibility) isDivisibleBy13(num string) {
	var result bool = false
	var sum int = 0
	var sign int = 1
	var temp int = 0
	var auxiliary string = num
	var length int = len(auxiliary)
	if length == 1 && num[0] == '0' {
		// When number is zero
		result = true
	} else if length > 1 {
		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 := length - 1 ; i >= 0 ; i-- {
			temp = (int(auxiliary[i]) - 48)
			i--
			temp += (int(auxiliary[i]) - 48) * 10
			i--
			temp += (int(auxiliary[i]) - 48) * 100
			sum += (temp * sign)
			sign = sign * -1
		}
		if (this.absValue(sum) % 13) == 0 {
			result = true
		}
	}
	if result {
		fmt.Println(" Given number (", num, ") is divisible by 13")
	} else {
		fmt.Println(" Given number (", num, ") is not divisible by 13")
	}
}
func main() {
	var task * Divisibility = getDivisibility()
	// Test
	task.isDivisibleBy13("45645645")
	task.isDivisibleBy13("6576534533439")
	task.isDivisibleBy13("34965")
	task.isDivisibleBy13("56456423434545645567567567867867")
	task.isDivisibleBy13("9999912")
}

Output

 Given number (45645645) is not divisible by 13
 Given number (6576534533439) is divisible by 13
 Given number (34965) is not divisible by 13
 Given number (56456423434545645567567567867867) is not divisible by 13
 Given number (9999912) is divisible by 13
<?php
// Php program for
// Check that if large number is divisible by 13
class Divisibility
{
	public	function absValue($x)
	{
		if ($x < 0)
		{
			return -$x;
		}
		return $x;
	}
	public	function isDivisibleBy13($num)
	{
		$result = false;
		$sum = 0;
		$sign = 1;
		$temp = 0;
		$auxiliary = $num;
		$length = strlen($auxiliary);
		if ($length == 1 && $num[0] == '0')
		{
			// When number is zero
			$result = true;
		}
		else if ($length > 1)
		{
			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 = $length - 1; $i >= 0; --$i)
			{
				$temp = (ord($auxiliary[$i]) - 48);
				$i--;
				$temp += (ord($auxiliary[$i]) - 48) * 10;
				$i--;
				$temp += (ord($auxiliary[$i]) - 48) * 100;
				$sum += ($temp * $sign);
				$sign = $sign * -1;
			}
			if (($this->absValue($sum) % 13) == 0)
			{
				$result = true;
			}
		}
		if ($result)
		{
			echo(" Given number (".$num.
				") is divisible by 13".
				"\n");
		}
		else
		{
			echo(" Given number (".$num.
				") is not divisible by 13".
				"\n");
		}
	}
}

function main()
{
	$task = new Divisibility();
	// Test
	$task->isDivisibleBy13("45645645");
	$task->isDivisibleBy13("6576534533439");
	$task->isDivisibleBy13("34965");
	$task->isDivisibleBy13("56456423434545645567567567867867");
	$task->isDivisibleBy13("9999912");
}
main();

Output

 Given number (45645645) is not divisible by 13
 Given number (6576534533439) is divisible by 13
 Given number (34965) is not divisible by 13
 Given number (56456423434545645567567567867867) is not divisible by 13
 Given number (9999912) is divisible by 13
// Node JS program for
// Check that if large number is divisible by 13
class Divisibility
{
	absValue(x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	isDivisibleBy13(num)
	{
		var result = false;
		var sum = 0;
		var sign = 1;
		var temp = 0;
		var auxiliary = num;
		var length = auxiliary.length;
		if (length == 1 && num.charAt(0) == '0')
		{
			// When number is zero
			result = true;
		}
		else if (length > 1)
		{
			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 = length - 1; i >= 0; --i)
			{
				temp = (auxiliary.charAt(i).charCodeAt(0) - 48);
				i--;
				temp += (auxiliary.charAt(i).charCodeAt(0) - 48) * 10;
				i--;
				temp += (auxiliary.charAt(i).charCodeAt(0) - 48) * 100;
				sum += (temp * sign);
				sign = sign * -1;
			}
			if ((this.absValue(sum) % 13) == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			console.log(" Given number (" + num + ") is divisible by 13");
		}
		else
		{
			console.log(" Given number (" + num + ") is not divisible by 13");
		}
	}
}

function main()
{
	var task = new Divisibility();
	// Test
	task.isDivisibleBy13("45645645");
	task.isDivisibleBy13("6576534533439");
	task.isDivisibleBy13("34965");
	task.isDivisibleBy13("56456423434545645567567567867867");
	task.isDivisibleBy13("9999912");
}
main();

Output

 Given number (45645645) is not divisible by 13
 Given number (6576534533439) is divisible by 13
 Given number (34965) is not divisible by 13
 Given number (56456423434545645567567567867867) is not divisible by 13
 Given number (9999912) is divisible by 13
#  Python 3 program for
#  Check that if large number is divisible by 13
class Divisibility :
	def absValue(self, x) :
		if (x < 0) :
			return -x
		
		return x
	
	def isDivisibleBy13(self, num) :
		result = False
		sum = 0
		sign = 1
		temp = 0
		auxiliary = num
		length = len(auxiliary)
		if (length == 1 and num[0] == '0') :
			#  When number is zero
			result = True
		elif (length > 1) :
			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 = length - 1
			while (i >= 0) :
				temp = (ord(auxiliary[i]) - 48)
				i -= 1
				temp += (ord(auxiliary[i]) - 48) * 10
				i -= 1
				temp += (ord(auxiliary[i]) - 48) * 100
				sum += (temp * sign)
				sign = sign * -1
				i -= 1
			
			if ((self.absValue(sum) % 13) == 0) :
				result = True
			
		
		if (result) :
			print(" Given number (", num ,") is divisible by 13")
		else :
			print(" Given number (", num ,") is not divisible by 13")
		
	

def main() :
	task = Divisibility()
	#  Test
	task.isDivisibleBy13("45645645")
	task.isDivisibleBy13("6576534533439")
	task.isDivisibleBy13("34965")
	task.isDivisibleBy13("56456423434545645567567567867867")
	task.isDivisibleBy13("9999912")

if __name__ == "__main__": main()

Output

 Given number ( 45645645 ) is not divisible by 13
 Given number ( 6576534533439 ) is divisible by 13
 Given number ( 34965 ) is not divisible by 13
 Given number ( 56456423434545645567567567867867 ) is not divisible by 13
 Given number ( 9999912 ) is divisible by 13
#  Ruby program for
#  Check that if large number is divisible by 13
class Divisibility 
	def absValue(x) 
		if (x < 0) 
			return -x
		end

		return x
	end

	def isDivisibleBy13(num) 
		result = false
		sum = 0
		sign = 1
		temp = 0
		auxiliary = num
		length = auxiliary.length
		if (length == 1 && num[0] == '0') 
			#  When number is zero
			result = true
		elsif (length > 1) 
			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 = length - 1
			while (i >= 0) 
				temp = (auxiliary[i].ord - 48)
				i -= 1
				temp += (auxiliary[i].ord - 48) * 10
				i -= 1
				temp += (auxiliary[i].ord - 48) * 100
				sum += (temp * sign)
				sign = sign * -1
				i -= 1
			end

			if ((self.absValue(sum) % 13) == 0) 
				result = true
			end

		end

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

	end

end

def main() 
	task = Divisibility.new()
	#  Test
	task.isDivisibleBy13("45645645")
	task.isDivisibleBy13("6576534533439")
	task.isDivisibleBy13("34965")
	task.isDivisibleBy13("56456423434545645567567567867867")
	task.isDivisibleBy13("9999912")
end

main()

Output

 Given number (45645645) is not divisible by 13
 Given number (6576534533439) is divisible by 13
 Given number (34965) is not divisible by 13
 Given number (56456423434545645567567567867867) is not divisible by 13
 Given number (9999912) is divisible by 13
import scala.collection.mutable._;
// Scala program for
// Check that if large number is divisible by 13
class Divisibility()
{
	def absValue(x: Int): Int = {
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	def isDivisibleBy13(num: String): Unit = {
		var result: Boolean = false;
		var sum: Int = 0;
		var sign: Int = 1;
		var temp: Int = 0;
		var auxiliary: String = num;
		var length: Int = auxiliary.length();
		if (length == 1 && num.charAt(0) == '0')
		{
			// When number is zero
			result = true;
		}
		else if (length > 1)
		{
			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 = length - 1;
			while (i >= 0)
			{
				temp = (auxiliary.charAt(i).toInt - 48);
				i -= 1;
				temp += (auxiliary.charAt(i).toInt - 48) * 10;
				i -= 1;
				temp += (auxiliary.charAt(i).toInt - 48) * 100;
				sum += (temp * sign);
				sign = sign * -1;
				i -= 1;
			}
			if ((absValue(sum) % 13) == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			println(" Given number (" + num + ") is divisible by 13");
		}
		else
		{
			println(" Given number (" + num + ") is not divisible by 13");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test
		task.isDivisibleBy13("45645645");
		task.isDivisibleBy13("6576534533439");
		task.isDivisibleBy13("34965");
		task.isDivisibleBy13("56456423434545645567567567867867");
		task.isDivisibleBy13("9999912");
	}
}

Output

 Given number (45645645) is not divisible by 13
 Given number (6576534533439) is divisible by 13
 Given number (34965) is not divisible by 13
 Given number (56456423434545645567567567867867) is not divisible by 13
 Given number (9999912) is divisible by 13
import Foundation;
// Swift 4 program for
// Check that if large number is divisible by 13
class Divisibility
{
	func absValue(_ x: Int) -> Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	func isDivisibleBy13(_ x: String)
	{
        let num = Array(x);
		var result: Bool = false;
		var sum: Int = 0;
		var sign: Int = 1;
		var temp: Int = 0;
		var auxiliary: String = x;
		var length: Int = auxiliary.count;
		if (length == 1 && num[0] == "0")
		{
			// When number is zero
			result = true;
		}
		else if (length > 1)
		{
			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 = length - 1;
            let v = Array(auxiliary); 
			while (i >= 0)
			{
				temp = (Int(UnicodeScalar(String(v[i]))!.value) - 48);
				i -= 1;
				temp += (Int(UnicodeScalar(String(v[i]))!.value) - 48) * 
                  10;
				i -= 1;
				temp += (Int(UnicodeScalar(String(v[i]))!.value) - 48) *
                  100;
				sum += (temp * sign);
				sign = sign * -1;
				i -= 1;
			}
			if ((self.absValue(sum) % 13) == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			print(" Given number (", x ,") is divisible by 13");
		}
		else
		{
			print(" Given number (", x ,") is not divisible by 13");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy13("45645645");
	task.isDivisibleBy13("6576534533439");
	task.isDivisibleBy13("34965");
	task.isDivisibleBy13("56456423434545645567567567867867");
	task.isDivisibleBy13("9999912");
}
main();

Output

 Given number ( 45645645 ) is not divisible by 13
 Given number ( 6576534533439 ) is divisible by 13
 Given number ( 34965 ) is not divisible by 13
 Given number ( 56456423434545645567567567867867 ) is not divisible by 13
 Given number ( 9999912 ) is divisible by 13
// Kotlin program for
// Check that if large number is divisible by 13
class Divisibility
{
	fun absValue(x: Int): Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	fun isDivisibleBy13(num: String): Unit
	{
		var result: Boolean = false;
		var sum: Int = 0;
		var sign: Int = 1;
		var temp: Int;
		var auxiliary: String = num;
		var length: Int = auxiliary.length;
		if (length == 1 && num.get(0) == '0')
		{
			// When number is zero
			result = true;
		}
		else if (length > 1)
		{
			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 = length - 1;
			while (i >= 0)
			{
				temp = (auxiliary.get(i).toInt() - 48);
				i -= 1;
				temp += (auxiliary.get(i).toInt() - 48) * 10;
				i -= 1;
				temp += (auxiliary.get(i).toInt() - 48) * 100;
				sum += (temp * sign);
				sign = sign * -1;
				i -= 1;
			}
			if ((this.absValue(sum) % 13) == 0)
			{
				result = true;
			}
		}
		if (result)
		{
			println(" Given number (" + num + ") is divisible by 13");
		}
		else
		{
			println(" Given number (" + num + ") is not divisible by 13");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy13("45645645");
	task.isDivisibleBy13("6576534533439");
	task.isDivisibleBy13("34965");
	task.isDivisibleBy13("56456423434545645567567567867867");
	task.isDivisibleBy13("9999912");
}

Output

 Given number (45645645) is not divisible by 13
 Given number (6576534533439) is divisible by 13
 Given number (34965) is not divisible by 13
 Given number (56456423434545645567567567867867) is not divisible by 13
 Given number (9999912) is divisible by 13


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