The algorithm inverse number

1k views Asked by At

Is this a formula for the inverse number?

eg 123-321?

Number is K

inv K= K%100 + K/10%10 * 10 + K % 10 *100

I'm not sure I exactly wrote, and I need this right for the task at school for graphic algorithm

Edit:Yes i'm stupid xD I'm Beginner xD

3

There are 3 answers

2
Ankit Agarwal On

This logic will help you. variable inverse is the output.

        int num = 123;
        double inverse = 0;
        while (num != 0)
        {
            inverse = inverse * 10;
            inverse = inverse + num % 10;
            num = num / 10;                
        }
3
Amjad On
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int i = 123045 , j = 0,k=0;

    while( i != 0 )
        {
        j=i%10;
        k = k *10 + j;
        i /=10;
    }

    printf("%d\n", k);
    return 0;
}

Output 540321

1
jpo38 On

If you want your code to work with any number, consider converting to a string and then reversing it!

int invert( int input )
{
    std::stringstream str;
    str << input;

    std::string s = str.str();
    std::reverse(s.begin(),s.end());

    return atoi( s.c_str() );
}