Swap first and last digit of a number
C program to swap first and last digit of a number.
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num, swap_num, temp;
int last_digit, first_digit, count;
int i;
printf("Enter the number\t:");
scanf("%d",&num);
temp = num;
count = 0;
while(temp>0)
{
count++;
temp = temp/10;
}
last_digit = num % 10;
first_digit = (int)(num / pow(10,count-1));
swap_num = (last_digit * (int)(pow(10, count-1)) + first_digit) + ((num - (first_digit * (int)(pow(10, count-1)))) - last_digit);
printf("\nNumber after swapping first and last digit: %d", swap_num);
getch();
}
Output
Enter the number :3542
Number after swapping first and last digit: 2543