Given a number, the task is to check if a number is divisible by 2, 3, and 5 or not. The input number may be large and it may not be possible to store even if we use long long int, so the number is taken as a string.
Examples:
Input : str = "725"
Output : NO
Input : str = "263730746028908374890"
Output : YES
A number is divisible by 2 if it’s right most digit is even and also a number is divisible by 5 if it’s right most digit is zero or five.
So, from above two observations, one can conclude that for the number to be divisible by both 2 and 5 the rightmost digit of the number must be zero.
Now, a number is divisible by 3 if the sum of its digits is divisible by three.
Therefore, a number will be divisible by all of 2, 3, and 5 if:
- Its rightmost digit is zero.
- Sum of all of its digits is divisible by 3.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int SumOfDigits(string str, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += ( int )(str[i] - '0' );
return sum;
}
bool Divisible(string str, int n)
{
if (SumOfDigits(str, n) % 3 == 0 and str[n - 1] == '0' )
return true ;
return false ;
}
int main()
{
string str = "263730746028908374890" ;
int n = str.size();
if (Divisible(str, n))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
C
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int SumOfDigits( char str[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += ( int )(str[i] - '0' );
return sum;
}
bool Divisible( char str[], int n)
{
if (SumOfDigits(str, n) % 3 == 0 && str[n - 1] == '0' )
return true ;
return false ;
}
int main()
{
char str[] = "263730746028908374890" ;
int n = strlen (str);
if (Divisible(str, n))
printf ( "YES" );
else
printf ( "NO" );
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int SumOfDigits(String str,
int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += ( int )(str.charAt(i) - '0' );
return sum;
}
static boolean Divisible(String str,
int n)
{
if (SumOfDigits(str, n) % 3 == 0 &&
str.charAt(n - 1 ) == '0' )
return true ;
return false ;
}
public static void main(String []args)
{
String str = "263730746028908374890" ;
int n = str.length();
if (Divisible(str, n))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python 3
def SumOfDigits( str , n):
sum = 0
for i in range ( 0 ,n):
sum + = int ( ord ( str [i] ) - ord ( '0' ))
return sum
def Divisible( str , n):
if ((SumOfDigits( str , n) % 3 = = 0 and
str [n - 1 ] = = '0' )):
return True
return False
if __name__ = = "__main__" :
str = "263730746028908374890"
n = len ( str )
if (Divisible( str , n)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static int SumOfDigits(String str,
int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += ( int )(str[i] - '0' );
return sum;
}
static bool Divisible(String str, int n)
{
if (SumOfDigits(str, n) % 3 == 0 &&
str[n - 1] == '0' )
return true ;
return false ;
}
public static void Main()
{
String str = "263730746028908374890" ;
int n = str.Length;
if (Divisible(str, n))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
function SumOfDigits( $str , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += (int)( $str [ $i ] - '0' );
return $sum ;
}
function Divisible( $str , $n )
{
if (SumOfDigits( $str , $n ) % 3 == 0 and
$str [ $n - 1] == '0' )
return true;
return false;
}
$str = "263730746028908374890" ;
$n = strlen ( $str );
if (Divisible( $str , $n ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function SumOfDigits(str, n)
{
var sum = 0;
for ( var i = 0; i < n; i++)
sum += (str.charAt(i) - '0' );
return sum;
}
function Divisible(str, n)
{
if (SumOfDigits(str, n) % 3 == 0 &&
str.charAt(n - 1) == '0' )
return true ;
return false ;
}
var str = "263730746028908374890" ;
var n = str.length;
if (Divisible(str, n))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(n), where n is the size of the given string str
Auxiliary Space: O(1), as no extra space is required
Another approach:
Approach:
1. Check if the last digit of the number is even. If it is, then the number is divisible by 2.
2. Check if the sum of the digits of the number is divisible by 3. If it is, then the number is divisible by 3.
3. Check if the last digit of the number is 0 or 5. If it is, then the number is divisible by 5.
C++
#include <iostream>
using namespace std;
int main()
{
long long int n = 1234567890123456789;
int sum = 0;
int last_digit = n % 10;
if (last_digit % 2 == 0) {
cout << n << " is divisible by 2\n" ;
}
else {
cout << n << " is not divisible by 2\n" ;
}
long long int temp_n = n;
while (temp_n > 0) {
sum += temp_n % 10;
temp_n /= 10;
}
if (sum % 3 == 0) {
cout << n << " is divisible by 3\n" ;
}
else {
cout << n << " is not divisible by 3\n" ;
}
if (last_digit == 0 || last_digit == 5) {
cout << n << " is divisible by 5\n" ;
}
else {
cout << n << " is not divisible by 5\n" ;
}
return 0;
}
|
C
#include <stdio.h>
int main() {
long long int n = 1234567890123456789;
int sum = 0;
int last_digit = n % 10;
if (last_digit % 2 == 0) {
printf ( "%lld is divisible by 2\n" , n);
} else {
printf ( "%lld is not divisible by 2\n" , n);
}
while (n > 0) {
sum += n % 10;
n /= 10;
}
if (sum % 3 == 0) {
printf ( "%lld is divisible by 3\n" , n);
} else {
printf ( "%lld is not divisible by 3\n" , n);
}
if (last_digit == 0 || last_digit == 5) {
printf ( "%lld is divisible by 5\n" , n);
} else {
printf ( "%lld is not divisible by 5\n" , n);
}
return 0;
}
|
Java
public class Main {
public static void main(String[] args) {
long n = 1234567890123456789L;
int sum = 0 ;
int last_digit = ( int ) n % 10 ;
if (last_digit % 2 == 0 ) {
System.out.println(n + " is divisible by 2" );
} else {
System.out.println(n + " is not divisible by 2" );
}
long temp_n = n;
while (temp_n > 0 ) {
sum += temp_n % 10 ;
temp_n /= 10 ;
}
if (sum % 3 == 0 ) {
System.out.println(n + " is divisible by 3" );
} else {
System.out.println(n + " is not divisible by 3" );
}
if (last_digit == 0 || last_digit == 5 ) {
System.out.println(n + " is divisible by 5" );
} else {
System.out.println(n + " is not divisible by 5" );
}
}
}
|
Python3
n = 1234567890123456789
sum = 0
last_digit = n % 10
if last_digit % 2 = = 0 :
print (n, "is divisible by 2" )
else :
print (n, "is not divisible by 2" )
temp_n = n
while temp_n > 0 :
sum + = temp_n % 10
temp_n / / = 10
if sum % 3 = = 0 :
print (n, "is divisible by 3" )
else :
print (n, "is not divisible by 3" )
if last_digit = = 0 or last_digit = = 5 :
print (n, "is divisible by 5" )
else :
print (n, "is not divisible by 5" )
|
C#
using System;
class Gfg {
public static void Main() {
long n = 1234567890123456789;
int sum = 0;
int last_digit = ( int )(n % 10);
if (last_digit % 2 == 0) {
Console.WriteLine($ "{n} is divisible by 2" );
} else {
Console.WriteLine($ "{n} is not divisible by 2" );
}
long temp_n = n;
while (temp_n > 0) {
sum += ( int )(temp_n % 10);
temp_n /= 10;
}
if (sum % 3 == 0) {
Console.WriteLine($ "{n} is divisible by 3" );
} else {
Console.WriteLine($ "{n} is not divisible by 3" );
}
if (last_digit == 0 || last_digit == 5) {
Console.WriteLine($ "{n} is divisible by 5" );
} else {
Console.WriteLine($ "{n} is not divisible by 5" );
}
}
}
|
Javascript
let n = 1234567890123456789;
if (n % 2 === 0) {
console.log(`${n} is divisible by 2`);
} else {
console.log(`${n} is not divisible by 2`);
}
let sum = 0;
let temp_n = n;
while (temp_n > 0) {
sum += temp_n % 10;
temp_n = Math.floor(temp_n / 10);
}
if (sum % 3 === 0) {
console.log(`${n} is divisible by 3`);
} else {
console.log(`${n} is not divisible by 3`);
}
let last_digit = n % 10;
if (last_digit === 0 || last_digit === 5) {
console.log(`${n} is divisible by 5`);
} else {
console.log(`${n} is not divisible by 5`);
}
|
Output1234567890123456789 is not divisible by 2
0 is divisible by 3
0 is not divisible by 5
Time Complexity: O(log10(n)), where n is the number being checked, because we iterate over each digit of the number once.
Space Complexity: O(1), because we only need a few variables to store the state of the algorithm
Please Login to comment...