Table of contents
1.
Introduction
2.
Example 1
2.1.
Algorithm
2.2.
Implementation
2.3.
C
3.
Example 2
3.1.
Algorithm
3.2.
Implementation
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: Dec 23, 2024
Easy

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

Author Ankit Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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 

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.

Implementation

  • 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;
}
You can also try this code with Online C Compiler
Run Code

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

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.

Implementation

  • 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;
}
You can also try this code with Online C Compiler
Run Code

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 Language

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.

Live masterclass