C program to swap first and last digit of a number

Write a C program to input a number from user and swap first and last digit of the given number. How to swap first and last digits of a number in C programming. Logic to swap first and last digit of a number in C program.

Example

Input

Input any number: 12345

Output

Number after swapping first and last digit: 52341

Required knowledge

Basic C programming, Basic Mathematics

Must know - Program to find first and last digit

Logic to swap first and last digit of a number

Logic to swap first and last digit of a number
Begin:
    read(num)
    lastDigitnum % 10;
    digits ← log10(num);
    firstDigitnum / pow(10, digits);
    
    swappedNumlastDigit * pow(10, digits);
    swappedNumswappedNum + num % pow(10, digits);
    swappedNumswappedNum - lastDigit;
    swappedNumswappedNum + firstDigit;
End

Let us do a dry run of the algorithm to get grip on the logic.

Suppose num = 12345
--------------------
lastDigit  = 12345 % 10 => 5
digits     = log10(12345) => 4
firstDigit = 12345 / pow (10, 4) => 12345 / 10000 => 1

swappedNum = 5 * pow(10, 4) => 5 * 10000 => 50000
swappedNum = 50000 + (12345 % 10000) => 50000 + 2345 => 52345
swappedNum = 52345 - 5 => 52340
swappedNum = 52340 + 1 => 52341

Program to swap first and last digit of a number

/**
 * C program to swap first and last digit of a number
 */

#include <stdio.h>
#include <math.h>

int main()
{
    int num, swappedNum;
    int firstDigit, lastDigit, digits;

    /* Input number from user */
    printf("Enter any number: ");
    scanf("%d", &num);

    /* Find last digit */
    lastDigit  = num % 10;

    /* Find total number of digit - 1 */
    digits     = (int)log10(num); 

    /* Find first digit */
    firstDigit = (int)(num / pow(10, digits)); 

    swappedNum  = lastDigit;
    swappedNum *= (int) pow(10, digits);
    swappedNum += num % ((int) pow(10, digits));
    swappedNum -= lastDigit;
    swappedNum += firstDigit;

    printf("Original number = %d", num);
    printf("Number after swapping first and last digit: %d", swappedNum);

    return 0;
}

Note: In some compiler the above program may not produce valid results for some inputs. You may use the below program with similar logic with a little change in the code.

Program to swap first and last digit of a number

/**
 * C program to swap first and last digit of a number
 */

#include <stdio.h>
#include <math.h>

int main()
{
    int num, swappedNum;
    int firstDigit, lastDigit, digits;

    /* Input a number from user */
    printf("Enter any number: ");
    scanf("%d", &num);

    /* Find last digit */
    lastDigit  = num % 10; 

    /* Total number of digit - 1 */
    digits     = (int) log10(num); 

    /* Find first digit */
    firstDigit = (int) (num / pow(10, digits));

    swappedNum  = lastDigit;
    swappedNum *= (int) round(pow(10, digits));
    swappedNum += num % ((int)round(pow(10, digits)));
    swappedNum -= lastDigit;
    swappedNum += firstDigit;

    printf("Original number = %d", num);
    printf("Number after swapping first and last digit: %d", swappedNum);

    return 0;
}

Note: In the above program I have used three mathematical function pow(), log10() and round().

  • pow() is used to find power of a number.
  • log10() is used to find log base 10 value of the passed parameter.
  • round() function is used to round a number to nearest integer.

Output

Enter any number: 1234
Original number = 1234 
Number after swapping first and last digit: 4231

Happy coding 😉

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.

Follow on: Facebook | Twitter | Google | or

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>