Skip to main content

Check that if large number is divisible by 2

If a large number is divisible by 2, it means that the number can be evenly divided by 2 without leaving a remainder. In other words, the number is an even number.

To check whether a large number is divisible by 2, you can look at its last digit. If the last digit is even (i.e. 0, 2, 4, 6, or 8), then the number is divisible by 2. If the last digit is odd (i.e. 1, 3, 5, 7, or 9), then the number is not divisible by 2.

For example, if you have a large number like 123,456,789, you can check if it is divisible by 2 by looking at its last digit, which is 9. Since 9 is an odd number, the large number is not divisible by 2.

Program Solution

// Java program for
// Check that if large number is divisible by 2
public class Divisibility
{
	public void isDivisibleBy2(String num)
	{
		// Condition
		// ➀ Last digit should be a Even number
		boolean result = false;
		int n = num.length();
		if (n == 1 && num.charAt(0) == '0')
		{
			// Given number is zero
			result = true;
		}
		else if (n > 0)
		{
			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')
			{
				result = true;
			}
		}
		if (result == true)
		{
			System.out.print(" Given number (" + 
                             num + ") is divisible by 2\n");
		}
		else
		{
			System.out.print(" Given number (" + 
                             num + ") is not divisible by 2\n");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy2("122938934834534345345343940");
		task.isDivisibleBy2("2342348234");
		task.isDivisibleBy2("2398231234233251");
		task.isDivisibleBy2("33282949823493");
	}
}

Output

