Tech Study

Write Program To swap First and Last Digit of a Number C++

Introduction:

program to swap first and last digit of a number in c++

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

#include <iostream>
#include <math.h>
 
using namespace std;
 
int main()
{
    int num, last, first, temp, swap, count = 0;
 
    //Reading a number from user
    cout<<"Enter any number:";
    cin>>num;
 
    temp = num;
    last = temp % 10;
    count = (int)log10(temp);
 
    while(temp>=10)
    {
        temp /= 10;
    }
    first = temp;
    swap = (last * pow(10, count) + first) + (num - (first * pow(10, count) + last));
 
    cout<<"Last Digit: "<<last<<endl;
 
    cout<<"First Digit: "<<first<<endl;
 
    cout<<num<<" is swapped to "<<swap;
 
    return 0;
}

Result

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

 To Learn more about c++ visit these links :

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

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more