Write C program to swap first and last digit of a number

Write C program to swap first and last digit of a number


Introduction

Write C program to swap first and last digit of a number


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int main(void) {
  6.  
  7. int num, last, first, temp, swap, count = 0;
  8.  
  9. printf("Enter any number: ");
  10. scanf("%d", &num);
  11.  
  12. temp = num;
  13. last = temp % 10;
  14. count = (int)log10(temp);
  15.  
  16. while(temp>=10)
  17. {
  18. temp /= 10;
  19. }
  20. first = temp;
  21. swap = (last * pow(10, count) + first) + (num - (first * pow(10, count) + last));
  22.  
  23. printf("Last Digit: %d\n", last);
  24.  
  25. printf("First Digit: %d\n", first);
  26.  
  27. printf("%d is swapped to %d\n", num, swap);
  28. return 0;
  29. }
  30.  

Result

C language

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.