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) lastDigit ← num % 10; digits ← log10(num); firstDigit ← num / pow(10, digits); swappedNum ← lastDigit * pow(10, digits); swappedNum ← swappedNum + num % pow(10, digits); swappedNum ← swappedNum - lastDigit; swappedNum ← swappedNum + 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; }
Enter any number: 1234 Original number = 1234 Number after swapping first and last digit: 4231
Happy coding ;)
You may also like
- Loop programming exercises index.
- C program to count number of digits in a number.
- C program to find first and last digit of any number.
- C program to find sum of digits of any number.
- C program to find product of digits of any number.
- C program to find sum of first and last digit of any number.
- C program to print a given number in words.