As per module in some mathematical function in C? I tried some basic things but it did not work.
As per module in some mathematical function in C? I tried some basic things but it did not work.
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;