Check that if large number is divisible by 2
The problem at hand revolves around determining whether a large number is divisible by 2 or not. Divisibility by 2 is a fundamental mathematical concept that is frequently used in programming to optimize various algorithms and processes. When a number is divisible by 2, it means it can be evenly divided by 2 without leaving a remainder. In the context of computer programming, this often involves analyzing the last digit of a number to determine its divisibility by 2.
Problem Statement and Description
Given a large number represented as a string, we want to determine if the number is divisible by 2. The approach to solving this problem involves checking the last digit of the number and examining whether it is an even digit (0, 2, 4, 6, or 8). If the last digit is even, then the number is divisible by 2; otherwise, it's not.
Example
Let's consider the number "123456". The last digit is 6, which is an even number. Therefore, the number 123456 is divisible by 2.
Idea to Solve the Problem
- Convert the given number to a string.
- Get the last digit of the string representation of the number.
- Check if the last digit is one of the even digits (0, 2, 4, 6, 8).
Test Cases
-
Input
"122938934834534345345343940"
Output
Divisible by 2
Explanation
The last digit is 0, which is even. -
Input
"2342348234"
Output
Divisible by 2
Explanation
The last digit is 4, which is even. -
Input
"2398231234233251"
Output
Not divisible by 2
Explanation
The last digit is 1, which is not even. -
Input
"33282949823493"
Output
Not divisible by 2
Explanation
The last digit is 3, which is not even.
Pseudocode
function isDivisibleBy2(num):
last_digit = num.charAt(num.length - 1)
if last_digit is '0' or '2' or '4' or '6' or '8':
return true
else:
return false
Algorithm Explanation
- Start by obtaining the last digit of the given number.
- Check if the last digit is any of the even digits ('0', '2', '4', '6', '8').
- If the condition is satisfied, return true, indicating that the number is divisible by 2. Otherwise, return false.
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
Resultant Output Explanation
The provided Java code implements the above algorithm. It checks the last digit of the given number and verifies if it is even. Based on the result, it prints whether the number is divisible by 2 or not.
Time Complexity
The time complexity of this code is O(1) because it only involves checking the last digit of the given number, which can be done in constant time regardless of the number's length.
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