BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to swap first and last digit of a number

Last Updated: May 16, 2024 by Chaitanya Singh | Filed Under: C Programs

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:

  1. 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 and lastDigit: to store the first and last digits of the input number.
    • digitsCount: to count the number of digits in the input number.
  2. Input from user: We used printf() function to prompt the user for entering a number, the entered number is stored in the variable num using scanf() function.
  3. 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 of digitsCount by 1. This process continues until the number becomes zero, at this moment the digitsCount contains the number of digits in the number.
  4. 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 = 1
    • lastDigit: This is equal to the remainder after dividing the number by 10, which can be easily done by modulo operator.
  5. Swapping first and last Digits: The program swaps the first and last digits of the input number:
  6. Output: Finally, the program prints the output number using printf() after swapping the first and last digits.

Top Related Articles:

  1. C Program to check abundant number
  2. C Program to find factorial of a number using Recursion
  3. C Program to Display Characters from A to Z Using Loop
  4. C Program to find sum of array elements
  5. C Program to find the Size of int, float, double and char

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap