I want to do this in a function: How do I find out in a C program if a number is divisible by 2, 3, 4, 5, 6, 8, 9, 25 and 125 without using the % operator and using the divisibility rules? the base should be 10*
-
2This will be helpful: `a % b == a - (a / b * b)` for positive integers `a` and `b`. – MikeCAT Feb 25 '21 at 14:07
-
it's possible using bitwise operations and [modular multiplicative inverse](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse) – tstanisl Feb 25 '21 at 14:11
-
How do you do that with using `%` operator? – MikeCAT Feb 25 '21 at 14:11
-
what is the range of divident? like 0-10000? – tstanisl Feb 25 '21 at 14:19
-
yes, the range should be between 0 and 10000 – Feb 25 '21 at 14:23
-
1@tstanisl that was impressive. – anastaciu Feb 25 '21 at 14:24
-
it may be problematic because "divisiblity rule by 3" requires checking if the sum of digits is divisible by 3. Does "no %" rule apply this this last check as well? – tstanisl Feb 25 '21 at 14:41
-
Does this answer your question? [Integer division algorithm](https://stackoverflow.com/questions/5097383/integer-division-algorithm) – TAbdiukov Feb 26 '21 at 04:07
2 Answers
To use divisibility rules, you have to work with digits. Perhaps task assumes no division (at least in explicit form - you can extract digits from string representation)
For divisibility by 2, check whether the last digit is in set 0,2,4,6,8
For divisibility by 4, check whether the last digit + doubled previous one is in set 0,4,8
. If result is larger than 10, repeat (88=>2*8+8=24=>2*2+4=8
)
Similar situation for 8, but sum last + 2*previous + 4*second_from_the_end
(512 => 4*5+2*1+2=24=>2*2+4=8
)
For divisibility by 5, check whether the last digit is in set 0,5
, similar situation for 25, 125
For divisibility by 3, sum all digits, repeat process until result becomes < 10. So-called "digit root" should be in set 0,3,6,9
, similar situation for divisibility by 9.
For 6 check divisibilty by both 2 and by 3
I am not strong in C, so my example perhaps is very weird (ideone check)
#include <stdio.h>
int divby3(int n) {
char s[10];
do {
sprintf(s, "%d", n); //transform 72 to char buffer "72"
n = 0;
int i = 0;
while(s[i]) //until nil (end of string) found, we can also use for loop
n += s[i++] - 0x30; //get difference of current char and char "0"
}
while (n >= 10); //until 1-digit value
return (n==0) || (n==3) || (n==6) || (n==9);
}
int divby5(int n) {
char s[10];
int len = sprintf(s, "%d", n);
n = s[len - 1] - 0x30; //last digit
return (n==0) || (n==5);
}
int main(void) {
printf("%d", divby3(72)); //try 71
return 0;
}

- 77,366
- 5
- 53
- 86
A function that uses the a - (a / b * b)
implementation of the modulus operator: (credit @MikeCat)
bool isDivisible(int a, int b)
{
if((a - (a / b * b)) == 0) return true;
return false;
}
Usage:
int main(void)
{
int a = 8;
int b = 4;
int c = 3;
bool res = isDivisible(a,b);
bool res2 = isDivisible(a,c);
return 0;
}
EDIT - to address question in comments:
"how can i represent such a program with the divisibility rules? Thank you for your code, i forgott to mention that i have to use the divisibility rules in each function"
The following shows how to pass in divisibility rules as an argument...
const int div_1[] = {2, 3, 4, 5, 6, 8, 9, 25, 125};
const int div_2[] = {7, 5, 17, 12, 11};
int main()
{
size_t size = 0;
size = sizeof div_1/sizeof div_1[0];
bool res = isDivisible(2*3*4*5*6*8*9*25*125, div_1, size);
size = sizeof div_2/sizeof div_2[0];
bool res2 = isDivisible(125, div_2, size);
return 0;
}
// numerator divisor array array size
bool isDivisible(long a, long div_rules[], size_t size)
{
//divisibility rules
const int divisors[] = {2, 3, 4, 5, 6, 8, 9, 25, 125};
for(int i = 0; i<size;i++)
{
if((a - (a / div_rules[i] * div_rules[i])) != 0) return false;
}
return true;
}

- 22,849
- 3
- 43
- 87
-
how can i represent such a program with the divisibility rules? Thank you for your code, i forgott to mention that i have to use the divisibility rules in each function – Feb 25 '21 at 14:29
-
@Fennix900, please add this information to the question. Add information about the range and that the base is 10. – tstanisl Feb 25 '21 at 14:35
-
@Fennix900 - Let me know specifics whether or not this implementation (See _EDIT_) works for you. – ryyker Feb 25 '21 at 16:06
-
@Fennix900 - _"it doesn't work"_ doesn't work either. with specifics I may be able to address the problem. – ryyker Feb 25 '21 at 16:32
-
1@ryyker Don't worry, MBo already finished this guy's homework for him. – Steve Summit Feb 25 '21 at 21:57
-
@SteveSummit - LOL, well, I have to admit that the concept of _divisibility rules_ is a little vague for me, so for all I know I totally missed the point and MBo got it right. Good for the both of them. :) – ryyker Feb 25 '21 at 22:03