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.

Input any number: 12345

Output after swapping: 52341

Happy coding ;)

**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 numberBegin:read(num)lastDigit←num% 10;digits← log10(num);firstDigit←num/ pow(10,digits);swappedNum←lastDigit* pow(10,digits);swappedNum←swappedNum+ num % pow(10,digits) -lastDigit;swappedNum←swappedNum+ 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

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

### You may also like

- Basic programming exercises index.
- C program to find sum of first and last digits of a number.
- C program to find sum of all digits of a number.
- C program to find product of digits.
- C program to count total number of notes in a given amount.
- C program to convert degree Fahrenheit to Centigrade.
- C program to find GCD of two numbers.
- C program to find LCM of two numbers.
- C program to find factorial of a given number.
- C program to find 2's complement of a binary number.
- C program to convert from binary to hexadecimal number system.
- C program to find profit or loss.
- C program to print pascal triangle.

< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).