Swap the first and last digits of a number with C.
#include <stdio.h>
#include <math.h>
int main()
{
int num, swappedNum;
int firstDigit, lastDigit, digits;
printf("Enter any number: ");
scanf("%d", &num);
lastDigit = num % 10;
digits = (int) log10(num);
firstDigit = (int) (num / pow(10, digits));
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: 456798
Original number = 456798
The number after swapping first and last digit: 856794
0 Comments