Swap First And Last Digit Of A Number In C

Approach and Solution:

We will implement the subsequent steps to interchange the first and last digits of a number:

Step 1: It is to read the user-inputted number.

Step 2: Identify the quantity of digits present in the given number.

Remove the digits at the beginning and end of the number in step 3.

Identify the positional value of the initial digit in Step 4.

Identify the placement value of the last numeral in the sequence.

Step 6: Utilizing temporary variables, interchange the initial and final digits.

Step 7: Create the final swapped number.

Step 8: Show the swapped number as the output.

Example:

Let's now explore how the phases would be executed in code utilizing the C programming language:

Example

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

int main() {
    int number, firstDigit, lastDigit, temp, numOfDigits;

printf("Enter a positive integer: ");
scanf("%d", &number);

    // Step 2: Calculate the number of digits in the given number
numOfDigits = snprintf(NULL, 0, "%d", number);

    // Step 3: Extract the first and last digits
lastDigit = number % 10;
firstDigit = number / pow(10, numOfDigits - 1);

    // Step 6: Swap the first and last digits
    temp = firstDigit;
firstDigit = lastDigit;
lastDigit = temp;

    // Step 7: Construct the final swapped number
    int swappedNumber = lastDigit * pow(10, numOfDigits - 1) + number % ((int)pow(10, numOfDigits - 1));

printf("Swapped number: %d\n", swappedNumber);

    return 0;
}

Output:

Output

Enter a positive integer: 9348 
Swapped number: 8349

Explanation:

The snprintf function determines the length of the input number in digits. It returns the total number of characters that would have been written if the string was sufficiently long. The printf and scanf functions utilize the placeholders %d and %f for displaying and reading integers and floating-point numbers, respectively.

Example:

An alternative method for swapping the first and last digits of a number in C is the following:

Example

#include <stdio.h>

int main() {
    int number, originalNumber, firstDigit, lastDigit, digitCount = 0;

    printf("Enter a positive integer: ");
    scanf("%d", &number);

    originalNumber = number;

    // Count the number of digits
    while (number > 0) {
        digitCount++;
        number /= 10;
    }

    // Extract the first and last digits
    lastDigit = originalNumber % 10;
    number = originalNumber / 10;

    // Construct the swapped number
    firstDigit = originalNumber / (10 * (digitCount - 1));
    int power = 1;
    for (int i = 0; i < digitCount - 2; i++) {
        power *= 10;
    }
    int swappedNumber = lastDigit * power * 10 + number % power * 10 + firstDigit;

    printf("Swapped number: %d\n", swappedNumber);

    return 0;
}

Output:

Output

Enter a positive integer: 75624
Swapped number: 45627

Explanation:

The software requests that the user input a whole number greater than zero (for instance, 75624 ). It calculates the exchanged number, 45627 , by executing the necessary steps. Subsequently, the program displays the altered number.

When the initial number is repeatedly divided by 10 until reaching 0, it is possible to determine the count of digits employing this unconventional technique. Subsequently, the initial and final digits are obtained through modulo and division procedures. Ultimately, the reversed number is constructed through a reorganization of the digits.

Conclusion:

Developers often encounter scenarios where they need to interchange the first and last digits of a number. This skill can prove beneficial in various coding situations. By referring to the detailed guidelines provided in this article and integrating the provided C programming code, you can efficiently accomplish this task. It is crucial to focus on the code's syntax and logic to achieve accurate outcomes.

The fundamental concepts of coding involve problem-solving and breaking down complex tasks into smaller, more manageable steps. Enhancing your programming skills and tackling challenging problems are possible through patience and persistence.

Input Required

This code uses input(). Please provide values below: