Javatpoint Logo
Javatpoint Logo

Swap first and last digit of a number in C

Programming situations frequently require us to alter numbers to produce the desired results. Swapping a number's initial and last digits is one example of this. Although this procedure may appear straightforward at first, accuracy is only possible with a methodical approach. In this article, we will study how to swap the first and last digits of a number in the C programming language. We will do this by offering code examples, syntax explanations, and sample outputs.

Let's first grasp the issue at hand before moving on to the answer. Our objective is to swap the initial and last digits of a positive integer handed to us. To achieve this, we must carry out a sequence of mathematical and logical procedures.

Approach and Solution:

We shall take the following actions to swap a number's initial and last digits:

Step 1: It is to read the user-inputted number.

Step 2: Determine how many digits there are in the provided number.

Step 3: Take off the number's leading and trailing digits.

Step 4: Determine the first digit's position value.

Step 5: Determine the position value of the final digit.

Step 6: Using temporary variables, switch the first and last digits.

Step 7: Create the final swapped number.

Step 8: Show the swapped number as the output.


Let's now examine how the stages would be implemented in code using the C programming language:


Enter a positive integer: 9348 
Swapped number: 8349


The snprintf function counts the digits in the inputted number. It gives back how many characters would have been written overall had the string been long enough. The printf and scanf functions employ the format specifiers %d and %f to read and print integers and floating-point numbers, respectively.


Another approach to switching a number's first and last digits in C is as follows:


Enter a positive integer: 75624
Swapped number: 45627


The program prompts the user to enter a positive integer (in this case, 75624). It determines the swapped number, 45627, by acting appropriately. After that, the output of the program shows the switched number.

By dividing the input number by 10 until it equals 0, we may calculate the number of digits using this alternative method. After that, the first and last digits are extracted using modulo and division operations. Finally, the swapped number is created by rearrangement of the digits.


Programmers frequently must switch a number's first and last digits, and knowing how to do it can be helpful in a variety of situations. You can complete this task by following the step-by-step process described in this blog post and implementing the accompanying C code. To ensure correct results, keep in mind to pay attention to the syntax and logic of the code.

The core principles of programming are problem-solving and the division of difficult tasks into smaller, more achievable steps. You may improve your programming abilities and solve trickier tasks with patience and perseverance.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA