How to correctly divide a number to only one digit in C?

Asked

Viewed 483 times

5

I have the following function :

divisibilidade11(int num);

And the following variables: :

int dividendo;
int divisor;

When the user enters with the type 11 for the variable divisor the function divisibilidade11 will be called and will check if the variable dividendo is divisible by divisor. And to check whether it is divisible or not, you will have to follow the criteria : If the sum of this even-order digit (Sp) subtracted from the odd-order sum (Si), results in 0 or a number divisible by 11, it returns true and if not, it returns false.

Example :

User enters with number 2376 for the variable dividendo;

User enters with number 11 for the variable divisor;

Is called the function divisibilidade11, to check whether or not it is divisible;

The sum of the impairments (Si) = 3+6 = 9;

Sum of pairs (Sp) = 2+7 = 9;

Si - Sp = 0; Therefore, 2376 is divisible by 11.

NOTE: The right-most digit is the odd-order first digit.

Example of how it would appear to the user :

The function divisibilidade11 you have to repeat the process until the result is a number with a digit. If this digit is 0, then the original number is divisible by 11.

  • 2

    Not from exercise. Why it counts the rightmost digit as odd-order first.

  • 1

    Thanks for the answer, I realized after performing a rereading of your question what you meant. Please see my answer, and ask your questions if necessary.

  • I’ll try to do here by following the criteria of divisibility, if I can I put the code.

1 answer

3


Using Strings

Knowing that the maximum value for integers that most computers currently support is up to 19 digits, I will use a 20-digit string to perform this operation.

#include <string.h>
#include <stdio.h>    

char divisibilidade11(long long int dividendo){
    char array_numerico[20] = "";
    int total_par = 0;
    int total_impar = 0;
    snprintf(array_numerico, 20, "%lld", dividendo);
    for(char iterador = strlen(array_numerico) - 1, par = 0; iterador >= 0; iterador -= 1){
        if(!par){
            total_impar += array_numerico[iterador] - 48;
            par = 1;
        }
        else{
            total_par += array_numerico[iterador] - 48;
            par = 0;
        }
    }
    if((total_par - total_impar) == 0 || ((total_par - total_impar) % 11 == 0)){
        return 1;
    }
    else{
        return 0;
    }
}

Using Decimal Mathematical Operations

char divisibilidade11(long long int dividendo){
    int total_par = 0;
    int total_impar = 0;
    char par = 0;
    while(dividendo != 0){
        if(!par){
            total_impar += (dividendo % 10);
            par = 1;
        }
        else{
            total_par += (dividendo % 10);
            par = 0;
        }
        dividendo /= 10;
    }
    if((total_par - total_impar) == 0 || (total_par - total_impar) % 11 == 0)
        return 1;
    else return 0;
}
  • 1

    Thank you very much for the answer, but the problem is that I cannot use array. So this exercise is complicated to do.

  • 2

    No problem! Let’s do it another way, then! @Mondial, my second answer meets the requirements of your question?

  • Wow, thank you so much for having patience to have modified the answer. I will test here according to the exercise criteria.

  • 2

    No problem! This question was not only a learning of C but also an interesting way to recall certain basic rules of mathematical numerical representation systems, and also served as a comparison between the two main ways of solving the question. Note that the second form may, for many, require more thought, but it really is much more efficient and fast than the first, which requires the use of various extra functions that make it much heavier and inefficient.

  • 1

    Thank you for your patience in answering my question and modifying your answer when necessary. But the problem that occurred is that in this example that you showed, you put even = 1 if the digit gives in even, or even = 0 if the digit gives in odd and then subtract , ends up in causing some numbers even though they are not divided by 11, to be shown that they are. Even so, thank you so much for everything, I gave +1. I will have to do otherwise, but still, thank you.

  • 1

    I will have to do like what I put in the example in the post, in which I have to separate the number into separate digits, and then pick the pair with pair and add, and odd with odd. For example, the number 1771561, in which the Sum of Impairments(Si) 1+7+5+1 = 14, and Sum of Pairs(Sp) 7+1+6 = 14. Si - Sp = 0, thus divisible by 11. That is, I need to separate digit by digit, add them in even or odd and then subtract, to see if they give 0 or are divisible by 11.

  • 3

    @Pedrohenrique +1 by the attempt, particularly liked the efficient mode in which you used array for the strings. But from what I understand, this mode will not serve, and even being useful for other circumstances, will not be for the OP.

  • @Mondial, do you have any example of number that does not work with this method?

  • @Pedrohenrique The number, for example, 439087 does not work, because it will do the subtraction by putting as pair = 1 and even = 0 according to the if, and put that can be divided by 11, and not da. I need a way to separate each digit and then check whether it is even or odd, only after, subtract and see if it is 0 or a number divisible by 11.

  • 4

    But 439087 is divisible by 11.

  • 3

    @Pedrohenrique I checked the code, and it works yes, +1. I believe this should be enough for OP.

  • 2

    Hahaha, I was desperate here! Any question or problem, just comment! :)

  • 2

    @Nexus Have need more not bro, the code gave :) . I even gave you +100 of bonus. Vlw bro for effort and patience with me.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.