Given a number N, the task is to check if for every value of i (0 <= i <= len), the first i digits of a number is divisible by (len – i + 1) or not, where len is the number of digits in N. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 52248.
Output: Yes
Explanation:
- 5 is divisible by 5
- 52 is divisible by 4
- 522 is divisible by 3
- 5224 is divisible by 2
- 52248 is divisible by 1
Input: N = 59268
Output : No
Approach: The idea is to traverse all the prefixes of the given number and for each prefix, check if it is satisfies the condition or not.
Follow the steps below to solve the problem:
- Initialize a variable, say i as 1, to maintain the value of (len – i + 1).
- Iterate while n is greater than 0.
- If N is not divisible by i, then return false.
- Otherwise, divide N by 10 and increase the value of i by 1.
- Finally, return true.
Below is the implementation of the above approach.
C++
// C++ program for the above approach #include <iostream> using namespace std; // Function to check if all prefixes // of a number is divisible by // remaining count of digits or not bool prefixDivisble( int n) { int i = 1; while (n > 0) { // Traverse and check divisibility // for each updated number if (n % i != 0) return false ; // Update the original number n = n / 10; i++; } return true ; } // Driver Code int main() { // Given Input int n = 52248; // Function Call if (prefixDivisble(n)) cout << "Yes" << endl; else cout << "No" << endl; return 0; } |
Java
// Java program for the above approach import java.io.*; class GFG{ // Function to check if all prefixes // of a number is divisible by // remaining count of digits or not public static boolean prefixDivisble( int n) { int i = 1 ; while (n > 0 ) { // Traverse and check divisibility // for each updated number if (n % i != 0 ) return false ; // Update the original number n = n / 10 ; i++; } return true ; } // Driver Code public static void main (String[] args) { // Given Input int n = 52248 ; // Function Call if (prefixDivisble(n)) System.out.println( "Yes" ); else System.out.println( "No" ); } } // This code is contributed by lokeshpotta20. |
Python3
# Python3 program for the above approach # Function to check if all prefixes of # a number is divisible by remaining count # of digits or not def prefixDivisble(n): i = 1 while n > 0 : # Traverse and check divisibility # for each updated number if n % i ! = 0 : return False # Update the original number n = n / / 10 i + = 1 return True # Driver Code # Given Input n = 52248 # Function Call if (prefixDivisble(n)): print ( "Yes" ) else : print ( "No" ) # This code is contributed by abhivick07 |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to check if all prefixes // of a number is divisible by // remaining count of digits or not static bool prefixDivisble( int n) { int i = 1; while (n > 0) { // Traverse and check divisibility // for each updated number if (n % i != 0) return false ; // Update the original number n = n / 10; i++; } return true ; } // Driver Code public static void Main() { // Given Input int n = 52248; // Function Call if (prefixDivisble(n)) Console.Write( "Yes" ); else Console.Write( "No" ); } } // This code is contributed by ipg2016107 |
Javascript
<script> // Javascript program for the above approach // Function to check if all prefixes // of a number is divisible by // remaining count of digits or not function prefixDivisble(n) { let i = 1; while (n > 0) { // Traverse and check divisibility // for each updated number if (n % i != 0) return false ; // Update the original number n = parseInt(n / 10); i++; } return true ; } // Driver Code // Given Input let n = 52248; // Function Call if (prefixDivisble(n) == true ) document.write( "Yes" ); else document.write( "No" ); // This code is contributed by lokeshpotta20. </script> |
Yes
Time Complexity : O(len) where len is the number of digits in N.
Auxiliary Space : O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!