C program to swap first and last digits of a number

Category: C Program

Learn how to write a C program to swap the first and last digits of a number. This article provides a detailed explanation and sample code for this basic yet essential task in C programming.

Swapping the first and last digits of a number is a common programming problem that helps in understanding basic arithmetic operations and control structures. This article will guide you through writing a C program to swap the first and last digits of a given number, providing a detailed explanation and sample code.

Steps to Swap the First and Last Digits

To solve this problem, follow these steps:

  1. Input the Number: Read the number from the user.
  2. Extract the Last Digit: Find the last digit of the number using the modulus operator.
  3. Extract the First Digit: Find the first digit by continuously dividing the number by 10 until the number is less than 10.
  4. Calculate the Number of Digits: Determine the number of digits in the number.
  5. Swap the Digits: Construct the new number with the first and last digits swapped.
  6. Print the Result: Output the new number to the console.

Write a C program to swap first and last digits of a number

Here's a C program to swap the first and last digits of a number:

#include <stdio.h>
#include <math.h>

int main() {
    int number, firstDigit, lastDigit, numDigits, divisor, middlePart, swappedNumber;

    // Input the number
    printf("Enter an integer: ");
    scanf("%d", &number);

    // Extract the last digit
    lastDigit = number % 10;

    // Extract the first digit
    firstDigit = number;
    while (firstDigit >= 10) {
        firstDigit /= 10;
    }

    // Calculate the number of digits
    numDigits = (int)log10(number) + 1;

    // Calculate the divisor for the first digit
    divisor = (int)pow(10, numDigits - 1);

    // Extract the middle part of the number
    middlePart = number % divisor;
    middlePart = middlePart / 10;

    // Swap the first and last digits and reconstruct the number
    swappedNumber = (lastDigit * divisor) + (middlePart * 10) + firstDigit;

    // Print the result
    printf("Number after swapping first and last digit: %d", swappedNumber);

    return 0;
}

Output

Enter an integer: 1234
Number after swapping first and last digit: 4231

Explanation

  1. Input the Number: The program prompts the user to enter an integer. The scanf function reads the input number and stores it in the variable number.
  2. Extract the Last Digit:
    • The last digit is found using the modulus operator (%), which gives the remainder when number is divided by 10.
    • For example, if the number is 1234, lastDigit = 1234 % 10 will result in lastDigit = 4.
  3. Extract the First Digit:
    • The first digit is found by repeatedly dividing the number by 10 until it is less than 10.
    • For example, if the number is 1234, the sequence of operations will be 1234 / 10 = 123, 123 / 10 = 12, 12 / 10 = 1, resulting in firstDigit = 1.
  4. Calculate the Number of Digits:
    • The number of digits is determined using the log10 function. For example, log10(1234) gives approximately 3.09, and adding 1 results in 4 digits.
  5. Calculate the Divisor for the First Digit:
    • The divisor is calculated as 10^(numDigits - 1). For a four-digit number, the divisor would be 1000.
  6. Extract the Middle Part of the Number:
    • The middle part is found by removing the first and last digits. This is done by first removing the first digit using the modulus operator and then removing the last digit by integer division.
  7. Swap the Digits:
    • Construct the new number by rearranging the digits. The new number is calculated by placing the last digit in the position of the first digit, followed by the middle part, and ending with the first digit.
  8. Print the Result: The program prints the new number with the first and last digits swapped to the console using the printf function.

Recommended Posts