Posted on by Kalkicode
Code Mathematics

Check that if large number is divisible by 22

To check if a large number is divisible by 22, you need to perform the following steps:

  1. Take the last digit of the number.
  2. Double this last digit and subtract it from the remaining digits of the number.
  3. If the result is divisible by 11, then the original number is divisible by 22.

For example, let's say we want to check if the number 15324 is divisible by 22.

  1. The last digit of 15324 is 4.
  2. Double 4 to get 8. Subtracting 8 from the remaining digits gives us 1532 - 8 = 1524.
  3. We need to check if 1524 is divisible by 11. We can do this by taking the sum of the alternate digits and subtracting the sum of the remaining digits. In this case, 1 - 5 + 2 - 4 = -6, which is not divisible by 11. Therefore, 15324 is not divisible by 22.

If the result is divisible by 11, then the original number is divisible by 22.

// Java program for
// Check that if large number is divisible by 22
public class Divisibility
{
	public void isDivisibleBy22(String num)
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 11
		boolean result = false;
		int n = num.length();
		if (n == 1 && num.charAt(0) == '0')
		{
			// When given number is zero
			result = true;
		}
		else 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 11 or not
				// Use to collect alternate position digit sum
				int oddDigitSum = 0;
				int evenDigitSum = 0;
				for (int i = 0; i < n; ++i)
				{
					if (i % 2 == 0)
					{
						evenDigitSum += (num.charAt(i) - 48);
					}
					else
					{
						oddDigitSum += (num.charAt(i) - 48);
					}
				}
				if ((oddDigitSum - evenDigitSum) % 11 == 0)
				{
					// When number is divisible by 11
					result = true;
				}
			}
		}
		if (result == true)
		{
			System.out.println(" Given number (" + 
                               num + ") is divisible by 22");
		}
		else
		{
			System.out.println(" Given number (" + 
                               num + ") is not divisible by 22");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy22("34534564565");
		task.isDivisibleBy22("32473248726872398347612");
		task.isDivisibleBy22("234234242");
		task.isDivisibleBy22("34534567567686558438");
	}
}

Output

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

using namespace std;
// C++ program for
// Check that if large number is divisible by 22
class Divisibility
{
	public: void isDivisibleBy22(string num)
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 11
		bool result = false;
		int n = num.length();
		if (n == 1 && num[0] == '0')
		{
			// When given number is zero
			result = true;
		}
		else 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 11 or not
				// Use to collect alternate position digit sum
				int oddDigitSum = 0;
				int evenDigitSum = 0;
				for (int i = 0; i < n; ++i)
				{
					if (i % 2 == 0)
					{
						evenDigitSum += (num[i] - 48);
					}
					else
					{
						oddDigitSum += (num[i] - 48);
					}
				}
				if ((oddDigitSum - evenDigitSum) % 11 == 0)
				{
					// When number is divisible by 11
					result = true;
				}
			}
		}
		if (result == true)
		{
			cout << " Given number (" 
                 << num 
                 << ") is divisible by 22" << endl;
		}
		else
		{
			cout << " Given number (" 
                 << num 
                 << ") is not divisible by 22" << endl;
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test
	task->isDivisibleBy22("34534564565");
	task->isDivisibleBy22("32473248726872398347612");
	task->isDivisibleBy22("234234242");
	task->isDivisibleBy22("34534567567686558438");
	return 0;
}

Output

 Given number (34534564565) is not divisible by 22
 Given number (32473248726872398347612) is not divisible by 22
 Given number (234234242) is divisible by 22
 Given number (34534567567686558438) is divisible by 22