 Given number (122938934834534345345343940) is divisible by 2
 Given number (2342348234) is divisible by 2
 Given number (2398231234233251) is not divisible by 2
 Given number (33282949823493) is not divisible by 2
// Include header file
#include <iostream>
#include <string>
using namespace std;
// C++ program for
// Check that if large number is divisible by 2
class Divisibility
{
	public: void isDivisibleBy2(string num)
	{
		// Condition
		// ➀ Last digit should be a Even number
		bool result = false;
		int n = num.length();
		if (n == 1 && num[0] == '0')
		{
			// Given number is zero
			result = true;
		}
		else if (n > 0)
		{
			if (num[n - 1] == '0' || 
                num[n - 1] == '2' || 
                num[n - 1] == '4' || 
                num[n - 1] == '6' || 
                num[n - 1] == '8')
			{
				result = true;
			}
		}
		if (result == true)
		{
			cout << " Given number (" << num << ") is divisible by 2\n";
		}
		else
		{
			cout << " Given number (" << num << ") is not divisible by 2\n";
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test
	task->isDivisibleBy2("122938934834534345345343940");
	task->isDivisibleBy2("2342348234");
	task->isDivisibleBy2("2398231234233251");
	task->isDivisibleBy2("33282949823493");
	return 0;
}

Output

 Given number (122938934834534345345343940) is divisible by 2
 Given number (2342348234) is divisible by 2
 Given number (2398231234233251) is not divisible by 2
 Given number (33282949823493) is not divisible by 2
// Include namespace system
using System;
// Csharp program for
// Check that if large number is divisible by 2
public class Divisibility
{
	public void isDivisibleBy2(String num)
	{
		// Condition
		// ➀ Last digit should be a Even number
		Boolean result = false;
		int n = num.Length;
		if (n == 1 && num[0] == '0')
		{
			// Given number is zero
			result = true;
		}
		else if (n > 0)
		{
			if (num[n - 1] == '0' || num[n - 1] == '2' || num[n - 1] == '4' || num[n - 1] == '6' || num[n - 1] == '8')
			{
				result = true;
			}
		}
		if (result == true)
		{
			Console.Write(" Given number (" + num + ") is divisible by 2\n");
		}
		else
		{
			Console.Write(" Given number (" + num + ") is not divisible by 2\n");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy2("122938934834534345345343940");
		task.isDivisibleBy2("2342348234");
		task.isDivisibleBy2("2398231234233251");
		task.isDivisibleBy2("33282949823493");
	}
}

Output

 Given number (122938934834534345345343940) is divisible by 2
 Given number (2342348234) is divisible by 2
 Given number (2398231234233251) is not divisible by 2
 Given number (33282949823493) is not divisible by 2
package main

import "fmt"
// Go program for
// Check that if large number is divisible by 2

func isDivisibleBy2(num string) {
	// Condition
	// ➀ Last digit should be a Even number
	var result bool = false
	var n int = len(num)
	if n == 1 && num[0] == '0' {
		// Given number is zero
		result = true
	} else if n > 0 {
		if num[n - 1] == '0' || 
		   num[n - 1] == '2' || 
		   num[n - 1] == '4' || 
		   num[n - 1] == '6' || 
		   num[n - 1] == '8' {
			result = true
		}
	}
	if result == true {
		fmt.Println(" Given number (", num, ") is divisible by 2")
	} else {
		fmt.Println(" Given number (", num, ") is not divisible by 2")
	}
}
func main() {

	// Test
	isDivisibleBy2("122938934834534345345343940")
	isDivisibleBy2("2342348234")
	isDivisibleBy2("2398231234233251")
	isDivisibleBy2("33282949823493")
}

Output

 Given number (122938934834534345345343940) is divisible by 2
 Given number (2342348234) is divisible by 2
 Given number (2398231234233251) is not divisible by 2
 Given number (33282949823493) is not divisible by 2
<?php
// Php program for
// Check that if large number is divisible by 2
class Divisibility
{
	public	function isDivisibleBy2($num)
	{
		// Condition
		// ➀ Last digit should be a Even number
		$result = false;
		$n = strlen($num);
		if ($n == 1 && $num[0] == '0')
		{
			// Given number is zero
			$result = true;
		}
		else if ($n > 0)
		{
			if ($num[$n - 1] == '0' || 
                $num[$n - 1] == '2' || 
                $num[$n - 1] == '4' || 
                $num[$n - 1] == '6' || 
                $num[$n - 1] == '8')
			{
				$result = true;
			}
		}
		if ($result == true)
		{
			echo(" Given number (".$num.
				") is divisible by 2\n");
		}
		else
		{
			echo(" Given number (".$num.
				") is not divisible by 2\n");
		}
	}
}

function main()
{
	$task = new Divisibility();
	// Test
	$task->isDivisibleBy2("122938934834534345345343940");
	$task->isDivisibleBy2("2342348234");
	$task->isDivisibleBy2("2398231234233251");
	$task->isDivisibleBy2("33282949823493");
}
main();

Output

 Given number (122938934834534345345343940) is divisible by 2
 Given number (2342348234) is divisible by 2
 Given number (2398231234233251) is not divisible by 2
 Given number (33282949823493) is not divisible by 2
// Node JS program for
// Check that if large number is divisible by 2
class Divisibility
{
	isDivisibleBy2(num)
	{
		// Condition
		// ➀ Last digit should be a Even number
		var result = false;
		var n = num.length;
		if (n == 1 && num.charAt(0) == '0')
		{
			// Given number is zero
			result = true;
		}
		else if (n > 0)
		{
			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')
			{
				result = true;
			}
		}
		if (result == true)
		{
			console.log(" Given number (" + num + ") is divisible by 2");
		}
		else
		{
			console.log(" Given number (" + num + ") is not divisible by 2");
		}
	}
}

function main()
{
	var task = new Divisibility();
	// Test
	task.isDivisibleBy2("122938934834534345345343940");
	task.isDivisibleBy2("2342348234");
	task.isDivisibleBy2("2398231234233251");
	task.isDivisibleBy2("33282949823493");
}
main();

Output

 Given number (122938934834534345345343940) is divisible by 2
 Given number (2342348234) is divisible by 2
 Given number (2398231234233251) is not divisible by 2
 Given number (33282949823493) is not divisible by 2
#  Python 3 program for
#  Check that if large number is divisible by 2
class Divisibility :
	def isDivisibleBy2(self, num) :
		#  Condition
		#  ➀ Last digit should be a Even number
		result = False
		n = len(num)
		if (n == 1 and num[0] == '0') :
			#  Given number is zero
			result = True
		elif (n > 0) :
			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') :
				result = True
			
		
		if (result == True) :
			print(" Given number (", num ,") is divisible by 2")
		else :
			print(" Given number (", num ,") is not divisible by 2")
		
	

def main() :
	task = Divisibility()
	#  Test
	task.isDivisibleBy2("122938934834534345345343940")
	task.isDivisibleBy2("2342348234")
	task.isDivisibleBy2("2398231234233251")
	task.isDivisibleBy2("33282949823493")

if __name__ == "__main__": main()

Output

 Given number ( 122938934834534345345343940 ) is divisible by 2
 Given number ( 2342348234 ) is divisible by 2
 Given number ( 2398231234233251 ) is not divisible by 2
 Given number ( 33282949823493 ) is not divisible by 2
#  Ruby program for
#  Check that if large number is divisible by 2
class Divisibility 
	def isDivisibleBy2(num) 
		#  Condition
		#  ➀ Last digit should be a Even number
		result = false
		n = num.length
		if (n == 1 && num[0] == '0') 
			#  Given number is zero
			result = true
		elsif (n > 0) 
			if (num[n - 1] == '0' || 
                num[n - 1] == '2' || 
                num[n - 1] == '4' || 
                num[n - 1] == '6' || 
                num[n - 1] == '8') 
				result = true
			end

		end

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

	end

end

def main() 
	task = Divisibility.new()
	#  Test
	task.isDivisibleBy2("122938934834534345345343940")
	task.isDivisibleBy2("2342348234")
	task.isDivisibleBy2("2398231234233251")
	task.isDivisibleBy2("33282949823493")
end

main()

Output

 Given number (122938934834534345345343940) is divisible by 2
 Given number (2342348234) is divisible by 2
 Given number (2398231234233251) is not divisible by 2
 Given number (33282949823493) is not divisible by 2
import scala.collection.mutable._;
// Scala program for
// Check that if large number is divisible by 2
class Divisibility()
{
	def isDivisibleBy2(num: String): Unit = {
		// Condition
		// ➀ Last digit should be a Even number
		var result: Boolean = false;
		var n: Int = num.length();
		if (n == 1 && num.charAt(0) == '0')
		{
			// Given number is zero
			result = true;
		}
		else if (n > 0)
		{
			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')
			{
				result = true;
			}
		}
		if (result == true)
		{
			println(" Given number (" + num + ") is divisible by 2");
		}
		else
		{
			println(" Given number (" + num + ") is not divisible by 2");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test
		task.isDivisibleBy2("122938934834534345345343940");
		task.isDivisibleBy2("2342348234");
		task.isDivisibleBy2("2398231234233251");
		task.isDivisibleBy2("33282949823493");
	}
}

Output

 Given number (122938934834534345345343940) is divisible by 2
 Given number (2342348234) is divisible by 2
 Given number (2398231234233251) is not divisible by 2
 Given number (33282949823493) is not divisible by 2
import Foundation;
// Swift 4 program for
// Check that if large number is divisible by 2
class Divisibility
{
	func isDivisibleBy2(_ n: String)
	{
      	let num = Array(n);
		// Condition
		// ➀ Last digit should be a Even number
		var result: Bool = false;
		let n: Int = num.count;
		if (n == 1 && num[0] == "0")
		{
			// Given number is zero
			result = true;
		}
		else if (n > 0)
		{
			if (num[n - 1] == "0" || 
                num[n - 1] == "2" || 
                num[n - 1] == "4" || 
                num[n - 1] == "6" || 
                num[n - 1] == "8")
			{
				result = true;
			}
		}
		if (result == true)
		{
			print(" Given number (", n ,") is divisible by 2");
		}
		else
		{
			print(" Given number (", n ,") is not divisible by 2");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy2("122938934834534345345343940");
	task.isDivisibleBy2("2342348234");
	task.isDivisibleBy2("2398231234233251");
	task.isDivisibleBy2("33282949823493");
}
main();

Output

 Given number ( 27 ) is divisible by 2
 Given number ( 10 ) is divisible by 2
 Given number ( 16 ) is not divisible by 2
 Given number ( 14 ) is not divisible by 2
// Kotlin program for
// Check that if large number is divisible by 2
class Divisibility
{
	fun isDivisibleBy2(num: String): Unit
	{
		// Condition
		// ➀ Last digit should be a Even number
		var result: Boolean = false;
		val n: Int = num.length;
		if (n == 1 && num.get(0) == '0')
		{
			// Given number is zero
			result = true;
		}
		else if (n > 0)
		{
			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')
			{
				result = true;
			}
		}
		if (result == true)
		{
			println(" Given number (" + num + ") is divisible by 2");
		}
		else
		{
			println(" Given number (" + num + ") is not divisible by 2");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy2("122938934834534345345343940");
	task.isDivisibleBy2("2342348234");
	task.isDivisibleBy2("2398231234233251");
	task.isDivisibleBy2("33282949823493");
}

Output

 Given number (122938934834534345345343940) is divisible by 2
 Given number (2342348234) is divisible by 2
 Given number (2398231234233251) is not divisible by 2
 Given number (33282949823493) is not divisible by 2




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