Check that if large number is divisible by 8
The given problem is about checking whether a large number, represented as a string, is divisible by 8 or not. Divisibility by 8 means that the number can be evenly divided by 8 without any remainder. We are given a Java code that implements a function to perform this check on large numbers represented as strings.
Test Condition
-
If the number has only one digit and the digit is either '0' or '8', then it is divisible by 8. This is because 0 and 8 are the only single-digit numbers that are divisible by 8.
-
If the number has two digits, it forms an auxiliary number by taking the last two digits of the given number and converting them to an integer. If this auxiliary number is divisible by 8, then the original number is also divisible by 8. This property is based on the divisibility rule for 8, which states that a number is divisible by 8 if the last three digits of the number are divisible by 8.
-
If the number has more than two digits, it forms an auxiliary number by taking the last three digits of the given number and converting them to an integer. If this auxiliary number is divisible by 8, then the original number is also divisible by 8. This is again based on the divisibility rule for 8, which states that a number is divisible by 8 if the last three digits of the number are divisible by 8.
Problem Statement
The task is to write a program that takes a string representing a large number as input and determines whether the number is divisible by 8 or not. The program should output a message stating whether the number is divisible by 8 or not.
Explanation with Suitable Example
Consider a large number represented as a string "5654464564564564512320". To check if this number is divisible by 8, we need to look at the last three digits, which are "320". Since "320" is divisible by 8, the entire number "5654464564564564512320" is also divisible by 8.
Standard Pseudocode
function isDivisibleBy8(num: string) -> boolean
result = false
n = length of num
if n == 1 AND (num[0] == '0' OR num[0] == '8')
result = true
else if n == 2
auxiliary = convertToInt(num[0]) * 10 + convertToInt(num[1])
if auxiliary % 8 == 0
result = true
else if n > 2
auxiliary = (convertToInt(num[n-3]) * 10 + convertToInt(num[n-2])) * 10 + convertToInt(num[n-1])
if auxiliary % 8 == 0
result = true
return result
Algorithm Explanation
- The function
isDivisibleBy8
takes a stringnum
as input and initializes a boolean variableresult
to store the final result. - It calculates the length of the input number
num
and stores it in the variablen
. - If
n
is equal to 1 and the first character of the number is either '0' or '8', then the number is divisible by 8, and theresult
is set to true. - If
n
is equal to 2, it forms a two-digit numberauxiliary
by converting the first two characters ofnum
to integers and multiplying the first digit by 10 and adding the second digit. Ifauxiliary
is divisible by 8, then theresult
is set to true. - If
n
is greater than 2, it forms a three-digit numberauxiliary
by converting the last three characters ofnum
to integers. Ifauxiliary
is divisible by 8, then theresult
is set to true. - The function returns the final result.
Program Solution
// Java program for
// Check that if large number is divisible by 8
public class Divisibility
{
public void isDivisibleBy8(String num)
{
boolean result = false;
int n = num.length();
int auxiliary = 0;
if (n == 1 && (num.charAt(0) == '0' ||
num.charAt(0) == '8'))
{
result = true;
}
else if (n == 2)
{
auxiliary = num.charAt(n - 2) * 10 +
num.charAt(n - 1);
if (auxiliary % 8 == 0)
{
result = true;
}
}
else if (n > 2)
{
// Get last three digit number
auxiliary = ((num.charAt(n - 3) - 48) * 10 +
(num.charAt(n - 2) - 48)) * 10 +
(num.charAt(n - 1) - 48);
if (auxiliary % 8 == 0)
{
result = true;
}
}
if (result)
{
System.out.println(" Given number (" +
num + ") is divisible by 8");
}
else
{
System.out.println(" Given number (" +
num + ") is not divisible by 8");
}
}
public static void main(String[] args)
{
Divisibility task = new Divisibility();
// Test
task.isDivisibleBy8("45645645");
task.isDivisibleBy8("65765345345");
task.isDivisibleBy8("5654464564564564512320");
task.isDivisibleBy8("56456423434545645567567567867824");
task.isDivisibleBy8("9999");
}
}
Output
Given number (45645645) is not divisible by 8
Given number (65765345345) is not divisible by 8
Given number (5654464564564564512320) is divisible by 8
Given number (56456423434545645567567567867824) is divisible by 8
Given number (9999) is not divisible by 8
// Include header file
#include <iostream>
#include <string>
using namespace std;
// C++ program for
// Check that if large number is divisible by 8
class Divisibility
{
public: void isDivisibleBy8(string num)
{
bool result = false;
int n = num.length();
int auxiliary = 0;
if (n == 1 && (num[0] == '0' || num[0] == '8'))
{
result = true;
}
else if (n == 2)
{
auxiliary = num[n - 2] *10 + num[n - 1];
if (auxiliary % 8 == 0)
{
result = true;
}
}
else if (n > 2)
{
// Get last three digit number
auxiliary = ((num[n - 3] - 48) *10 +
(num[n - 2] - 48)) *10 + (num[n - 1] - 48);
if (auxiliary % 8 == 0)
{
result = true;
}
}
if (result)
{
cout << " Given number ("
<< num << ") is divisible by 8" << endl;
}
else
{
cout << " Given number ("
<< num << ") is not divisible by 8" << endl;
}
}
};
int main()
{
Divisibility *task = new Divisibility();
// Test
task->isDivisibleBy8("45645645");
task->isDivisibleBy8("65765345345");
task->isDivisibleBy8("5654464564564564512320");
task->isDivisibleBy8("56456423434545645567567567867824");
task->isDivisibleBy8("9999");
return 0;
}
Output
Given number (45645645) is not divisible by 8
Given number (65765345345) is not divisible by 8
Given number (5654464564564564512320) is divisible by 8
Given number (56456423434545645567567567867824) is divisible by 8
Given number (9999) is not divisible by 8
// Include namespace system
using System;
// Csharp program for
// Check that if large number is divisible by 8
public class Divisibility
{
public void isDivisibleBy8(String num)
{
Boolean result = false;
int n = num.Length;
int auxiliary = 0;
if (n == 1 && (num[0] == '0' ||
num[0] == '8'))
{
result = true;
}
else if (n == 2)
{
auxiliary = num[n - 2] * 10 + num[n - 1];
if (auxiliary % 8 == 0)
{
result = true;
}
}
else if (n > 2)
{
// Get last three digit number
auxiliary = ((num[n - 3] - 48) * 10 +
(num[n - 2] - 48)) * 10 + (num[n - 1] - 48);
if (auxiliary % 8 == 0)
{
result = true;
}
}
if (result)
{
Console.WriteLine(" Given number (" +
num + ") is divisible by 8");
}
else
{
Console.WriteLine(" Given number (" +
num + ") is not divisible by 8");
}
}
public static void Main(String[] args)
{
Divisibility task = new Divisibility();
// Test
task.isDivisibleBy8("45645645");
task.isDivisibleBy8("65765345345");
task.isDivisibleBy8("5654464564564564512320");
task.isDivisibleBy8("56456423434545645567567567867824");
task.isDivisibleBy8("9999");
}
}
Output
Given number (45645645) is not divisible by 8
Given number (65765345345) is not divisible by 8
Given number (5654464564564564512320) is divisible by 8
Given number (56456423434545645567567567867824) is divisible by 8
Given number (9999) is not divisible by 8
package main
import "fmt"
// Go program for
// Check that if large number is divisible by 8
func isDivisibleBy8(num string) {
var result bool = false
var n int = len(num)
var auxiliary int = 0
if n == 1 && (num[0] == '0' || num[0] == '8') {
result = true
} else if n == 2 {
auxiliary = int(num[n - 2]) * 10 +int( num[n - 1])
if auxiliary % 8 == 0 {
result = true
}
} else if n > 2 {
// Get last three digit number
auxiliary = ((int(num[n - 3]) - 48) * 10 +
(int(num[n - 2]) - 48)) * 10 + (int(num[n - 1]) - 48)
if auxiliary % 8 == 0 {
result = true
}
}
if result {
fmt.Println(" Given number (", num, ") is divisible by 8")
} else {
fmt.Println(" Given number (", num, ") is not divisible by 8")
}
}
func main() {
// Test
isDivisibleBy8("45645645")
isDivisibleBy8("65765345345")
isDivisibleBy8("5654464564564564512320")
isDivisibleBy8("56456423434545645567567567867824")
isDivisibleBy8("9999")
}
Output
Given number (45645645) is not divisible by 8
Given number (65765345345) is not divisible by 8
Given number (5654464564564564512320) is divisible by 8
Given number (56456423434545645567567567867824) is divisible by 8
Given number (9999) is not divisible by 8
<?php
// Php program for
// Check that if large number is divisible by 8
class Divisibility
{
public function isDivisibleBy8($num)
{
$result = false;
$n = strlen($num);
$auxiliary = 0;
if ($n == 1 && ($num[0] == '0' ||
$num[0] == '8'))
{
$result = true;
}
else if ($n == 2)
{
$auxiliary = ord($num[$n - 2]) * 10 +
ord($num[$n - 1]);
if ($auxiliary % 8 == 0)
{
$result = true;
}
}
else if ($n > 2)
{
// Get last three digit number
$auxiliary = ((ord($num[$n - 3]) - 48) * 10 +
(ord($num[$n - 2]) - 48)) * 10 +
(ord($num[$n - 1]) - 48);
if ($auxiliary % 8 == 0)
{
$result = true;
}
}
if ($result)
{
echo(" Given number (".$num.
") is divisible by 8\n");
}
else
{
echo(" Given number (".$num.
") is not divisible by 8\n");
}
}
}
function main()
{
$task = new Divisibility();
// Test
$task->isDivisibleBy8("45645645");
$task->isDivisibleBy8("65765345345");
$task->isDivisibleBy8("5654464564564564512320");
$task->isDivisibleBy8("56456423434545645567567567867824");
$task->isDivisibleBy8("9999");
}
main();
Output
Given number (45645645) is not divisible by 8
Given number (65765345345) is not divisible by 8
Given number (5654464564564564512320) is divisible by 8
Given number (56456423434545645567567567867824) is divisible by 8
Given number (9999) is not divisible by 8
// Node JS program for
// Check that if large number is divisible by 8
class Divisibility
{
isDivisibleBy8(num)
{
var result = false;
var n = num.length;
var auxiliary = 0;
if (n == 1 && (num.charAt(0) == '0' ||
num.charAt(0) == '8'))
{
result = true;
}
else if (n == 2)
{
auxiliary = num.charCodeAt(n - 2) * 10 +
num.charCodeAt(n - 1);
if (auxiliary % 8 == 0)
{
result = true;
}
}
else if (n > 2)
{
// Get last three digit number
auxiliary = ((num.charCodeAt(n - 3) * 10 +
(num.charCodeAt(n - 2) - 48)) * 10 +
(num.charCodeAt(n - 1) - 48));
if (auxiliary % 8 == 0)
{
result = true;
}
}
if (result)
{
console.log(" Given number (" + num + ") is divisible by 8");
}
else
{
console.log(" Given number (" + num + ") is not divisible by 8");
}
}
}
function main()
{
var task = new Divisibility();
// Test
task.isDivisibleBy8("45645645");
task.isDivisibleBy8("65765345345");
task.isDivisibleBy8("5654464564564564512320");
task.isDivisibleBy8("56456423434545645567567567867824");
task.isDivisibleBy8("9999");
}
main();
Output
Given number (45645645) is not divisible by 8
Given number (65765345345) is not divisible by 8
Given number (5654464564564564512320) is divisible by 8
Given number (56456423434545645567567567867824) is divisible by 8
Given number (9999) is not divisible by 8
# Python 3 program for
# Check that if large number is divisible by 8
class Divisibility :
def isDivisibleBy8(self, num) :
result = False
n = len(num)
auxiliary = 0
if (n == 1 and(num[0] == '0'
or num[0] == '8')) :
result = True
elif (n == 2) :
auxiliary = ord(num[n - 2]) * 10 + ord(num[n - 1])
if (auxiliary % 8 == 0) :
result = True
elif (n > 2) :
# Get last three digit number
auxiliary = ((ord(num[n - 3]) - 48) * 10 +
(ord(num[n - 2]) - 48)) * 10 + (ord(num[n - 1]) - 48)
if (auxiliary % 8 == 0) :
result = True
if (result) :
print(" Given number (", num ,") is divisible by 8")
else :
print(" Given number (", num ,") is not divisible by 8")
def main() :
task = Divisibility()
# Test
task.isDivisibleBy8("45645645")
task.isDivisibleBy8("65765345345")
task.isDivisibleBy8("5654464564564564512320")
task.isDivisibleBy8("56456423434545645567567567867824")
task.isDivisibleBy8("9999")
if __name__ == "__main__": main()
Output
Given number ( 45645645 ) is not divisible by 8
Given number ( 65765345345 ) is not divisible by 8
Given number ( 5654464564564564512320 ) is divisible by 8
Given number ( 56456423434545645567567567867824 ) is divisible by 8
Given number ( 9999 ) is not divisible by 8
# Ruby program for
# Check that if large number is divisible by 8
class Divisibility
def isDivisibleBy8(num)
result = false
n = num.length
auxiliary = 0
if (n == 1 && (num[0] == '0' || num[0] == '8'))
result = true
elsif (n == 2)
auxiliary = num[n - 2].ord * 10 + num[n - 1].ord
if (auxiliary % 8 == 0)
result = true
end
elsif (n > 2)
# Get last three digit number
auxiliary = ((num[n - 3].ord - 48) * 10 +
(num[n - 2].ord - 48)) * 10 +
(num[n - 1].ord - 48)
if (auxiliary % 8 == 0)
result = true
end
end
if (result)
print(" Given number (", num ,") is divisible by 8", "\n")
else
print(" Given number (", num ,") is not divisible by 8", "\n")
end
end
end
def main()
task = Divisibility.new()
# Test
task.isDivisibleBy8("45645645")
task.isDivisibleBy8("65765345345")
task.isDivisibleBy8("5654464564564564512320")
task.isDivisibleBy8("56456423434545645567567567867824")
task.isDivisibleBy8("9999")
end
main()
Output
Given number (45645645) is not divisible by 8
Given number (65765345345) is not divisible by 8
Given number (5654464564564564512320) is divisible by 8
Given number (56456423434545645567567567867824) is divisible by 8
Given number (9999) is not divisible by 8
import scala.collection.mutable._;
// Scala program for
// Check that if large number is divisible by 8
class Divisibility()
{
def isDivisibleBy8(num: String): Unit = {
var result: Boolean = false;
var n: Int = num.length();
var auxiliary: Int = 0;
if (n == 1 && (num.charAt(0) == '0' ||
num.charAt(0) == '8'))
{
result = true;
}
else if (n == 2)
{
auxiliary = num.charAt(n - 2).toInt * 10 +
num.charAt(n - 1).toInt;
if (auxiliary % 8 == 0)
{
result = true;
}
}
else if (n > 2)
{
// Get last three digit number
auxiliary = ((num.charAt(n - 3).toInt - 48) * 10 +
(num.charAt(n - 2).toInt - 48)) * 10 +
(num.charAt(n - 1).toInt - 48);
if (auxiliary % 8 == 0)
{
result = true;
}
}
if (result)
{
println(" Given number (" + num + ") is divisible by 8");
}
else
{
println(" Given number (" + num + ") is not divisible by 8");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Divisibility = new Divisibility();
// Test
task.isDivisibleBy8("45645645");
task.isDivisibleBy8("65765345345");
task.isDivisibleBy8("5654464564564564512320");
task.isDivisibleBy8("56456423434545645567567567867824");
task.isDivisibleBy8("9999");
}
}
Output
Given number (45645645) is not divisible by 8
Given number (65765345345) is not divisible by 8
Given number (5654464564564564512320) is divisible by 8
Given number (56456423434545645567567567867824) is divisible by 8
Given number (9999) is not divisible by 8
import Foundation;
// Swift 4 program for
// Check that if large number is divisible by 8
class Divisibility
{
func isDivisibleBy8(_ t: String)
{
let num = Array(t);
var result: Bool = false;
let n: Int = num.count;
var auxiliary: Int = 0;
if (n == 1 && (num[0] == "0" || num[0] == "8"))
{
result = true;
}
else if (n == 2)
{
auxiliary = Int(UnicodeScalar(String(num[n - 2]))!.value) * 10 +
Int(UnicodeScalar(String(num[n - 1]))!.value);
if (auxiliary % 8 == 0)
{
result = true;
}
}
else if (n > 2)
{
// Get last three digit number
auxiliary = ((Int(UnicodeScalar(String(num[n - 3]))!.value) - 48) *
10 + (Int(UnicodeScalar(String(num[n - 2]))!.value) -
48)) * 10 + (Int(UnicodeScalar(String(
num[n - 1]))!.value) - 48);
if (auxiliary % 8 == 0)
{
result = true;
}
}
if (result)
{
print(" Given number (", t ,") is divisible by 8");
}
else
{
print(" Given number (", t ,") is not divisible by 8");
}
}
}
func main()
{
let task: Divisibility = Divisibility();
// Test
task.isDivisibleBy8("45645645");
task.isDivisibleBy8("65765345345");
task.isDivisibleBy8("5654464564564564512320");
task.isDivisibleBy8("56456423434545645567567567867824");
task.isDivisibleBy8("9999");
}
main();
Output
Given number ( 45645645 ) is not divisible by 8
Given number ( 65765345345 ) is not divisible by 8
Given number ( 5654464564564564512320 ) is divisible by 8
Given number ( 56456423434545645567567567867824 ) is divisible by 8
Given number ( 9999 ) is not divisible by 8
// Kotlin program for
// Check that if large number is divisible by 8
class Divisibility
{
fun isDivisibleBy8(num: String): Unit
{
var result: Boolean = false;
val n: Int = num.length;
var auxiliary: Int ;
if (n == 1 && (num.get(0) == '0' ||
num.get(0) == '8'))
{
result = true;
}
else if (n == 2)
{
auxiliary = num.get(n - 2).toInt() * 10 +
num.get(n - 1).toInt();
if (auxiliary % 8 == 0)
{
result = true;
}
}
else if (n > 2)
{
// Get last three digit number
auxiliary = ((num.get(n - 3).toInt() - 48) * 10 +
(num.get(n - 2).toInt() - 48)) * 10 +
(num.get(n - 1).toInt() - 48);
if (auxiliary % 8 == 0)
{
result = true;
}
}
if (result)
{
println(" Given number (" + num + ") is divisible by 8");
}
else
{
println(" Given number (" + num + ") is not divisible by 8");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Divisibility = Divisibility();
// Test
task.isDivisibleBy8("45645645");
task.isDivisibleBy8("65765345345");
task.isDivisibleBy8("5654464564564564512320");
task.isDivisibleBy8("56456423434545645567567567867824");
task.isDivisibleBy8("9999");
}
Output
Given number (45645645) is not divisible by 8
Given number (65765345345) is not divisible by 8
Given number (5654464564564564512320) is divisible by 8
Given number (56456423434545645567567567867824) is divisible by 8
Given number (9999) is not divisible by 8
Resultant Output Explanation
For each test case in the main
function, the isDivisibleBy8
function is called with
the given input number represented as a string. The function checks whether the number is divisible by 8 or not
and prints the appropriate message accordingly.
- For the input "45645645", the number is not divisible by 8.
- For the input "65765345345", the number is not divisible by 8.
- For the input "5654464564564564512320", the number is divisible by 8.
- For the input "56456423434545645567567567867824", the number is divisible by 8.
- For the input "9999", the number is not divisible by 8.
Time Complexity of Code
Let's analyze the time complexity of the code:
- Calculating the length of the input number
num
takes O(n) time. - The conditions and calculations for determining divisibility by 8 take constant time for each test case.
Therefore, the overall time complexity of the code is O(n), where n is the length of the input number
num
. The code has a linear time complexity with respect to the size of the input number.
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