// Include namespace system
using System;
// Csharp program for
// Check that if large number is divisible by 22
public class Divisibility
{
	public void isDivisibleBy22(String num)
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 11
		Boolean result = false;
		int n = num.Length;
		if (n == 1 && num[0] == '0')
		{
			// When given number is zero
			result = true;
		}
		else 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 11 or not
				// Use to collect alternate position digit sum
				int oddDigitSum = 0;
				int evenDigitSum = 0;
				for (int i = 0; i < n; ++i)
				{
					if (i % 2 == 0)
					{
						evenDigitSum += (num[i] - 48);
					}
					else
					{
						oddDigitSum += (num[i] - 48);
					}
				}
				if ((oddDigitSum - evenDigitSum) % 11 == 0)
				{
					// When number is divisible by 11
					result = true;
				}
			}
		}
		if (result == true)
		{
			Console.WriteLine(" Given number (" + 
                              num + ") is divisible by 22");
		}
		else
		{
			Console.WriteLine(" Given number (" + 
                              num + ") is not divisible by 22");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy22("34534564565");
		task.isDivisibleBy22("32473248726872398347612");
		task.isDivisibleBy22("234234242");
		task.isDivisibleBy22("34534567567686558438");
	}
}

Output

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


func isDivisibleBy22(num string) {
	// Condition
	// ➀  Number should be divisible by 2
	// ➁  Number should be divisible by 11
	var result bool = false
	var n int = len(num)
	if n == 1 && num[0] == '0' {
		// When given number is zero
		result = true
	} else 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 11 or not
			// Use to collect alternate position digit sum
			var oddDigitSum int = 0
			var evenDigitSum int = 0
			for i := 0 ; i < n ; i++ {
				if i % 2 == 0 {
					evenDigitSum += (int(num[i]) - 48)
				} else {
					oddDigitSum += (int(num[i]) - 48)
				}
			}
			if (oddDigitSum - evenDigitSum) % 11 == 0 {
				// When number is divisible by 11
				result = true
			}
		}
	}
	if result == true {
		fmt.Println(" Given number (", num, ") is divisible by 22")
	} else {
		fmt.Println(" Given number (", num, ") is not divisible by 22")
	}
}
func main() {
	
	// Test
	isDivisibleBy22("34534564565")
	isDivisibleBy22("32473248726872398347612")
	isDivisibleBy22("234234242")
	isDivisibleBy22("34534567567686558438")
}

Output

 Given number (34534564565) is not divisible by 22
 Given number (32473248726872398347612) is not divisible by 22
 Given number (234234242) is divisible by 22
 Given number (34534567567686558438) is divisible by 22
<?php
// Php program for
// Check that if large number is divisible by 22
class Divisibility
{
	public	function isDivisibleBy22($num)
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 11
		$result = false;
		$n = strlen($num);
		if ($n == 1 && $num[0] == '0')
		{
			// When given number is zero
			$result = true;
		}
		else 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 11 or not
				// Use to collect alternate position digit sum
				$oddDigitSum = 0;
				$evenDigitSum = 0;
				for ($i = 0; $i < $n; ++$i)
				{
					if ($i % 2 == 0)
					{
						$evenDigitSum += (ord($num[$i]) - 48);
					}
					else
					{
						$oddDigitSum += (ord($num[$i]) - 48);
					}
				}
				if (($oddDigitSum - $evenDigitSum) % 11 == 0)
				{
					// When number is divisible by 11
					$result = true;
				}
			}
		}
		if ($result == true)
		{
			echo(" Given number (".$num.
				") is divisible by 22".
				"\n");
		}
		else
		{
			echo(" Given number (".$num.
				") is not divisible by 22".
				"\n");
		}
	}
}

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

Output

 Given number (34534564565) is not divisible by 22
 Given number (32473248726872398347612) is not divisible by 22
 Given number (234234242) is divisible by 22
 Given number (34534567567686558438) is divisible by 22
// Node JS program for
// Check that if large number is divisible by 22
class Divisibility
{
	isDivisibleBy22(num)
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 11
		var result = false;
		var n = num.length;
		if (n == 1 && num.charAt(0) == '0')
		{
			// When given number is zero
			result = true;
		}
		else 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 11 or not
				// Use to collect alternate position digit sum
				var oddDigitSum = 0;
				var evenDigitSum = 0;
				for (var i = 0; i < n; ++i)
				{
					if (i % 2 == 0)
					{
						evenDigitSum += (num.charCodeAt(i) - 48);
					}
					else
					{
						oddDigitSum += (num.charCodeAt(i) - 48);
					}
				}
				if ((oddDigitSum - evenDigitSum) % 11 == 0)
				{
					// When number is divisible by 11
					result = true;
				}
			}
		}
		if (result == true)
		{
			console.log(" Given number (" + num + ") is divisible by 22");
		}
		else
		{
			console.log(" Given number (" + num + ") is not divisible by 22");
		}
	}
}

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

