Given a number, the task is to check if number is divisible by 25. The input number may be large and it may not be possible to store even if we use long long int.
Examples:
Input : n = 56945250
Output : Yes
Input : n = 1234567589333100
Output : Yes
Input : n = 3635883959606670431112222
Output : No
Since input number may be very large, we cannot use n % 25 to check if a number is divisible by 25 or not, especially in languages like C/C++. The idea is based on following fact.
A number is divisible by 25 if its digits
last two digits will be 0 or divisible by 25 .
Illustration:
For example, let us consider 769575
Number formed by last two digits is = 75
Since 75 is divisible by 25 , answer is YES.
Let us consider 5325, we can write it as
5325 = 5*1000 + 3*100 + 2*10 + 5
The proof is based on below observation:
Remainder of 10i divided by 25 is 0 if i greater
than or equal to two. Note than 100, 1000,
... etc lead to remainder 0 when divided by 25.
So remainder of " 5*1000 + 3*100 + 2*10 + 5"
divided by 25 is equivalent to remainder
of following :
0 + 0 + 20 + 5 = 25
Since 25 is divisible by 25, answer is yes.
C++
#include<bits/stdc++.h>
using namespace std;
bool isDivisibleBy25(string str)
{
int n = str.length();
if (n == 1)
return false ;
return ( (str[n-1]- '0' == 0 &&
str[n-2]- '0' == 0) ||
((str[n-2]- '0' )*10 + (str[n-1]- '0' ))%25 == 0 );
}
int main()
{
string str = "76955" ;
isDivisibleBy25(str)? cout << "Yes" :
cout << "No " ;
return 0;
}
|
Java
import java.io.*;
class IsDivisible
{
static boolean isDivisibleBy25(String str)
{
int n = str.length();
if (n == 1 )
return false ;
return ( (str.charAt(n- 1 )- '0' == 0 &&
str.charAt(n- 2 )- '0' == 0 ) ||
((str.charAt(n- 2 )- '0' )* 10 + (str.charAt(n- 1 )- '0' ))% 25 == 0 );
}
public static void main (String[] args)
{
String str = "76955" ;
if (isDivisibleBy25(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isDivisibleBy25(st) :
n = len (st)
if (n = = 1 ) :
return False
return (( int )(st[n - 1 ]) = = 0 and (( int )(st[n - 2 ]) = = 0 ) or
(( int )(st[n - 2 ]) * 10 + ( int )(st[n - 1 ]) % 25 = = 0 ))
st = "76955"
if (isDivisibleBy25(st)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class IsDivisible
{
static bool isDivisibleBy25(String str)
{
int n = str.Length;
if (n == 1)
return false ;
return ((str[n - 1] - '0' == 0 &&
str[n - 2] - '0' == 0) ||
((str[n - 2] - '0' ) * 10 +
(str[n - 1] - '0' )) % 25 == 0);
}
public static void Main ()
{
String str = "76955" ;
if (isDivisibleBy25(str))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function isDivisibleBy25( $str )
{
$n = strlen ( $str );
if ( $n == 1)
return false;
return ( ( $str [ $n - 1] - '0' == 0 &&
$str [ $n - 2] - '0' == 0) ||
(( $str [ $n - 2] - '0' ) * 10 +
( $str [ $n - 1] - '0' )) % 25 == 0 );
}
$str = "76955" ;
$x = isDivisibleBy25( $str ) ? "Yes" : "No " ;
echo ( $x );
?>
|
Javascript
<script>
function isDivisibleBy25(str)
{
n = str.length;
if (n == 1)
return false ;
return ((str[n - 1] -'0 ' == 0 &&
str[n - 2] -' 0 ' == 0) ||
((str[n - 2] -' 0 ') * 10 +
(str[n - 1] - ' 0')) % 25 == 0);
}
var str = "76955" ;
var x = isDivisibleBy25(str) ? "Yes" : "No" ;
document.write (x);
</script>
|
Method: Checking given number is divisible by 25 or not by using the modulo division operator “%”.
C++
#include <iostream>
using namespace std;
int main()
{
int num = 56945250;
if (num % 25 == 0) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static void main(String[] args)
{
long n= 56945250 ;
if ((n)% 25 == 0 )
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
n = 56945250
if int (n) % 25 = = 0 :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
public static void Main()
{
long n=56945250;
if (n%25==0)
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
PHP
<?php
$n =56945250;
if ( $n %25==0)
{
echo "Yes" ;
}
else
{
echo "No" ;
}
?>
|
Javascript
var n = 56945250
if (n % 25 == 0)
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(1), as we are not using any loops for traversing.
Auxiliary Space: O(1), as we are not using any extra space.
This article is contributed by DANISH_RAZA . If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Method 3:
Approach:
1. Declare a function is_divisible_by_25 which returns an integer.
2. Declare a long long variable num and assign it the value of 12345678998765432100.
3. Declare a char array num_str with size 21 (maximum number of digits + null terminator) and use sprintf to convert num to a string and store it in num_str.
4. Get the length of the string num_str and store it in the variable len.
5. Check if the length of the string is less than 2. If it is, return 0 (false).
6. Loop through the last two characters of the string (i.e., num_str[len-2] and num_str[len-1]).
7. Check if the current pair of characters represents a number that is divisible by 25 (i.e., ends in 00, 25, 50, or 75). If it is, return 1 (true).
8. If the loop completes without finding a pair of digits that is divisible by 25, return 0 (false).
9. In the main function, call the is_divisible_by_25 function and print a message based on its return value.
C
#include <stdio.h>
#include <string.h>
int is_divisible_by_25() {
long long num = 1234567899876;
char num_str[21];
sprintf (num_str, "%lld" , num);
int len = strlen (num_str);
if (len < 2) {
return 0;
}
for ( int i = len-2; i < len; i++) {
if ((num_str[i] == '0' && num_str[i+1] == '0' )
|| (num_str[i] == '2' && num_str[i+1] == '5' )
|| (num_str[i] == '5' && num_str[i+1] == '0' )
|| (num_str[i] == '7' && num_str[i+1] == '5' )) {
return 1;
}
}
return 0;
}
int main() {
if (is_divisible_by_25()) {
printf ( "The number is divisible by 25.\n" );
} else {
printf ( "The number is not divisible by 25.\n" );
}
return 0;
}
|
C++
#include <iostream>
#include <string>
bool is_divisible_by_25() {
long long num = 1234567899876;
std::string num_str = std::to_string(num);
int len = num_str.length();
if (len < 2) {
return false ;
}
for ( int i = len-2; i < len; i++) {
if ((num_str[i] == '0' && num_str[i+1] == '0' )
|| (num_str[i] == '2' && num_str[i+1] == '5' )
|| (num_str[i] == '5' && num_str[i+1] == '0' )
|| (num_str[i] == '7' && num_str[i+1] == '5' )) {
return true ;
}
}
return false ;
}
int main() {
if (is_divisible_by_25()) {
std::cout << "The number is divisible by 25." << std::endl;
} else {
std::cout << "The number is not divisible by 25." << std::endl;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean isDivisibleBy25()
{
long num = 1234567899876L;
String numStr = Long.toString(num);
int len = numStr.length();
if (len < 2 ) {
return false ;
}
for ( int i = len - 2 ; i < len; i++) {
if ((numStr.charAt(i) == '0'
&& numStr.charAt(i + 1 ) == '0' )
|| (numStr.charAt(i) == '2'
&& numStr.charAt(i + 1 ) == '5' )
|| (numStr.charAt(i) == '5'
&& numStr.charAt(i + 1 ) == '0' )
|| (numStr.charAt(i) == '7'
&& numStr.charAt(i + 1 ) == '5' )) {
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
if (isDivisibleBy25()) {
System.out.println(
"The number is divisible by 25." );
}
else {
System.out.println(
"The number is not divisible by 25." );
}
}
}
|
Javascript
function is_divisible_by_25() {
let num = 1234567899876;
let num_str = num.toString();
let len = num_str.length;
if (len < 2) {
return false ;
}
for (let i = len - 2; i < len; i++) {
if (
(num_str[i] == "0" && num_str[i + 1] == "0" ) ||
(num_str[i] == "2" && num_str[i + 1] == "5" ) ||
(num_str[i] == "5" && num_str[i + 1] == "0" ) ||
(num_str[i] == "7" && num_str[i + 1] == "5" )
){
return true ;
}
}
return false ;
}
if (is_divisible_by_25()) {
console.log( "The number is divisible by 25." );
}
else {
console.log( "The number is not divisible by 25." );
}
|
Python3
def is_divisible_by_25():
num = 1234567899876
num_str = str (num)
length = len (num_str)
if length < 2 :
return False
for i in range (length - 2 , length):
if (num_str[i] = = '0' and num_str[i + 1 ] = = '0' ) \
or (num_str[i] = = '2' and num_str[i + 1 ] = = '5' ) \
or (num_str[i] = = '5' and num_str[i + 1 ] = = '0' ) \
or (num_str[i] = = '7' and num_str[i + 1 ] = = '5' ):
return True
return False
if is_divisible_by_25():
print ( "The number is divisible by 25." )
else :
print ( "The number is not divisible by 25." )
|
C#
using System;
class GFG
{
static bool IsDivisibleBy25()
{
long num = 1234567899876;
string numStr = num.ToString();
int len = numStr.Length;
if (len < 2)
{
return false ;
}
for ( int i = len - 2; i < len; i++)
{
if ((numStr[i] == '0' && numStr[i + 1] == '0' )
|| (numStr[i] == '2' && numStr[i + 1] == '5' )
|| (numStr[i] == '5' && numStr[i + 1] == '0' )
|| (numStr[i] == '7' && numStr[i + 1] == '5' ))
{
return true ;
}
}
return false ;
}
static void Main()
{
if (IsDivisibleBy25())
{
Console.WriteLine( "The number is divisible by 25." );
}
else
{
Console.WriteLine( "The number is not divisible by 25." );
}
}
}
|
Output
The number is not divisible by 25.
Time Complexity : O(1)
Auxiliary Space : O(log n)
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!