In this article, we will write a simple C program to swap the first and last digit of a number. For example, if the input number is 3456, then the program should print 6453 (swapping first and last digit) as output.
C Program to swap the first and last digit of a number
In this program, we first counts the number of digits in the input number using while loop, then we determine the first and last digit using division and modulo operators. Later in this program, we swap these digits using arithmetic operations.
The explanation of the program is at the end of this code.
#include <stdio.h>
int main() {
int num, temp, firstDigit, lastDigit, digitsCount = 0;
// Input number
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
// Count number of digits
while (temp != 0) {
temp /= 10;
digitsCount++;
}
// Finds first and last digits of the input number
firstDigit = num / (int)pow(10, digitsCount - 1);
lastDigit = num % 10;
// Swap first and last digits of the number
temp = num - (firstDigit * (int)pow(10, digitsCount - 1));
temp += (lastDigit * (int)pow(10, digitsCount - 1));
temp -= lastDigit;
temp += firstDigit;
// Output the number with swapped digits
printf("Number with first and last digits swapped: %d\n", temp);
return 0;
}
Output:
Enter a number: 12345
Number with first and last digits swapped: 52341
In this example, user entered the number 12345
. The first digit is 1
, and the last digit is 5
. The program swapped these first and last digits and printed the number 52341
.
Explanation of the program:
- Variable Declaration: In the
main
function, we declared the following variables:num
: This stores the number entered by the user.temp
: a temporary variable used for calculations.firstDigit
andlastDigit
: to store the first and last digits of the input number.digitsCount
: to count the number of digits in the input number.
- Input from user: We used
printf()
function to prompt the user for entering a number, the entered number is stored in the variable num usingscanf()
function. - Counting Digits: The program counts the number of digits in the input number using a
while
loop. It is done by dividing the input number by 10, this effectively removes a digit from the number, at the same time we are increasing the value ofdigitsCount
by 1. This process continues until the number becomes zero, at this moment thedigitsCount
contains the number of digits in the number. - Finding the First and Last Digits: The first and last digits are calculated based on the following logic:
firstDigit
: This is calculated by dividing the input number by 10 raised to the power of(digitsCount - 1)
. For example, if I need to find the first digits in number 20, this would be 20/(10^(2-1)) = 20/10 = 2, Similarly for 123 it would be 123/(10^(3-1)) = 123/100 = 1lastDigit
: This is equal to the remainder after dividing the number by 10, which can be easily done by modulo operator.
- Swapping first and last Digits: The program swaps the first and last digits of the input number:
- Output: Finally, the program prints the output number using
printf()
after swapping the first and last digits.
Leave a Reply