Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Example 1
2.1.
Algorithm
2.2.
Code
2.3.
C
3.
Example 2
3.1.
Algorithm
3.2.
Code
3.3.
C
4.
Frequently Asked Questions
4.1.
How to swap the first and last digit in C?
4.2.
How to find the first and last digit of a number in C?
4.3.
What are the header files required to execute the code for swapping the first and last digit of a number?
4.4.
In which header files Log10() function is present?
4.5.
What is the time and space complexity for swapping a number's first and last digit?
5.
Conclusion
Last Updated: May 7, 2024
Easy

Program to Swap First and Last Digit Of a Number In C

Author Ankit Kumar
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Swapping the first and last digits of a number is a common task in programming, often encountered in various mathematical and algorithmic problems. In this blog, we delve into a simple yet effective program to accomplish this task using the C programming language.

Program to Swap First and Last Digit Of a Number In C

Also Read, Binary to Hex Converter and C Static Function.

Example 1

To swap the first and last digit of a number, firstly, we need to input a number from the user. We will declare a type ‘int’ variable and store the user input value for inputting a number. 

Our task now is to know the count of total digits in the input number. 

After learning the count of digits, we will calculate the number's first digit by dividing the input number by 10 to the power of total digits in the number minus one. 

Similarly, we will find out the last digit of the input number with the help of the modulo operator. After finding out the first and last digit of the input number, we will swap them, and afterwards, we will print the result. 

Let us look at the algorithm and the code for this problem to get a clear understanding of the approach.

Algorithm

Step 1 - Declare the variables Num, First_Digit, Digits_Count, Last_Digit, x, y, Swap_Num.

Step 2 - Input a number and store it in Num.

Step 3 - Perform log10 on Num and store it in Digits_Count.

Step 4 - Set First_Digit = Num/pow(10, Digits_Count).

Step 5 - Set Last_Digit = Num % 10.

Step 6 - Set x = First_Digit * (pow(10, Digits_Count)).

Step 7 - y = Num % x.

Step 8 - Num = y / 10.

Step 9 - Swap_Num = Last_Digit * (pow(10, Digits_Count)) + (Num * 10 + First_Digit).

Step 10 - Print the result stored in Swap_Num.

Also read reverse a number.

Code

  • C

C

// A ‘C’ Program to Swap the First and Last Digit Of a User-defined Number 
#include <stdio.h>
#include <math.h>
int main()
{
int Num, First_Digit, Digits_Count, Last_Digit, x, y, Swap_Num;

printf("\n Enter the number : ");
scanf("%d", & Num);
Digits_Count = log10(Num); 
  //Finding the first digit
First_Digit = Num / pow(10, Digits_Count);
//Finding the last digit
Last_Digit = Num % 10;
//Swapping the first digit and last digit.
x = First_Digit * (pow(10, Digits_Count));
y = Num % x;
Num = y / 10;
//Storing the result
Swap_Num = Last_Digit * (pow(10, Digits_Count)) + (Num * 10 + First_Digit);
printf(" \n The Number after Swapping the First Digit and Last Digit =  %d", Swap_Num);

return 0;
}

The above code will produce the following output.

Enter the number: 12356

 The Number after Swapping the First Digit and Last Digit =  62351

Program finished with exit code 0

Press ENTER to exit console.

Let us now discuss the second example of swapping the first and last digit of a number and try it to implement on C compiler.

You can also read about dynamic array in c, and Tribonacci Series  You can also read about - Strong number in c

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example 2

The flow of this example will remain the same. First, we will scan the input from the user and in a similar fashion we will find out the first digit and last digit of the input number. The only change comes in the swapping method. Let us understand that with the help of the following algorithm and its code.

Algorithm

Step 1 - Declare the variables Num, First_Digit, Digits_Count, Last_Digit, x, y, Swap_Num.

Step 2 - Input a number and store it in Num.

Step 3 - Perform log10 on Num and store it in Digits_Count.

Step 4 - Set First_Digit = Num/pow(10, Digits_Count).

Step 5 - Set Last_Digit = Num % 10.

Step 6 - Set Swap_Num = Last_Digit.

Step 7 - Swap_Num = Swap_Num  * (round(pow(10, Digits_Count))).

Step 8 - Swap_Num = Swap_Num + Num % (int)(round(pow(10, Digits_Count))).

Step 9 - Swap_Num = Swap_Num - Last_Digit.

Step 10 - Swap_Num = Swap_Num + First_Digit.

Step 11 - Display the result stored in Swap_Num.

Code

  • C

C

// C Program to Swap First and Last Digit Of a Number 
#include <stdio.h>
#include <math.h>
int main()
{
int Num, First_Digit, Digits_Count, Last_Digit, x, y, Swap_Num;
printf("\nEnter the number : ");
scanf("%d", & Num);
Digits_Count = log10(Num); 
First_Digit = Num / pow(10, Digits_Count);
Last_Digit = Num % 10;
Swap_Num = Last_Digit;
Swap_Num = Swap_Num  * (round(pow(10, Digits_Count)));
Swap_Num = Swap_Num + Num % (int)(round(pow(10, Digits_Count)));
Swap_Num = Swap_Num - Last_Digit;
Swap_Num = Swap_Num + First_Digit;
printf(" \n The Number after Swapping First Digit and Last Digit is =  %d", Swap_Num);
return 0;
}

The above code will produce the following output.

Enter the number : 1256 

 The Number after Swapping First Digit and Last Digit is =  6251

...Program finished with exit code 0

Press ENTER to exit console.

So with the end of this example and you can try by yourself with the help of an online C editor. we can now answer some of the frequently asked questions related to this topic.

Click on the following link to read further: Features of C LanguageShort int in C Programming

Frequently Asked Questions

How to swap the first and last digit in C?

To swap the first and last digit in C, extract the first and last digits, swap them, and update the number.

How to find the first and last digit of a number in C?

To find the first and last digit of a number in C, use modulo and division operations to extract them.

What are the header files required to execute the code for swapping the first and last digit of a number?

You must add maths.h and iostream.h header files to execute the code for swapping the first and last digit of a number.

In which header files Log10() function is present?

It is a mathematical function, and thus it is present in maths.h header file.

What is the time and space complexity for swapping a number's first and last digit?

The time complexity is constant, and we do not consume any extra space to swap the last and first digit of a number.

Conclusion

In this article, we extensively discussed the program for swapping the first and last digit of a number and implemented that in C. We discussed two different approaches to swap these numbers and also implemented them and displayed the output. Do not stop here and learn the difference between C and C++Difference between c and embedded c here.

Check out this problem - Find Duplicate In Array

We hope that this blog has helped you enhance your knowledge regarding swapping the first and last digit of a number in C and if you would like to learn more.

Previous article
Program to Count Total Duplicate Elements in an Array
Next article
C Program to Delete Duplicate Elements from the Array.
Live masterclass