Output

 Given number (34534564565) is not divisible by 22
 Given number (32473248726872398347612) is not divisible by 22
 Given number (234234242) is divisible by 22
 Given number (34534567567686558438) is divisible by 22
#  Python 3 program for
#  Check that if large number is divisible by 22
class Divisibility :
	def isDivisibleBy22(self, num) :
		#  Condition
		#  ➀  Number should be divisible by 2
		#  ➁  Number should be divisible by 11
		result = False
		n = len(num)
		if (n == 1 and num[0] == '0') :
			#  When given number is zero
			result = True
		elif (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 11 or not
				#  Use to collect alternate position digit sum
				oddDigitSum = 0
				evenDigitSum = 0
				i = 0
				while (i < n) :
					if (i % 2 == 0) :
						evenDigitSum += (ord(num[i]) - 48)
					else :
						oddDigitSum += (ord(num[i]) - 48)
					
					i += 1
				
				if ((oddDigitSum - evenDigitSum) % 11 == 0) :
					#  When number is divisible by 11
					result = True
				
			
		
		if (result == True) :
			print(" Given number (", num ,") is divisible by 22")
		else :
			print(" Given number (", num ,") is not divisible by 22")
		
	

def main() :
	task = Divisibility()
	#  Test
	task.isDivisibleBy22("34534564565")
	task.isDivisibleBy22("32473248726872398347612")
	task.isDivisibleBy22("234234242")
	task.isDivisibleBy22("34534567567686558438")

if __name__ == "__main__": main()

Output

 Given number ( 34534564565 ) is not divisible by 22
 Given number ( 32473248726872398347612 ) is not divisible by 22
 Given number ( 234234242 ) is divisible by 22
 Given number ( 34534567567686558438 ) is divisible by 22
#  Ruby program for
#  Check that if large number is divisible by 22
class Divisibility 
	def isDivisibleBy22(num) 
		#  Condition
		#  ➀  Number should be divisible by 2
		#  ➁  Number should be divisible by 11
		result = false
		n = num.length
		if (n == 1 && num[0] == '0') 
			#  When given number is zero
			result = true
		elsif (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 11 or not
				#  Use to collect alternate position digit sum
				oddDigitSum = 0
				evenDigitSum = 0
				i = 0
				while (i < n) 
					if (i % 2 == 0) 
						evenDigitSum += (num[i].ord - 48)
					else
 
						oddDigitSum += (num[i].ord - 48)
					end

					i += 1
				end

				if ((oddDigitSum - evenDigitSum) % 11 == 0) 
					#  When number is divisible by 11
					result = true
				end

			end

		end

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

	end

end

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

main()

Output

 Given number (34534564565) is not divisible by 22
 Given number (32473248726872398347612) is not divisible by 22
 Given number (234234242) is divisible by 22
 Given number (34534567567686558438) is divisible by 22
import scala.collection.mutable._;
// Scala program for
// Check that if large number is divisible by 22
class Divisibility()
{
	def isDivisibleBy22(num: String): Unit = {
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 11
		var result: Boolean = false;
		var n: Int = num.length();
		if (n == 1 && num.charAt(0) == '0')
		{
			// When given number is zero
			result = true;
		}
		else 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 11 or not
				// Use to collect alternate position digit sum
				var oddDigitSum: Int = 0;
				var evenDigitSum: Int = 0;
				var i: Int = 0;
				while (i < n)
				{
					if (i % 2 == 0)
					{
						evenDigitSum += (num.charAt(i).toInt - 48);
					}
					else
					{
						oddDigitSum += (num.charAt(i).toInt - 48);
					}
					i += 1;
				}
				if ((oddDigitSum - evenDigitSum) % 11 == 0)
				{
					// When number is divisible by 11
					result = true;
				}
			}
		}
		if (result == true)
		{
			println(" Given number (" + num + ") is divisible by 22");
		}
		else
		{
			println(" Given number (" + num + ") is not divisible by 22");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test
		task.isDivisibleBy22("34534564565");
		task.isDivisibleBy22("32473248726872398347612");
		task.isDivisibleBy22("234234242");
		task.isDivisibleBy22("34534567567686558438");
	}
}

Output

 Given number (34534564565) is not divisible by 22
 Given number (32473248726872398347612) is not divisible by 22
 Given number (234234242) is divisible by 22
 Given number (34534567567686558438) is divisible by 22
import Foundation;
// Swift 4 program for
// Check that if large number is divisible by 22
class Divisibility
{
	func isDivisibleBy22(_ v: String)
	{
      	let num = Array(v);
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 11
		var result: Bool = false;
		let n: Int = num.count;
		if (n == 1 && num[0] == "0")
		{
			// When given number is zero
			result = true;
		}
		else 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 11 or not
				// Use to collect alternate position digit sum
				var oddDigitSum: Int = 0;
				var evenDigitSum: Int = 0;
				var i: Int = 0;
				while (i < n)
				{
					if (i % 2 == 0)
					{
						evenDigitSum += 
                          (Int(UnicodeScalar(String(num[i]))!.value) - 48);
					}
					else
					{
						oddDigitSum += 
                          (Int(UnicodeScalar(String(num[i]))!.value) - 48);
					}
					i += 1;
				}
				if ((oddDigitSum - evenDigitSum) % 11 == 0)
				{
					// When number is divisible by 11
					result = true;
				}
			}
		}
		if (result == true)
		{
			print(" Given number (", v ,") is divisible by 22");
		}
		else
		{
			print(" Given number (", v ,") is not divisible by 22");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy22("34534564565");
	task.isDivisibleBy22("32473248726872398347612");
	task.isDivisibleBy22("234234242");
	task.isDivisibleBy22("34534567567686558438");
}
main();

Output

 Given number ( 34534564565 ) is not divisible by 22
 Given number ( 32473248726872398347612 ) is not divisible by 22
 Given number ( 234234242 ) is divisible by 22
 Given number ( 34534567567686558438 ) is divisible by 22
// Kotlin program for
// Check that if large number is divisible by 22
class Divisibility
{
	fun isDivisibleBy22(num: String): Unit
	{
		// Condition
		// ➀  Number should be divisible by 2
		// ➁  Number should be divisible by 11
		var result: Boolean = false;
		val n: Int = num.length;
		if (n == 1 && num.get(0) == '0')
		{
			// When given number is zero
			result = true;
		}
		else 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 11 or not
				// Use to collect alternate position digit sum
				var oddDigitSum: Int = 0;
				var evenDigitSum: Int = 0;
				var i: Int = 0;
				while (i < n)
				{
					if (i % 2 == 0)
					{
						evenDigitSum += (num.get(i).toInt() - 48);
					}
					else
					{
						oddDigitSum += (num.get(i).toInt() - 48);
					}
					i += 1;
				}
				if ((oddDigitSum - evenDigitSum) % 11 == 0)
				{
					// When number is divisible by 11
					result = true;
				}
			}
		}
		if (result == true)
		{
			println(" Given number (" + num + ") is divisible by 22");
		}
		else
		{
			println(" Given number (" + num + ") is not divisible by 22");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy22("34534564565");
	task.isDivisibleBy22("32473248726872398347612");
	task.isDivisibleBy22("234234242");
	task.isDivisibleBy22("34534567567686558438");
}

Output

 Given number (34534564565) is not divisible by 22
 Given number (32473248726872398347612) is not divisible by 22
 Given number (234234242) is divisible by 22
 Given number (34534567567686558438) is divisible by 22

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