Swap number digit order by separate a number into arrays and then merge

Viewed 68

I can't elaborate a program with arrays language C.

The console received 4 numbers. I want to change the first number digits and multiply with the other.

Example input: 1260

Desire output: Change 12 to 21 and them multiple by 60 -> so output will be 1260 (as 21 * 60)

This is my current code:

int main() {
    int number, temp;
    int newnumber[4];
    int n = 3;

    printf("put the number");
    scanf("%d", &number);

    do {
        newnumber[n] = number % 10;
        number = number / 10;
        n--;
    } while (n >= o);

    temp = newnumber[1];
    newnumber[1] = newnumber[2];
    newnumber[2] = temp;
}

know how i do 21 multiply with 60?

4 Answers

I would have go with slightly different approach: first separate the numbers. Then call function to change the number order.

If you always get numbers with 2 digit you can do this:

int first  = number / 100;
int second = number % 100;

And a function to swap the digit:

function swapDigits(int num) {
    int ans = 0;
    while (num > 0) {
        ans = ans * 10 + num % 10;
        num /= 10; 
    }
    return ans;
}

Now just do second * swapDigits(first) to get your result.

I'm not c expert so verify my code before use...

If we look at your example: 1260 => change 2 with 1, and multiply 21 with 60.

The permutation in your main function is wrong, cause you changed numbers at the index 1 (second position) and 2 (third position).

Back to your question, you can get the result you're looking for by doing the oppisite of what you did to get the units, tens and hundreds...

int main() {
    int number, temp1, temp2;
    int newnumber[4];
    int n = 3;

    printf("put the number");
    scanf("%d", &number);

    do {
        newnumber[n] = number % 10;
        number = number / 10;
        n--;
    } while (n >= 0);

    temp1 = newnumber[0];
    newnumber[0] = newnumber[1];
    newnumber[1] = temp1;

    temp1 = newnumber[0] * 10;
    temp1 += newnumber[1];

    temp2 = newnumber[2] * 10;
    temp2 += newnumber[3];

    printf("%d", temp1 * temp2);
}

If your conditions always hold you can keep it simple and do something like this:

int main() {
    int number;
    int newnumber[4];
    int n = 3;

    printf("put the number");
    scanf("%d", &number);

    do {
        newnumber[n] = number % 10;
        number = number / 10;
        n--;
    } while (n >= 0);

    printf("And the result: %d\n", (newnumber[1] * 10 + newnumber[0]) * (newnumber[2] * 10 + newnumber[3]));
}

Then you are not getting the individual digits, but pairs and the like. Just get the last two digits in one shot:

int rem = number % 100;  /* last two digits */
number /= 100;
int msd = number % 10;   /* next, third digit */
number /= 10;
int lsd = number % 10;   /* most significant digit */
/* I don't assume you have more digits, because you are doing different
 * operations with them, no pattern up to here, but you should continue
 * your approach here. */
int out = (msd * 10 + lsd) * rem;

should give you a solution. No arrays needed.

Related