Nandakumar
Content Network

You are not logged in.
Click here to login.

Some part of this library is not public. You must register to get exclusive access. If you are already registered, click here to login.


C Programming >> 2017

C Program to Interchange the First and Last Digits of a Number

By | | License: proprietary


Search the Web for a sample C program that interchanges the first and last digits of a number, and you can find many on different websites. Most of them employ a beautiful mathematical approach based on log10 (or some similar feature to get the number of digits), and perform the swapping using additional math. But unfortunately, those who wrote such programs miss a bug -- this method won't work for numbers having trailing zeros. For example, an input of 1020 will produce the output 21, which is incorrect.

That's why I post my version here. It uses array, less fashionable compared to some math trick, but it works. Yes, this version also has limits. It can take trailing zeros, but it will trim the leading zeros, if any (e.g.: 01020 is considered just 1020). It isn't a problem since leading zeros has no mathematical significance. Also, it reprints the original input without the leading zeros in the output line, justifying what it does.

Yes, you can write another version considering the leading zeros, if you receive the input as a string instead of an integer. But suddenly the program will loose all of its math (especially the idea of digit extraction), and that's why I'm not doing it here.

/* * C porgram to interchange the first and last digits of a number * Nandakumar Edamana * 8 Oct 2017 * The log10 or similar arithmetic methods won't work for 1020; that's why array is used. */ #include <stdio.h> int main() { int n, n_bak, digits[10], count = 0, i; printf("Enter a no. (max. 10 digits): "); scanf("%d", &n); n_bak = n; /* Digit extraction */ while(n) { digits[count ++] = n % 10; n /= 10; } printf("%d after interchanging the first and last digits is ", n_bak); printf("%d", digits[0]); for(i = count - 2; i > 0; i --) { printf("%d", digits[i]); } printf("%d", digits[count - 1]); puts(""); /* Put a newline */ return 0; }

The while loop under the comment /* Digit extraction */ simply extracts the digits in the number and stores them in reverse order in the array digits. For example, after running this loop for the input 12345, the array is something like this: [5, 4, 3, 2, 1]. Now comes the output part. Printing digits[0] will give you the last digit of the input. Now we have to print the rest of the array elements (excluding the last one, which is the first digit, actually) in reverse order (because the digits are stored in reverse order). That's why there is a for loop. After that, we print digits[count - 1], the last element of the array, which happens to be the first digit.

If you are confused with the line digits[count ++] = n % 10;, have a look at its simplified version:

digits[count] = n % 10;
count = count + 1;

It simply means add the last digit of the number to the [current] last position of the array. Note that the number looses its last digit in each iteration (because of n /= 10 or n = n/10), and count gets increased.


Keywords (click to browse): swapping interchanging first-and-last-digits digits digit-extraction array log10 log c programming examples sample-code system-software

C Program to Interchange the First and Last Digits of a Number | Nandakumar Content Network
Copyright © 2015, 2016, 2017 Nandakumar Edamana.