Check if large number is divisible by 20
In this article, we will discuss a problem and its solution in C that involves checking whether a large number is divisible by 20. We will explain the problem, provide a suitable example, present the algorithm and pseudocode, and finally discuss the output with the time complexity of the code.
Problem Introduction
The problem is to determine if a given large number, represented as a string, is divisible by 20. Divisibility by 20 means that the number can be divided evenly by 20 without leaving a remainder.
Problem Statement
Given a positive number as a string, we need to check if the number is divisible by 20.
Example
Let's consider the number "93782639377588932980". We want to check if this number is divisible by 20.
To solve this problem, we can examine the last two digits of the given number. In our example, the last two digits are "80". We convert these two digits to an integer value, which is 80 in this case. If the resulting value is divisible by 20, then the entire number is divisible by 20 as well.
In our example, 80 is divisible by 20, so the number "93782639377588932980" is divisible by 20.
Algorithm and Pseudocode
The following algorithm describes the steps to check if a large number is divisible by 20:
- Get the length of the number string.
- If the length is 0, return.
- If the length is 1 and the only digit is '0', the number is divisible by 20.
- If the length is greater than 1 and the last digit is '0', collect the value of the last two digits as an integer.
- If the value of the last two digits is divisible by 20, the entire number is divisible by 20.
Here is the pseudocode for the algorithm:
divisibleBy20(num):
n = length(num)
result = 0
if n == 0:
return
if n == 1 and num[0] == '0':
result = 1
else if n > 1 and num[n - 1] == '0':
value = (num[n - 2] - '0') * 10 + (num[n - 1] - '0')
if value % 20 == 0:
result = 1
if result == 1:
print "Number", num, "is divisible by 20"
else:
print "Number", num, "is not divisible by 20"
Code Solution
/*
C program for
Check if large number is divisible by 20
*/
#include <stdio.h>
#include <string.h>
void divisibleBy20(const char *num)
{
// Assuming that number contains valid positive number
// Get the length of num
int n = strlen(num);
// Resultant indicator
int result = 0;
if (n == 0)
{
return;
}
if (n == 1 && num[0] == '0')
{
// When single digit and number is zero
result = 1;
}
else if (n > 1 && num[n - 1] == '0')
{
// Collect a number of last two digits
int value = ((num[n - 2] - '0') *10) + (num[n - 1] - '0');
if (value % 20 == 0)
{
// When last 2 digit number is divisible by 20
result = 1;
}
}
if (result == 1)
{
printf("\n Number %s is divisible by 20", num);
}
else
{
printf("\n Number %s is not divisible by 20", num);
}
}
int main(int argc, char
const *argv[])
{
// Test Inputs
divisibleBy20("110");
divisibleBy20("93782639377588932980");
divisibleBy20("33612315332343");
divisibleBy20("3824566134312124240");
return 0;
}
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
/*
Java program for
Check if large number is divisible by 20
*/
class Divisibility
{
public void divisibleBy20(String num)
{
// Assuming that number contains valid positive number
// Get the length of num
int n = num.length();
// Resultant indicator
int result = 0;
if (n == 0)
{
return;
}
if (n == 1 && num.charAt(0) == '0')
{
// When single digit and number is zero
result = 1;
}
else if (n > 1 && num.charAt(n - 1) == '0')
{
// Collect a number of last two digits
int value = ((num.charAt(n - 2) - '0') * 10) +
(num.charAt(n - 1) - '0');
if ((value % 20) == 0)
{
// When last 2 digit number is divisible by 20
result = 1;
}
}
if (result == 1)
{
System.out.print("\n Number " + num + " is divisible by 20");
}
else
{
System.out.print("\n Number " + num + " is not divisible by 20");
}
}
public static void main(String[] args)
{
Divisibility task = new Divisibility();
// Test Inputs
task.divisibleBy20("110");
task.divisibleBy20("93782639377588932980");
task.divisibleBy20("33612315332343");
task.divisibleBy20("3824566134312124240");
}
}
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ program for
Check if large number is divisible by 20
*/
class Divisibility
{
public: void divisibleBy20(string num)
{
// Assuming that number contains valid positive number
// Get the length of num
int n = num.length();
// Resultant indicator
int result = 0;
if (n == 0)
{
return;
}
if (n == 1 && num[0] == '0')
{
// When single digit and number is zero
result = 1;
}
else if (n > 1 && num[n - 1] == '0')
{
// Collect a number of last two digits
int value = ((num[n - 2] - '0') *10) + (num[n - 1] - '0');
if ((value % 20) == 0)
{
// When last 2 digit number is divisible by 20
result = 1;
}
}
if (result == 1)
{
cout << "\n Number " << num << " is divisible by 20";
}
else
{
cout << "\n Number " << num << " is not divisible by 20";
}
}
};
int main()
{
Divisibility *task = new Divisibility();
// Test Inputs
task->divisibleBy20("110");
task->divisibleBy20("93782639377588932980");
task->divisibleBy20("33612315332343");
task->divisibleBy20("3824566134312124240");
return 0;
}
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
// Include namespace system
using System;
/*
Csharp program for
Check if large number is divisible by 20
*/
public class Divisibility
{
public void divisibleBy20(String num)
{
// Assuming that number contains valid positive number
// Get the length of num
int n = num.Length;
// Resultant indicator
int result = 0;
if (n == 0)
{
return;
}
if (n == 1 && num[0] == '0')
{
// When single digit and number is zero
result = 1;
}
else if (n > 1 && num[n - 1] == '0')
{
// Collect a number of last two digits
int value = ((num[n - 2] - '0') * 10) + (num[n - 1] - '0');
if ((value % 20) == 0)
{
// When last 2 digit number is divisible by 20
result = 1;
}
}
if (result == 1)
{
Console.Write("\n Number " + num + " is divisible by 20");
}
else
{
Console.Write("\n Number " + num + " is not divisible by 20");
}
}
public static void Main(String[] args)
{
Divisibility task = new Divisibility();
// Test Inputs
task.divisibleBy20("110");
task.divisibleBy20("93782639377588932980");
task.divisibleBy20("33612315332343");
task.divisibleBy20("3824566134312124240");
}
}
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
package main
import "fmt"
/*
Go program for
Check if large number is divisible by 20
*/
func divisibleBy20(num string) {
// Assuming that number contains valid positive number
// Get the length of num
var n int = len(num)
// Resultant indicator
var result int = 0
if n == 0 {
return
}
if n == 1 && num[0] == '0' {
// When single digit and number is zero
result = 1
} else if n > 1 && num[n - 1] == '0' {
// Collect a number of last two digits
var value int = int(((num[n - 2] - '0') * 10) + (num[n - 1] - '0'))
if (value % 20) == 0 {
// When last 2 digit number is divisible by 20
result = 1
}
}
if result == 1 {
fmt.Print("\n Number ", num, " is divisible by 20")
} else {
fmt.Print("\n Number ", num, " is not divisible by 20")
}
}
func main() {
// Test Inputs
divisibleBy20("110")
divisibleBy20("93782639377588932980")
divisibleBy20("33612315332343")
divisibleBy20("3824566134312124240")
}
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
<?php
/*
Php program for
Check if large number is divisible by 20
*/
class Divisibility
{
public function divisibleBy20($num)
{
// Assuming that number contains valid positive number
// Get the length of num
$n = strlen($num);
// Resultant indicator
$result = 0;
if ($n == 0)
{
return;
}
if ($n == 1 && $num[0] == '0')
{
// When single digit and number is zero
$result = 1;
}
else if ($n > 1 && $num[$n - 1] == '0')
{
// Collect a number of last two digits
$value = ((ord($num[$n - 2]) - ord('0')) * 10) +
(ord($num[$n - 1]) - ord('0'));
if (($value % 20) == 0)
{
// When last 2 digit number is divisible by 20
$result = 1;
}
}
if ($result == 1)
{
echo("\n Number ".$num.
" is divisible by 20");
}
else
{
echo("\n Number ".$num.
" is not divisible by 20");
}
}
}
function main()
{
$task = new Divisibility();
// Test Inputs
$task->divisibleBy20("110");
$task->divisibleBy20("93782639377588932980");
$task->divisibleBy20("33612315332343");
$task->divisibleBy20("3824566134312124240");
}
main();
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
/*
Node JS program for
Check if large number is divisible by 20
*/
class Divisibility
{
divisibleBy20(num)
{
// Assuming that number contains valid positive number
// Get the length of num
var n = num.length;
// Resultant indicator
var result = 0;
if (n == 0)
{
return;
}
if (n == 1 && num.charAt(0) == '0')
{
// When single digit and number is zero
result = 1;
}
else if (n > 1 && num.charAt(n - 1) == '0')
{
// Collect a number of last two digits
var value = ((num.charCodeAt(n - 2) - '0'.charCodeAt(0)) * 10) +
(num.charCodeAt(n - 1) - '0'.charCodeAt(0));
if ((value % 20) == 0)
{
// When last 2 digit number is divisible by 20
result = 1;
}
}
if (result == 1)
{
process.stdout.write("\n Number " + num + " is divisible by 20");
}
else
{
process.stdout.write("\n Number " + num + " is not divisible by 20");
}
}
}
function main()
{
var task = new Divisibility();
// Test Inputs
task.divisibleBy20("110");
task.divisibleBy20("93782639377588932980");
task.divisibleBy20("33612315332343");
task.divisibleBy20("3824566134312124240");
}
main();
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
# Python 3 program for
# Check if large number is divisible by 20
class Divisibility :
def divisibleBy20(self, num) :
# Assuming that number contains valid positive number
# Get the length of num
n = len(num)
# Resultant indicator
result = 0
if (n == 0) :
return
if (n == 1 and num[0] == '0') :
# When single digit and number is zero
result = 1
elif (n > 1 and num[n - 1] == '0') :
# Collect a number of last two digits
value = ((ord(num[n - 2]) - ord('0')) * 10) + (
ord(num[n - 1]) - ord('0'))
if ((value % 20) == 0) :
# When last 2 digit number is divisible by 20
result = 1
if (result == 1) :
print("\n Number ", num ," is divisible by 20", end = "")
else :
print("\n Number ", num ," is not divisible by 20", end = "")
def main() :
task = Divisibility()
# Test Inputs
task.divisibleBy20("110")
task.divisibleBy20("93782639377588932980")
task.divisibleBy20("33612315332343")
task.divisibleBy20("3824566134312124240")
if __name__ == "__main__": main()
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
# Ruby program for
# Check if large number is divisible by 20
class Divisibility
def divisibleBy20(num)
# Assuming that number contains valid positive number
# Get the length of num
n = num.length
# Resultant indicator
result = 0
if (n == 0)
return
end
if (n == 1 && num[0] == '0')
# When single digit and number is zero
result = 1
elsif (n > 1 && num[n - 1] == '0')
# Collect a number of last two digits
value = ((num[n - 2].ord - '0'.ord) * 10) +
(num[n - 1].ord - '0'.ord)
if ((value % 20) == 0)
# When last 2 digit number is divisible by 20
result = 1
end
end
if (result == 1)
print("\n Number ", num ," is divisible by 20")
else
print("\n Number ", num ," is not divisible by 20")
end
end
end
def main()
task = Divisibility.new()
# Test Inputs
task.divisibleBy20("110")
task.divisibleBy20("93782639377588932980")
task.divisibleBy20("33612315332343")
task.divisibleBy20("3824566134312124240")
end
main()
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
import scala.collection.mutable._;
/*
Scala program for
Check if large number is divisible by 20
*/
class Divisibility()
{
def divisibleBy20(num: String): Unit = {
// Assuming that number contains valid positive number
// Get the length of num
var n: Int = num.length();
// Resultant indicator
var result: Int = 0;
if (n == 0)
{
return;
}
if (n == 1 && num.charAt(0) == '0')
{
// When single digit and number is zero
result = 1;
}
else if (n > 1 && num.charAt(n - 1) == '0')
{
// Collect a number of last two digits
var value: Int = ((num.charAt(n - 2).toInt - '0'.toInt) * 10)
+ (num.charAt(n - 1).toInt - '0'.toInt);
if ((value % 20) == 0)
{
// When last 2 digit number is divisible by 20
result = 1;
}
}
if (result == 1)
{
print("\n Number " + num + " is divisible by 20");
}
else
{
print("\n Number " + num + " is not divisible by 20");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Divisibility = new Divisibility();
// Test Inputs
task.divisibleBy20("110");
task.divisibleBy20("93782639377588932980");
task.divisibleBy20("33612315332343");
task.divisibleBy20("3824566134312124240");
}
}
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
import Foundation;
/*
Swift 4 program for
Check if large number is divisible by 20
*/
class Divisibility
{
func divisibleBy20(_ data: String)
{
let num = Array(data);
// Assuming that number contains valid positive number
// Get the length of num
let n: Int = num.count;
// Resultant indicator
var result: Int = 0;
if (n == 0)
{
return;
}
if (n == 1 && num[0] == "0")
{
// When single digit and number is zero
result = 1;
}
else if (n > 1 && num[n - 1] == "0")
{
// Collect a number of last two digits
let value: Int = ((Int(UnicodeScalar(String(num[n - 2]))!.value) -
Int(UnicodeScalar(String("0"))!.value)) * 10) +
(Int(UnicodeScalar(String(num[n - 1]))!.value) -
Int(UnicodeScalar(String("0"))!.value));
if ((value % 20) == 0)
{
// When last 2 digit number is divisible by 20
result = 1;
}
}
if (result == 1)
{
print("\n Number", data ,"is divisible by 20", terminator: "");
}
else
{
print("\n Number", data ,"is not divisible by 20", terminator: "");
}
}
}
func main()
{
let task: Divisibility = Divisibility();
// Test Inputs
task.divisibleBy20("110");
task.divisibleBy20("93782639377588932980");
task.divisibleBy20("33612315332343");
task.divisibleBy20("3824566134312124240");
}
main();
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
/*
Kotlin program for
Check if large number is divisible by 20
*/
class Divisibility
{
fun divisibleBy20(num: String): Unit
{
// Assuming that number contains valid positive number
// Get the length of num
val n: Int = num.length;
// Resultant indicator
var result: Int = 0;
if (n == 0)
{
return;
}
if (n == 1 && num.get(0) == '0')
{
// When single digit and number is zero
result = 1;
}
else if (n > 1 && num.get(n - 1) == '0')
{
// Collect a number of last two digits
val value: Int = ((num.get(n - 2).toInt() - '0'.toInt()) * 10) +
(num.get(n - 1).toInt() - '0'.toInt());
if ((value % 20) == 0)
{
// When last 2 digit number is divisible by 20
result = 1;
}
}
if (result == 1)
{
print("\n Number " + num + " is divisible by 20");
}
else
{
print("\n Number " + num + " is not divisible by 20");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Divisibility = Divisibility();
// Test Inputs
task.divisibleBy20("110");
task.divisibleBy20("93782639377588932980");
task.divisibleBy20("33612315332343");
task.divisibleBy20("3824566134312124240");
}
Output
Number 110 is not divisible by 20
Number 93782639377588932980 is divisible by 20
Number 33612315332343 is not divisible by 20
Number 3824566134312124240 is divisible by 20
Explanation and Output
Let's analyze the code and its output for the given test inputs.
For the input "110", the last two digits are "10". Since 10 is not divisible by 20, the number "110" is not divisible by 20.
For the input "93782639377588932980", the last two digits are "80". As we discussed earlier, 80 is divisible by 20. Therefore, the number "93782639377588932980" is divisible by 20.
For the input "33612315332343", the last two digits are "43". Since 43 is not divisible by 20, the number "33612315332343" is not divisible by 20.
For the input "3824566134312124240", the last two digits are "40". As 40 is divisible by 20, the number "3824566134312124240" is divisible by 20.
The output of the code matches our expectations for all the test cases.
Time Complexity
The time complexity of this code is O(1) because the operations performed are independent of the size of the input number. The code only checks the last two digits, making it a constant time operation.
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