I’m in doubt how to do

Asked

Viewed 119 times

3

How to module into some mathematical function in C? I tried some basic things but it didn’t work.

  • 6

    post your code there, or things you’ve tried and failed, be clearer on the question... This helps you get faster and more accurate answers.

  • The module you speak of is the rest of the division? It would be 5%2 that would result in 1, that?

  • 3

    Is there a specific problem you’re having in some code? Post it. If you want only generically about the use of the module operator, you have a question from a few minutes ago that answers this: http://answall.com/questions/88260/problema-em-c-howto fin-o-resto-da-divis%C3%a3o

  • 1

    Your question is a little strange, I recommend you edit it and put something like "How to calculate the rest of the division in C?" or "How to use the module function in C?" so it will help other people in the future with the same doubt.

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 make 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 doubt refers to the use of the module operator (%) which returns the rest of the division can use so.

int resto, dividendo, divisor;
resto = dividendo % divisor;

Browser other questions tagged

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