Check if a number is divisible by 31 or not | C ++ program to check whether a number is divisible by 31 or not ; Function to check if the number is divisible by 31 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting three times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 31 or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <stdlib.h> NEW_LINE using namespace std ; bool isDivisible ( int n ) { int d ; while ( n / 100 ) { d = n % 10 ; n /= 10 ; n = abs ( n - ( d * 3 ) ) ; } return ( n % 31 == 0 ) ; } int main ( ) { int N = 1922 ; if ( isDivisible ( N ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }