Check if a large number is divisible by 75 or not
Given a very large number in the form of a string, the task is to check if the number is divisible by 75 or not.
Examples:
Input: N = 175 Output: No Input: N = 100000000000000000754586672150 Output: Yes
Approach: A number is divisible by 75 only if it is divisible by 3(if the sum of the digit is divisible by 3) and divisible by 25 (if the last two digit is divisible by 25) both.
Below is the implementation to check the given number is divisible by 75 or not.
C++
// C++ implementation to check the number // is divisible by 75 or not #include <bits/stdc++.h> using namespace std; // check divisibleBy3 bool divisibleBy3(string number) { // to store sum of Digit int sumOfDigit = 0; // traversing through each digit for ( int i = 0; i < number.length(); i++) // summing up Digit sumOfDigit += number[i] - '0' ; // check if sumOfDigit is divisibleBy3 if (sumOfDigit % 3 == 0) return true ; return false ; } // check divisibleBy25 bool divisibleBy25(string number) { // if a single digit number if (number.length() < 2) return false ; // length of the number int length = number.length(); // taking the last two digit int lastTwo = (number[length - 2] - '0' ) * 10 + (number[length - 1] - '0' ); // checking if the lastTwo digit is divisibleBy25 if (lastTwo % 25 == 0) return true ; return false ; } // Function to check divisibleBy75 or not bool divisibleBy75(string number) { // check if divisibleBy3 and divisibleBy25 if (divisibleBy3(number) && divisibleBy25(number)) return true ; return false ; } // Drivers code int main() { string number = "754586672150" ; // divisible bool divisible = divisibleBy75(number); // if divisibleBy75 if (divisible) cout << "Yes" ; else cout << "No" ; return 0; } |
chevron_right
filter_none
Java
// Java implementation to check the number // is divisible by 75 or not import java.io.*; class GFG { // check divisibleBy3 static boolean divisibleBy3(String number) { // to store sum of Digit int sumOfDigit = 0 ; // traversing through each digit for ( int i = 0 ; i < number.length(); i++) // summing up Digit sumOfDigit += number.charAt(i) - '0' ; // check if sumOfDigit is divisibleBy3 if (sumOfDigit % 3 == 0 ) return true ; return false ; } // check divisibleBy25 static boolean divisibleBy25(String number) { // if a single digit number if (number.length() < 2 ) return false ; // length of the number int length = number.length(); // taking the last two digit int lastTwo = (number.charAt(length - 2 ) - '0' ) * 10 + (number.charAt(length - 1 ) - '0' ); // checking if the lastTwo digit is divisibleBy25 if (lastTwo % 25 == 0 ) return true ; return false ; } // Function to check divisibleBy75 or not static boolean divisibleBy75(String number) { // check if divisibleBy3 and divisibleBy25 if (divisibleBy3(number) && divisibleBy25(number)) return true ; return false ; } // Drivers code public static void main (String[] args) { String number = "754586672150" ; // divisible boolean divisible = divisibleBy75(number); // if divisibleBy75 if (divisible) System.out.println( "Yes" ); else System.out.println( "No" ); } } // This code is contributed // by inder_verma.. |
chevron_right
filter_none
Python3
# Python 3 implementation to check the # number is divisible by 75 or not # check divisibleBy3 def divisibleBy3(number): # to store sum of Digit sumOfDigit = 0 # traversing through each digit for i in range ( 0 , len (number), 1 ): # summing up Digit sumOfDigit + = ord (number[i]) - ord ( '0' ) # check if sumOfDigit is divisibleBy3 if (sumOfDigit % 3 = = 0 ): return True return False # check divisibleBy25 def divisibleBy25(number): # if a single digit number if ( len (number) < 2 ): return False # length of the number length = len (number) # taking the last two digit lastTwo = (( ord (number[length - 2 ]) - ord ( '0' )) * 10 + ( ord (number[length - 1 ]) - ord ( '0' ))) # checking if the lastTwo digit # is divisibleBy25 if (lastTwo % 25 = = 0 ): return True return False # Function to check divisibleBy75 or not def divisibleBy75(number): # check if divisibleBy3 and divisibleBy25 if (divisibleBy3(number) and divisibleBy25(number)): return True return False # Driver Code if __name__ = = '__main__' : number = "754586672150" # divisible divisible = divisibleBy75(number) # if divisibleBy75 if (divisible): print ( "Yes" ) else : print ( "No" ) # This code is contributed by # Surendra_Gangwar |
chevron_right
filter_none
C#
// C# implementation to check the number // is divisible by 75 or not using System; class GFG { // check divisibleBy3 static bool divisibleBy3( string number) { // to store sum of Digit int sumOfDigit = 0; // traversing through each digit for ( int i = 0; i < number.Length; i++) // summing up Digit sumOfDigit += number[i] - '0' ; // check if sumOfDigit is divisibleBy3 if (sumOfDigit % 3 == 0) return true ; return false ; } // check divisibleBy25 static bool divisibleBy25( string number) { // if a single digit number if (number.Length < 2) return false ; // length of the number int length = number.Length; // taking the last two digit int lastTwo = (number[length - 2] - '0' ) * 10 + (number[length - 1] - '0' ); // checking if the lastTwo digit // is divisibleBy25 if (lastTwo % 25 == 0) return true ; return false ; } // Function to check divisibleBy75 or not static bool divisibleBy75( string number) { // check if divisibleBy3 and divisibleBy25 if (divisibleBy3(number) && divisibleBy25(number)) return true ; return false ; } // Driver Code public static void Main () { string number = "754586672150" ; // divisible bool divisible = divisibleBy75(number); // if divisibleBy75 if (divisible) Console.WriteLine( "Yes" ); else Console.WriteLine( "No" ); } } // This code is contributed // by inder_verma.. |
chevron_right
filter_none
PHP
<?php // PHP implementation to check the // number is divisible by 75 or not // check divisibleBy3 function divisibleBy3( $number ) { // to store sum of Digit $sumOfDigit = 0; // traversing through each digit for ( $i = 0; $i < strlen ( $number ); $i ++) // summing up Digit $sumOfDigit += $number [ $i ] - '0' ; // check if sumOfDigit is // divisibleBy3 if ( $sumOfDigit % 3 == 0) return true; return false; } // check divisibleBy25 function divisibleBy25( $number ) { // if a single digit number if ( strlen ( $number ) < 2) return false; // length of the number $length = strlen ( $number ); // taking the last two digit $lastTwo = ( $number [ $length - 2] - '0' ) * 10 + ( $number [ $length - 1] - '0' ); // checking if the lastTwo digit // is divisibleBy25 if ( $lastTwo % 25 == 0) return true; return false; } // Function to check divisibleBy75 or not function divisibleBy75( $number ) { // check if divisibleBy3 and // divisibleBy25 if (divisibleBy3( $number ) && divisibleBy25( $number )) return true; return false; } // Driver Code $number = "754586672150" ; // divisible $divisible = divisibleBy75( $number ); // if divisibleBy75 if ( $divisible ) echo "Yes" ; else echo "No" ; // This code is contributed by ANKITRAI1 ?> |
chevron_right
filter_none
Output:
No
Recommended Posts:
- Check if a large number is divisible by 20
- Check if a large number is divisible by 13 or not
- To check whether a large number is divisible by 7
- Check if any large number is divisible by 19 or not
- Check if any large number is divisible by 17 or not
- Check if a large number is divisible by 5 or not
- Check if a large number is divisible by 25 or not
- Check a large number is divisible by 16 or not
- Check if a large number is divisible by 9 or not
- Check if a large number is divisible by 3 or not
- Check if a large number is divisible by 6 or not
- Check if a large number is divisible by 8 or not
- Check if a large number is divisible by 4 or not
- Check if a large number is divisible by 11 or not
- Check if a large number is divisible by 2, 3 and 5 or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.