fork download
  1. //Q41. Write a program to swap the first and last digit of a number.
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. int main() {
  7. int num, firstDigit, lastDigit, digits, swappedNum;
  8. printf("Enter a number: \n");
  9. scanf("%d", &num);
  10.  
  11. lastDigit = num % 10;
  12. digits = (int)log10(num);
  13. firstDigit = num / (int)pow(10, digits);
  14.  
  15. swappedNum = lastDigit * (int)pow(10, digits) +
  16. (num % (int)pow(10, digits)) - lastDigit + firstDigit;
  17.  
  18. printf("Number after swapping first and last digit: %d\n", swappedNum);
  19. return 0;
  20. }
  21.  
Success #stdin #stdout 0s 5280KB
stdin
1234
stdout
Enter a number: 
Number after swapping first and last digit: 4231