C program to swap first and last digit of a number

Previous Program Next Program

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

Logic to swap first and last digit

Swapping first and last digit of any number is little tricky. You need to apply some basic maths to swap first and last digit. But before you actually perform the swap operation, you must know how to find 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;

    // Read 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;

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

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment