# C Program to Interchange the Digits of a Number

« Previous Program Next Program »

In this article, you will learn and get code about interchanging digits of a number given by user at run-time. Interchanging digits of a number has been created in following ways:

• Interchange First and Last Digit of a Number
• Interchange First and Last Digit of a Number using Array
• Interchange any two Digit (given by user with its position) of a Number

## Interchange First and Last Digit of a Number

Let's first create a program in C that interchanges first and last digit of a given number.

```#include<stdio.h>
#include<conio.h>
int main()
{
int num, rem, temp, rev=0, noOfDigit=0, noOfDigitTemp, revNum, remTemp;
printf("Enter the Number: ");
scanf("%d", &num);
temp = num;
while(temp>0)
{
temp = temp/10;
noOfDigit++;
}
if(noOfDigit<2)
{
printf("\nIt's a single-digit number.");
printf("\nTo interchange the digit, enter a two or more digit number");
}
else if(noOfDigit==2)
{
temp = num;
while(temp>0)
{
rem = temp%10;
rev = (rev*10)+rem;
temp = temp/10;
}
printf("\nFirst and Last Digit Interchanged Successfully!");
printf("\n\nNew Number = %d", rev);
}
else
{
temp = num;
while(temp>0)
{
rem = temp%10;
rev = (rev*10)+rem;
temp = temp/10;
}
revNum = rev;
rev = 0;
temp = num;
noOfDigitTemp = noOfDigit;
while(temp>0)
{
remTemp = revNum%10;
if(noOfDigitTemp==noOfDigit)
{
rem = temp%10;
rev = (rev*10)+rem;
}
else if(noOfDigitTemp==1)
{
rem = temp%10;
rev = (rev*10)+rem;
}
else
{
rev = (rev*10)+remTemp;
}
temp = temp/10;
revNum = revNum/10;
noOfDigitTemp--;
}
printf("\nFirst and Last Digit Interchanged Successfully!");
printf("\n\nNew Number = %d", rev);
}
getch();
return 0;
}```

This program was build and run under Code::Blocks IDE. Here is its sample run:

Now supply any number say 12345 and press `ENTER` key to see the following output:

The following block of code:

```while(temp>0)
{
temp = temp/10;
noOfDigit++;
}```

is used to count total number of digits available in given number. For example, if user enters 12345 as number, then total number of digit is 5. If total digit will be less than 2, then program will print a message like, single digit number can not be interchanged. That is, to interchange, it must be a two-digit or more than two-digit number. If the given number is a two-digit number, then simply reverse the number. For example, if the number is 12 then after reversing, it will become 21. See, its digit (first and last) gets interchanged.

If there are more than 2 digit available in given number, then program flow goes inside the else block. There, the main logic is:

• Let's suppose, user has entered 12345 as number input
• Reverse it and store its reverse in rev variable that will hold 54321
• Now we have to reverse 1 (first digit) and 5 (last digit) only
• As you can see, rev holds 54321 where the first and last digit gets reversed, but rest of the three digit present in middle also gets reversed
• Therefore, perform the reverse operation for first and last digit on original number, and for rest of the digit, perform the reverse operation on reversed number.
• That is, original number is 12345 and reversed number is 54321. And performing the reverse operation on first and last digit (on original number) with reverse operation on rest of the digit (on reversed number) gives you 52341

The dry run of previous program with user input 12345 goes like:

• Initially, num=12345, noOfDigit=5, rev=0
• Inside else block
• temp=num or temp=12345
• Using while loop, number gets reversed and stored in rev
• Therefore, rev=54321
• revNum=rev or revNum=54321, rev=0, temp=num or temp=12345, noOfDigitTemp=noOfDigit or noOfDigitTemp=5
• Now program flow goes inside the while loop
• Condition temp>0 or 12345>0 evaluates to be true
• Program flow goes inside the loop
• remTemp=revNum%10 or remTemp=54321%10 or remTemp=1
• The condition noOfDigitTemp==noOfDigit or 5==5 of if block evaluates to be true
• Program flow goes inside the if block
• rem=temp%10 or rem=12345%10 or rem=5
• rev=(rev*10)+rem or rev=(0*10)+5 or rev=5
• Then temp=temp/10 or temp=12345/10 or temp=1234
• revNum=revNum/10 or revNum=54321/10 or revNum=5432
• noOfDigitTemp-- means 1 gets subtracted the previous value of noOfDigitTemp. Therefore noOfDigitTemp=4
• Program flow again goes to condition of while loop
• That is temp>0 or 1234>0 evaluates to be true, therefore program flow again goes inside the loop
• remTemp=revNum%10 or remTemp=5432%10 or remTemp=2
• Because noOfDigitTemp is equal to 4, and 4 is not equal to noOfDigit (5) and 1. Therefore program flow goes inside else block
• And rev=(rev*10)+remTemp or rev=(5*10)+2 or rev=52
• Then temp=temp/10 or temp=1234/10 or temp=123
• revNum=revNum/10 or revNum=5432/10 or revNum=543
• noOfDigitTemp-- means 1 gets subtracted the previous value of noOfDigitTemp. Therefore noOfDigitTemp=3
• Now again program flow again goes to condition of while loop
• On continuing the same process, here is the one by one updated value of each variable
• remTemp=revNum%10 or remTemp=543%10 or remTemp=3
• In else block again, rev=(rev*10)+remTemp or rev=(52*10)+3 or rev=523
• Then temp=temp/10 or temp=123/10 or temp=12
• revNum=revNum/10 or revNum=543/10 or revNum=54
• noOfDigitTemp=2
• Again, remTemp=revNum%10 or remTemp=54%10 or remTemp=4
• In else block again, rev=(rev*10)+remTemp or rev=(523*10)+4 or rev=5234
• Then temp=temp/10 or temp=12/10 or temp=1
• revNum=revNum/10 or revNum=54/10 or revNum=5
• noOfDigitTemp=1
• Again, remTemp=revNum%10 or remTemp=5%10 or remTemp=5
• Because noOfDigitTemp (holds 1) equal to 1. Therefore program flow goes inside if-else block
• rem=temp%10 or rem=1%10 rem=1
• rev=(rev*10)+rem or rev=(5234*10)+1 or rev=52341
• Then temp=temp/10 or temp=1/10 or temp=0
• revNum=revNum/10 or revNum=5/10 or revNum=0
• noOfDigitTemp=0
• This time the condition of while loop, temp>0 or 0>0 evaluates to be false, therefore program flow exit loop and prints the new number as output

Note - Previous program has a limitation that, if user enters a number that starts or ends with 0. So to overcome this problem. We have created another program using array. That will be the complete solution of interchanging the digits of a number.

### Using Array

Here is another program that does the same job but using array. Using array, the program becomes easier to create and understand. Just reverse the number first. And one by one, initialize all the digit to array. Then interchange the number at 0th index with number at last index. Reverse of a number is required, because to get digit, one by one using rem=num%10, we will get the last digit of a number. But after reversing the number, we will get last digit of reversed number (but first digit of original number).

```#include<stdio.h>
#include<conio.h>
int main()
{
int num, rem, temp, rev=0, noOfDigit=0, arr, i;
printf("Enter the Number: ");
scanf("%d", &num);
temp = num;
while(temp>0)
{
temp = temp/10;
noOfDigit++;
}
temp = num;
while(temp>0)
{
rem = temp%10;
rev = (rev*10)+rem;
temp = temp/10;
}
for(i=0; i<noOfDigit; i++)
{
rem = rev%10;
arr[i] = rem;
rev = rev/10;
}
if(noOfDigit==1)
{
printf("\nIt's a single-digit number.");
printf("\nTo interchange the digit, enter a two or more digit number");
}
else if(noOfDigit==2)
{
temp = arr;
arr = arr;
arr = temp;
printf("\nFirst and Last Digit Interchanged Successfully!");
printf("\n\nNew Number = %d%d", arr, arr);
}
else
{
i=0;
temp = arr[i];
arr[i] = arr[noOfDigit-1];
arr[noOfDigit-1] = temp;
printf("\nFirst and Last Digit Interchanged Successfully!");
printf("\n\nNew Number = ");
for(i=0; i<noOfDigit; i++)
printf("%d", arr[i]);
}
getch();
return 0;
}```

It will produce the same output as of previous program.

## Interchange any Two Digit of a Number

This program is created to interchange the two digit of a number present at any position. The number and positions entered by user.

```#include<stdio.h>
#include<conio.h>
int main()
{
int num, posFirst, posSecond;
int rem, temp, rev=0, noOfDigit=0, arr, i;
printf("Enter the Number: ");
scanf("%d", &num);
temp = num;
while(temp>0)
{
temp = temp/10;
noOfDigit++;
}
if(noOfDigit==1)
{
printf("\nIt's a single-digit number.");
printf("\nTo interchange the digit, enter a two or more digit number");
getch();
return 0;
}
else
{
printf("\nInterchange the Digit at Position: ");
scanf("%d", &posFirst);
printf("With Digit at Position: ");
scanf("%d", &posSecond);
}
if(posFirst>noOfDigit || posSecond>noOfDigit)
printf("\nInvalid Position!");
else
{
temp = num;
while(temp>0)
{
rem = temp%10;
rev = (rev*10)+rem;
temp = temp/10;
}
for(i=0; i<noOfDigit; i++)
{
rem = rev%10;
arr[i] = rem;
rev = rev/10;
}
i=0;
temp = arr[posFirst-1];
arr[posFirst-1] = arr[posSecond-1];
arr[posSecond-1] = temp;
printf("\nDigits Interchanged Successfully!");
printf("\n\nNew Number = ");
for(i=0; i<noOfDigit; i++)
printf("%d", arr[i]);
}
getch();
return 0;
}```

Here is its sample run:

Now supply any number say 12345. Because it is a number that has more than one digit. Therefore program further asks to enter the position of digit to be exchange or interchange. For example, if user wants to interchange the the number at second position with number at fourth position. Then program just interchange the digit 2 with the digit 4 and the new number become 14325 as shown in the following output:

Here is another sample run, with an input number as 9871 and digit at position first (that is 9) gets interchanged with digit at position third (that is 7):

C Online Test

« Previous Program Next Program »