C program to swap first and last digit of a number

Write a C program to input any number from user and swap the first and last digit of the given number. How to swap first and last digits of any given number in C programming. C program for swapping first and last digit of an integer.

Example:
Input any number: 12345
Output after swapping: 52341

Required knowledge

Basic C programming

Logic to swap first and last digits

Swapping first and last digits of a number is little tricky. But if you can apply some basic logic it can be solved easily. Before continuing you must be done with program for finding first and last digits of a number. Here is what you need to do.
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) - lastDigit;
    swappedNumswappedNum + firstDigit;
End


Program to swap first and last digits

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

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

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

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

    printf("\nOriginal number = %d", num);
    printf("\nNumber 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 digits

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

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

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

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

    printf("\nOriginal number = %d", num);
    printf("\nNumber 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

Note: You can try a dry run of the above logic to get a quick grasp over the program.


Happy coding ;)


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
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment