1

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

  • I would separate the calculations more to make them easier to follow, like `units = K %10; tens = ...; hundreds = ...;` and then when the digits are all separate, combine them again `inv = 100 * units + 10 * tens + hundreds;`. – Bo Persson Jan 02 '17 at 19:22
  • You're not very clear as to what you're asking. Is `123-321` a string you receive and have to verify it's a palindrome? of you you receive `123` and have to return `321`? Will the numbers you receive always 3 digits long? – AntonH Jan 02 '17 at 19:24
  • I do not need this to write a program, but I would draw an algorithm for the number of 3 digits. It is good way or not? my english is suck sorry xD – Nemanja Milenovic Jan 02 '17 at 19:25
  • From a logic standpoint, what you wrote **looks** correct, but is a very specific solution to the problem. If you code it, you may have to be careful with operation precedence, though, so may need to add brackets for clarification. – AntonH Jan 02 '17 at 19:30
  • Convert the number to a string (library function). Reverse the string. Convert that string to another number (library function) - but watch out for [octal](https://en.wikipedia.org/wiki/Octal) if a leading `0` is present! Then subtract the two numbers. – Weather Vane Jan 02 '17 at 19:31
  • `K%100` typo as `K/100` – BLUEPIXY Jan 02 '17 at 19:43
  • If you need to reverse a number, keep it as a string, then use methods to revers the string. – Thomas Matthews Jan 02 '17 at 20:10
  • Yes, if you only need to print the reversed number and not to use it as a number, then it has sense to read and reverse directly as string. But do not **convert** it to string and back, because that is very wasteful compared to doing it directly in int (dynamic memory allocation for string etc.). – EmDroid Jan 02 '17 at 20:17

3 Answers3

3

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;                
        }
1

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() );
}
jpo38
  • 20,821
  • 10
  • 70
  • 151
  • I'd prefer to do it directly in integer instead of doing unnecessary conversions to/from string (involving dynamic memory allocations btw.). – EmDroid Jan 02 '17 at 20:06
0
#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

Amjad
  • 3,110
  • 2
  • 20
  • 19
  • In each iteration of the while loop `i%10` will extract the first digit from right. This digit is carried over to `k` by `j` and we remove the digit from `i` by using integer division. – Amjad Jan 02 '17 at 19:46
  • 2
    This will not work for number containing zero, e.g. 12305, the termination condition is wrong. – EmDroid Jan 02 '17 at 20:03
  • Modified; Thanks for bringing this up! – Amjad Jan 02 '17 at 20:10