Given a number, the task is to check if number is divisible by 20. The input number may be large and it may not be possible to store long long int and it may be very large number then we use the string.
Examples:
Input : 7575680
Output : Yes
Input : 987985865687690
Output : No
A number is divisible by 20 if it is divisible by 5 and 4. We can check if a number is divisible by 4 by checking if last two digits are divisible by 4. We can check for divisibility by 5 by checking last digit. Also, if the last digit of a number is zero and the second last digit is a multiple of 2 then the number is divisible by 20.
C++
#include <iostream>
using namespace std;
bool divisibleBy20(string num)
{
int lastTwoDigits = stoi(num.substr(num.length() - 2,
num.length() - 1));
return ((lastTwoDigits % 5 == 0) &&
(lastTwoDigits % 4 == 0));
}
int main()
{
string num = "63284689320" ;
if (divisibleBy20(num))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static Boolean divisibleBy20(String num)
{
int lastTwoDigits = Integer.parseInt(num.substring(num.length() - 2 ,
num.length() ));
return ((lastTwoDigits % 5 == 0 ) &&
(lastTwoDigits % 4 == 0 ));
}
public static void main (String[] args)
{
String num = "63284689320" ;
if (divisibleBy20(num) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import math
def divisibleBy20(num):
lastTwoDigits = int (num[ - 2 :])
return ((lastTwoDigits % 5 = = 0 and
lastTwoDigits % 4 = = 0 ))
num = "63284689320"
if (divisibleBy20(num) = = True ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Text;
class GFG
{
static bool divisibleBy20(String num)
{
int lastTwoDigits = Int32.Parse(num.Substring(2));
return ((lastTwoDigits % 5 == 0) &&
(lastTwoDigits % 4 == 0));
}
static public void Main ()
{
String num = "63284689320" ;
if (divisibleBy20(num) == true )
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function divisibleBy20( $num )
{
$lastTwoDigits = intval ( substr ( $num ,
( strlen ( $num ) - 2), 2));
return (( $lastTwoDigits % 5 == 0) &&
( $lastTwoDigits % 4 == 0));
}
$num = "63284689320" ;
if (divisibleBy20( $num ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function divisibleBy20(num)
{
let lastTwoDigits = parseInt(num.slice(-2, num.length))
console.log(num.slice(-2, 1))
return ((lastTwoDigits % 5 == 0) &&
(lastTwoDigits % 4 == 0))
}
let num = "63284689320" ;
if (divisibleBy20(num))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity: O(1)
Auxiliary space: O(1)
Method: Checking given number is divisible by 20 or not by using modulo division operator “%”.
C++
#include <iostream>
using namespace std;
int main() {
long int num = 987985865687690;
if (num % 20 == 0) {
cout<< "Yes" ;
}
else {
cout<< "No" ;
}
return 0;
}
|
Java
import java.math.BigInteger;
class GFG {
public static void main(String[] args)
{
BigInteger num = new BigInteger( "987985865687690" );
if (num.mod( new BigInteger( "20" ))
.equals( new BigInteger( "0" ))) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
n = 987985865687690
if int (n) % 20 = = 0 :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
static public void Main()
{
long num = 987985865687690;
if (num % 20 == 0) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
}
}
|
Javascript
<script>
var n = 987985865687690
if (n % 20 == 0)
document.write( "Yes" )
else
document.write( "No" )
</script>
|
PHP
<?php
$num = 987985865687690;
if ( ( $num ) % 20 == 0)
echo "Yes" ;
else
echo "No" ;
?>
|
Time complexity: O(1) it is performing constant operations
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!