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


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int num, last, first, temp, swap, count = 0;
  9.  
  10. //Reading a number from user
  11. cout<<"Enter any number:";
  12. cin>>num;
  13.  
  14. temp = num;
  15. last = temp % 10;
  16. count = (int)log10(temp);
  17.  
  18. while(temp>=10)
  19. {
  20. temp /= 10;
  21. }
  22. first = temp;
  23. swap = (last * pow(10, count) + first) + (num - (first * pow(10, count) + last));
  24.  
  25. cout<<"Last Digit: "<<last<<endl;
  26.  
  27. cout<<"First Digit: "<<first<<endl;
  28.  
  29. cout<<num<<" is swapped to "<<swap;
  30.  
  31. return 0;
  32. }
  33.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts