0

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*

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24

2 Answers2

1

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;
}
MBo
  • 77,366
  • 5
  • 53
  • 86
0

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;
}
ryyker
  • 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