Monday, November 18, 2024
Google search engine
HomeData Modelling & AICheck if a large number is divisible by 25 or not

Check if a large number is divisible by 25 or not

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++




// C++ program to find if a number is
// divisible by 25 or not
#include<bits/stdc++.h>
using namespace std;
 
// Function to find that number divisible
// by 25 or not.
bool isDivisibleBy25(string str)
{
    // If length of string is single digit then
    // it's not divisible by 25
    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 );
}
 
// Driver code
int main()
{
    string str = "76955";
    isDivisibleBy25(str)?  cout << "Yes" :
                           cout << "No ";
    return 0;
}


Java




// Java program to find if a number is
// divisible by 25 or not
import java.io.*;
class IsDivisible
{
    // Function to find that number divisible
    // by 25 or not.
    static boolean isDivisibleBy25(String str)
    {
        // If length of string is single digit then
        // it's not divisible by 25
        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 );
    }
     
    // main function
    public static void main (String[] args)
    {
        String str = "76955";
        if(isDivisibleBy25(str))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


Python3




# Python 3 program to find if
# a number is divisible by 25
# or not
 
# Function to find that
# number divisible by 25 or not.
def isDivisibleBy25(st) :
 
    # If length of string is
    # single digit then it's
    # not divisible by 25
    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))
 
# Driver code
st = "76955"
if(isDivisibleBy25(st)) :
    print("Yes")
else :
    print("No")
     
 
# This code is contributed by Nikita Tiwari.


C#




// C# program to find if a number
// is divisible by 25 or not
using System;
 
class IsDivisible
{
    // Function to find that number
    // divisible by 25 or not.
    static bool isDivisibleBy25(String str)
    {
        // If length of string is single digit then
        // then it's not divisible by 25
        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);
    }
     
    // Driver Code
    public static void Main ()
    {
        String str = "76955";
        if(isDivisibleBy25(str))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
 
// This code is contributed by Nitin Mittal


PHP




<?php
// PHP program to find if a number
// is divisible by 25 or not
 
// Function to find that number
//  divisible by 25 or not.
function isDivisibleBy25($str)
{
     
    // If length of string
    // is single digit then
    // it's not divisible by 25
    $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 );
}
 
// Driver code
$str = "76955";
$x = isDivisibleBy25($str) ? "Yes" : "No ";
echo($x);
 
// This code is contributed by Ajit.
?>


Javascript




<script>
 
// Javascript program to find if a number
// is divisible by 25 or not
   
// Function to find that number
// divisible by 25 or not
function isDivisibleBy25(str)
{
     
    // If length of string
    // is single digit then
    // it's not divisible by 25
    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);
}
   
// Driver Code
var str = "76955";
var x = isDivisibleBy25(str) ? "Yes" : "No";
 
document.write (x);
 
// This code is contributed by bunnyram19
 
</script>


Output

No 

Method: Checking given number is divisible by 25 or not by using the modulo division operator “%”.

C++




// C++ code To check whether the given number is divisible
// by 25 or not
 
#include <iostream>
using namespace std;
 
int main()
{
 
    // input number
    int num = 56945250;
    // checking if the given number is divisible by 25 or
    // not using modulo division operator if the output of
    // num%25 is equal to 0 then given number is divisible
    // by 25 otherwise not divisible by 25
    if (num % 25 == 0) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java




// Java code
// To check whether the given number is divisible by 25 or not
  
import java.io.*;
import java.util.*;
   
class GFG
{
    
  public static void main(String[] args)
  {
    //input
    long n=56945250;
    // finding given number is divisible by 25 or not
      
    if ((n)%25==0)
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
      
  }
}


Python3




# Python code
# To check whether the given number is divisible by 25 or not
  
#input
n=56945250
# the above input can also be given as n=input() -> taking input from user
# finding given number is divisible by 25 or not
if int(n)%25==0:
  print("Yes")
else:
  print("No")


C#




using System;
  
public class GFG {
      
    public static void Main()
    {
        //input
        long n=56945250;
         
         
        // finding given number is divisible by 25 or not
        if (n%25==0)
        {
            Console.Write("Yes");
        }
        else
        {
            Console.Write("No");
        }
    }
}


PHP




<?php
 
   //input
 
    $n=56945250;
 
      
 
    // finding given number is divisible by 25 or not
 
    if ($n%25==0)
 
    {
 
        echo "Yes";
 
    }
 
    else
 
    {
 
        echo "No";
 
    }
 
  
 
// This code is contributed by laxmigangarajula03
 
?>


Javascript




// JavaScript code for the above approach
 
// To check whether the given number is divisible by 7 or not
 
//input
 
var n = 56945250
 
// the above input can also be given as n=input() -> taking input from user
 
// finding given number is divisible by 25 or not
 
if (n % 25 == 0)
 
    console.log("Yes");
 
else
 
    console.log("No");


Output

Yes

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 to check if a number is divisible by 25
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(); // convert the number to a string
        int len = numStr.Length;
        if (len < 2)
        {
            return false;
        }
        for (int i = len - 2; i < len; i++) // loop over the last two digits
        {
            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; // if any of the conditions are met, return true
            }
        }
        return false; // if none of the conditions are met, 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!

RELATED ARTICLES

Most Popular

Recent Comments