I am in doubt how to do [closed]

3

As per module in some mathematical function in C? I tried some basic things but it did not work.

    
asked by anonymous 22.09.2015 / 16:17

1 answer

6

To calculate the module of any number check two cases being the positive number, your code does nothing, and the negative number makes it positive.

#include<stdio.h>

int modulo(int num);

main()
{
    printf("%d", modulo(2));
    printf("%d", modulo(-2));
}

int modulo(int num)
{
    int resultado;

    if(num < 0)
        resultado = -num;
    else
        resultado = num;

    return resultado;
}

If your question refers to the use of the module operator ( % ) that returns the rest of the division you can use it as well.

int resto, dividendo, divisor;
resto = dividendo % divisor;
    
22.09.2015 / 16:40