C program to swap first and last digit of a number

Write a C program to input a number from user and swap the first and last digit of the given number. How to swap first and last digits of any 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 read - Program to find first and last digit

Logic to swap first and last digit

Below is the algorithm to swap first and last digit.

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 a strong 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

/**
 * 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);

    lastDigit  = num % 10; // Get last digit
    digits     = (int)log10(num); // Total number of digit - 1
    firstDigit = (int)(num / pow(10, digits)); // Get the first digit

    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);

    lastDigit  = num % 10; // Get last digit
    digits     = (int) log10(num); // Total number of digit - 1
    firstDigit = (int) (num / pow(10, digits)); // Get the first digit

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

Output

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

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>