Check that if large number is divisible by 10
The problem addressed in this context involves determining whether a large number, represented as a string, is divisible by 10 or not. Divisibility by 10 is an essential concept in mathematics, particularly in the realm of number theory and computer programming. When a number is divisible by 10, it means that the number can be evenly divided by 10 without leaving a remainder. In the context of computer programming, this is typically determined by examining the last digit of the number.
Problem Statement and Description
Given a large number in the form of a string, the objective is to ascertain whether the number is divisible by 10 or not. The approach for solving this problem centers around investigating the last digit of the number and verifying whether it is a zero. If the last digit is indeed zero, then the number is divisible by 10; otherwise, it is not.
Example
Let's consider the number "123450". The last digit is 0, which is indeed a zero. Therefore, the number 123450 is divisible by 10.
Idea to Solve the Problem
- Convert the given number to a string.
- Obtain the last digit of the string representation of the number.
- Check if the last digit is zero.
Test Cases
Input: "12293893483940"
Output: Divisible by 10
Explanation: The last digit is 0, which satisfies the condition.Input: "2342348234"
Output: Not divisible by 10
Explanation: The last digit is 4, which is not zero.Input: "23982310"
Output: Divisible by 10
Explanation: The last digit is 0, meeting the requirement.Input: "33282949823490"
Output: Divisible by 10
Explanation: The last digit is 0, which satisfies the condition.
Pseudocode
function isDivisibleBy10(num):
last_digit = num.charAt(num.length - 1)
if last_digit is '0':
return true
else:
return false
Algorithm Explanation
- Begin by obtaining the last digit of the provided number.
- Check whether the last digit is indeed a zero.
- If the condition is met, return true, signifying that the number is divisible by 10. Otherwise, return false.
Program Solution
// Java program for
// Check that if large number is divisible by 10
public class Divisibility
{
public void isDivisibleBy10(String num)
{
// Condition
// ➀ Last digit of given number must be zero
boolean result = false;
int n = num.length();
if (n >= 1 && num.charAt(n - 1) == '0')
{
// When of number last digit is zero
result = true;
}
if (result == true)
{
System.out.print(" Given number (" +
num + ") is divisible by 10\n");
}
else
{
System.out.print(" Given number (" +
num + ") is not divisible by 10\n");
}
}
public static void main(String[] args)
{
Divisibility task = new Divisibility();
// Test
task.isDivisibleBy10("12293893483940");
task.isDivisibleBy10("2342348234");
task.isDivisibleBy10("23982310");
task.isDivisibleBy10("33282949823490");
}
}
Output
Given number (12293893483940) is divisible by 10
Given number (2342348234) is not divisible by 10
Given number (23982310) is divisible by 10
Given number (33282949823490) is divisible by 10
// Include header file
#include <iostream>
#include <string>
using namespace std;
// C++ program for
// Check that if large number is divisible by 10
class Divisibility
{
public: void isDivisibleBy10(string num)
{
// Condition
// ➀ Last digit of given number must be zero
bool result = false;
int n = num.length();
if (n >= 1 && num[n - 1] == '0')
{
// When of number last digit is zero
result = true;
}
if (result == true)
{
cout << " Given number (" << num << ") is divisible by 10\n";
}
else
{
cout << " Given number (" << num << ") is not divisible by 10\n";
}
}
};
int main()
{
Divisibility *task = new Divisibility();
// Test
task->isDivisibleBy10("12293893483940");
task->isDivisibleBy10("2342348234");
task->isDivisibleBy10("23982310");
task->isDivisibleBy10("33282949823490");
return 0;
}
Output
Given number (12293893483940) is divisible by 10
Given number (2342348234) is not divisible by 10
Given number (23982310) is divisible by 10
Given number (33282949823490) is divisible by 10
// Include namespace system
using System;
// Csharp program for
// Check that if large number is divisible by 10
public class Divisibility
{
public void isDivisibleBy10(String num)
{
// Condition
// ➀ Last digit of given number must be zero
Boolean result = false;
int n = num.Length;
if (n >= 1 && num[n - 1] == '0')
{
// When of number last digit is zero
result = true;
}
if (result == true)
{
Console.Write(" Given number (" +
num + ") is divisible by 10\n");
}
else
{
Console.Write(" Given number (" +
num + ") is not divisible by 10\n");
}
}
public static void Main(String[] args)
{
Divisibility task = new Divisibility();
// Test
task.isDivisibleBy10("12293893483940");
task.isDivisibleBy10("2342348234");
task.isDivisibleBy10("23982310");
task.isDivisibleBy10("33282949823490");
}
}
Output
Given number (12293893483940) is divisible by 10
Given number (2342348234) is not divisible by 10
Given number (23982310) is divisible by 10
Given number (33282949823490) is divisible by 10
package main
import "fmt"
// Go program for
// Check that if large number is divisible by 10
func isDivisibleBy10(num string) {
// Condition
// ➀ Last digit of given number must be zero
var result bool = false
var n int = len(num)
if n >= 1 && num[n - 1] == '0' {
// When of number last digit is zero
result = true
}
if result == true {
fmt.Println(" Given number (",
num, ") is divisible by 10")
} else {
fmt.Println(" Given number (",
num, ") is not divisible by 10")
}
}
func main() {
// Test
isDivisibleBy10("12293893483940")
isDivisibleBy10("2342348234")
isDivisibleBy10("23982310")
isDivisibleBy10("33282949823490")
}
Output
Given number (12293893483940) is divisible by 10
Given number (2342348234) is not divisible by 10
Given number (23982310) is divisible by 10
Given number (33282949823490) is divisible by 10
<?php
// Php program for
// Check that if large number is divisible by 10
class Divisibility
{
public function isDivisibleBy10($num)
{
// Condition
// ➀ Last digit of given number must be zero
$result = false;
$n = strlen($num);
if ($n >= 1 && $num[$n - 1] == '0')
{
// When of number last digit is zero
$result = true;
}
if ($result == true)
{
echo(" Given number (".$num.
") is divisible by 10\n");
}
else
{
echo(" Given number (".$num.
") is not divisible by 10\n");
}
}
}
function main()
{
$task = new Divisibility();
// Test
$task->isDivisibleBy10("12293893483940");
$task->isDivisibleBy10("2342348234");
$task->isDivisibleBy10("23982310");
$task->isDivisibleBy10("33282949823490");
}
main();
Output
Given number (12293893483940) is divisible by 10
Given number (2342348234) is not divisible by 10
Given number (23982310) is divisible by 10
Given number (33282949823490) is divisible by 10
// Node JS program for
// Check that if large number is divisible by 10
class Divisibility
{
isDivisibleBy10(num)
{
// Condition
// ➀ Last digit of given number must be zero
var result = false;
var n = num.length;
if (n >= 1 && num.charAt(n - 1) == '0')
{
// When of number last digit is zero
result = true;
}
if (result == true)
{
console.log(" Given number (" +
num + ") is divisible by 10");
}
else
{
console.log(" Given number (" +
num + ") is not divisible by 10");
}
}
}
function main()
{
var task = new Divisibility();
// Test
task.isDivisibleBy10("12293893483940");
task.isDivisibleBy10("2342348234");
task.isDivisibleBy10("23982310");
task.isDivisibleBy10("33282949823490");
}
main();
Output
Given number (12293893483940) is divisible by 10
Given number (2342348234) is not divisible by 10
Given number (23982310) is divisible by 10
Given number (33282949823490) is divisible by 10
# Python 3 program for
# Check that if large number is divisible by 10
class Divisibility :
def isDivisibleBy10(self, num) :
# Condition
# ➀ Last digit of given number must be zero
result = False
n = len(num)
if (n >= 1 and num[n - 1] == '0') :
# When of number last digit is zero
result = True
if (result == True) :
print(" Given number (", num ,") is divisible by 10")
else :
print(" Given number (", num ,") is not divisible by 10")
def main() :
task = Divisibility()
# Test
task.isDivisibleBy10("12293893483940")
task.isDivisibleBy10("2342348234")
task.isDivisibleBy10("23982310")
task.isDivisibleBy10("33282949823490")
if __name__ == "__main__": main()
Output
Given number ( 12293893483940 ) is divisible by 10
Given number ( 2342348234 ) is not divisible by 10
Given number ( 23982310 ) is divisible by 10
Given number ( 33282949823490 ) is divisible by 10
# Ruby program for
# Check that if large number is divisible by 10
class Divisibility
def isDivisibleBy10(num)
# Condition
# ➀ Last digit of given number must be zero
result = false
n = num.length
if (n >= 1 && num[n - 1] == '0')
# When of number last digit is zero
result = true
end
if (result == true)
print(" Given number (", num ,") is divisible by 10", "\n")
else
print(" Given number (", num ,") is not divisible by 10", "\n")
end
end
end
def main()
task = Divisibility.new()
# Test
task.isDivisibleBy10("12293893483940")
task.isDivisibleBy10("2342348234")
task.isDivisibleBy10("23982310")
task.isDivisibleBy10("33282949823490")
end
main()
Output
Given number (12293893483940) is divisible by 10
Given number (2342348234) is not divisible by 10
Given number (23982310) is divisible by 10
Given number (33282949823490) is divisible by 10
import scala.collection.mutable._;
// Scala program for
// Check that if large number is divisible by 10
class Divisibility()
{
def isDivisibleBy10(num: String): Unit = {
// Condition
// ➀ Last digit of given number must be zero
var result: Boolean = false;
var n: Int = num.length();
if (n >= 1 && num.charAt(n - 1) == '0')
{
// When of number last digit is zero
result = true;
}
if (result == true)
{
println(" Given number (" + num + ") is divisible by 10");
}
else
{
println(" Given number (" + num + ") is not divisible by 10");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Divisibility = new Divisibility();
// Test
task.isDivisibleBy10("12293893483940");
task.isDivisibleBy10("2342348234");
task.isDivisibleBy10("23982310");
task.isDivisibleBy10("33282949823490");
}
}
Output
Given number (12293893483940) is divisible by 10
Given number (2342348234) is not divisible by 10
Given number (23982310) is divisible by 10
Given number (33282949823490) is divisible by 10
import Foundation;
// Swift 4 program for
// Check that if large number is divisible by 10
class Divisibility
{
func isDivisibleBy10(_ num: String)
{
// Condition
// ➀ Last digit of given number must be zero
var result: Bool = false;
let n: Int = num.count;
if (n >= 1 && Array(num)[n - 1] == "0")
{
// When of number last digit is zero
result = true;
}
if (result == true)
{
print(" Given number (", num ,") is divisible by 10");
}
else
{
print(" Given number (", num ,") is not divisible by 10");
}
}
}
func main()
{
let task: Divisibility = Divisibility();
// Test
task.isDivisibleBy10("12293893483940");
task.isDivisibleBy10("2342348234");
task.isDivisibleBy10("23982310");
task.isDivisibleBy10("33282949823490");
}
main();
Output
Given number ( 12293893483940 ) is divisible by 10
Given number ( 2342348234 ) is not divisible by 10
Given number ( 23982310 ) is divisible by 10
Given number ( 33282949823490 ) is divisible by 10
// Kotlin program for
// Check that if large number is divisible by 10
class Divisibility
{
fun isDivisibleBy10(num: String): Unit
{
// Condition
// ➀ Last digit of given number must be zero
var result: Boolean = false;
val n: Int = num.length;
if (n >= 1 && num.get(n - 1) == '0')
{
// When of number last digit is zero
result = true;
}
if (result == true)
{
println(" Given number (" +
num + ") is divisible by 10");
}
else
{
println(" Given number (" +
num + ") is not divisible by 10");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Divisibility = Divisibility();
// Test
task.isDivisibleBy10("12293893483940");
task.isDivisibleBy10("2342348234");
task.isDivisibleBy10("23982310");
task.isDivisibleBy10("33282949823490");
}
Output
Given number (12293893483940) is divisible by 10
Given number (2342348234) is not divisible by 10
Given number (23982310) is divisible by 10
Given number (33282949823490) is divisible by 10
Time Complexity
The time complexity of this code is O(1) since it involves checking only the last digit of the given number, a process that can be completed 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