Partitioning the algorithm into functions

1

The problem is as follows:

  

A function receives an int as a parameter, and returns it written to the contrary and another to test if the input number and the inverted number is palindrome.

I was able to solve the problem of identifying if the input number is palindrome, but I need to split the code into functions according to what was requested.

So far my code looks like this:

int i, j;

scanf("%d", &i);

int n = i;
int l = 0;

while(n != 0){
    j = n % 10;
    l = (l * 10) + j;
    n = n / 10;
}

if(i == l)
    printf("sim");
else
    printf("nao");
    
asked by anonymous 31.10.2018 / 02:56

1 answer

3

I separated the interaction part with the user with the algorithm itself and made the communication through parameter and return.

#include <stdio.h>

int Inverte(int n) { //n é um parâmetro que recebe o que foi passado (digitado)
    int l = 0, j;
    while (n != 0) {
        j = n % 10;
        l = (l * 10) + j;
        n /= 10;
    }
    return l; //retorna o valor invertido que poderá ser usado onde precisa
}
int main(void) {
    int i;
    scanf("%d", &i);
    printf(i == Inverte(i) ? "sim" : "nao"); //em vez de comparar uma variável verifica com o retorno da função que é chamada com o que foi digitado como argumento
}

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

    
31.10.2018 / 12:03