Program to Swap First and Last Digit of a Number using While loop

Program to Swap First and Last Digit of a Number using While loop

  • Write a program to Swap First and Last Digit of a Number using While loop in C
  • Write a program to Swap First and Last Digit of a Number using While loop in C++
  • Write a program to Swap First and Last Digit of a Number using While loop in Python
  • Write a program to Swap First and Last Digit of a Number using While loop in PHP
  • Write a program to Swap First and Last Digit of a Number using While loop in Java
  • Write a program to Swap First and Last Digit of a Number using While loop in Java Script
  • Write a program to Swap First and Last Digit of a Number using While loop in C#

Explanation:

To swap the first and last digits of a number using a While loop, follow this approach:

Steps:

  1. Extract the last digit: Use the modulo operation (n % 10).
  2. Extract the first digit: Repeatedly divide the number by 10 until only one digit remains.
  3. Remove the first and last digits: Construct a new number by removing the first and last digits and place the swapped digits in their respective positions.
  4. Rebuild the number: Create a new number by placing the last digit at the front and the first digit at the end.

Logic:

  1. Take input for the number (n).
  2. If the number has only one digit, there’s no need to swap.
  3. Get the last digit using n % 10.
  4. Get the first digit by dividing the number by 10 repeatedly until the result is less than 10.
  5. Remove the first and last digits from the number.
  6. Rebuild the number with the first and last digits swapped.

Program to Swap First and Last Digit of a Number using While loop

#include <stdio.h>

int main() {
    int num, first_digit, last_digit, digits = 0, temp;

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

    // Handle negative numbers
    if (num < 0) {
        num = -num;
    }

    temp = num;

    // Find number of digits
    while (temp != 0) {
        digits++;
        temp /= 10;
    }

    // Find last digit
    last_digit = num % 10;

    // Find first digit
    first_digit = num;
    while (first_digit >= 10) {
        first_digit /= 10;
    }

    // Remove first and last digits from the number
    num = num - first_digit * pow(10, digits - 1);
    num = num + last_digit * pow(10, digits - 1);
    num = num - last_digit + first_digit;

    printf("Number after swapping first and last digit: %d\n", num);

    return 0;
}

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int num, first_digit, last_digit, digits = 0, temp;

    cout << "Enter a number: ";
    cin >> num;

    // Handle negative numbers
    if (num < 0) {
        num = -num;
    }

    temp = num;

    // Find number of digits
    while (temp != 0) {
        digits++;
        temp /= 10;
    }

    // Find last digit
    last_digit = num % 10;

    // Find first digit
    first_digit = num;
    while (first_digit >= 10) {
        first_digit /= 10;
    }

    // Remove first and last digits from the number
    num = num - first_digit * pow(10, digits - 1);
    num = num + last_digit * pow(10, digits - 1);
    num = num - last_digit + first_digit;

    cout << "Number after swapping first and last digit: " << num << endl;

    return 0;
}

num = int(input("Enter a number: "))

# Handle negative numbers
if num < 0:
    num = -num

# Convert to string to easily manipulate digits
num_str = str(num)

# Get first and last digits
first_digit = int(num_str[0])
last_digit = int(num_str[-1])

# Swap first and last digits
if len(num_str) > 1:
    num_str = str(last_digit) + num_str[1:-1] + str(first_digit)
else:
    num_str = str(first_digit)

print(f"Number after swapping first and last digit: {num_str}")

<?php
$num = (int)readline("Enter a number: ");

// Handle negative numbers
if ($num < 0) {
    $num = -$num;
}

$num_str = strval($num);

// Get first and last digits
$first_digit = intval($num_str[0]);
$last_digit = intval($num_str[strlen($num_str) - 1]);

// Swap first and last digits
if (strlen($num_str) > 1) {
    $num_str = $last_digit . substr($num_str, 1, strlen($num_str) - 2) . $first_digit;
} else {
    $num_str = $first_digit;
}

echo "Number after swapping first and last digit: $num_str\n";
?>

import java.util.Scanner;

public class SwapFirstLast {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        // Handle negative numbers
        if (num < 0) {
            num = -num;
        }

        String numStr = Integer.toString(num);

        // Get first and last digits
        int firstDigit = Character.getNumericValue(numStr.charAt(0));
        int lastDigit = Character.getNumericValue(numStr.charAt(numStr.length() - 1));

        // Swap first and last digits
        if (numStr.length() > 1) {
            numStr = lastDigit + numStr.substring(1, numStr.length() - 1) + firstDigit;
        } else {
            numStr = Integer.toString(firstDigit);
        }

        System.out.println("Number after swapping first and last digit: " + numStr);
        sc.close();
    }
}

let num = parseInt(prompt("Enter a number: "));

// Handle negative numbers
if (num < 0) {
    num = -num;
}

let numStr = num.toString();

// Get first and last digits
let firstDigit = parseInt(numStr[0]);
let lastDigit = parseInt(numStr[numStr.length - 1]);

// Swap first and last digits
if (numStr.length > 1) {
    numStr = lastDigit + numStr.slice(1, numStr.length - 1) + firstDigit;
} else {
    numStr = firstDigit.toString();
}

console.log(`Number after swapping first and last digit: ${numStr}`);

using System;

class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int num = int.Parse(Console.ReadLine());

        // Handle negative numbers
        if (num < 0) {
            num = -num;
        }

        string numStr = num.ToString();

        // Get first and last digits
        int firstDigit = int.Parse(numStr[0].ToString());
        int lastDigit = int.Parse(numStr[numStr.Length - 1].ToString());

        // Swap first and last digits
        if (numStr.Length > 1) {
            numStr = lastDigit + numStr.Substring(1, numStr.Length - 2) + firstDigit;
        } else {
            numStr = firstDigit.ToString();
        }

        Console.WriteLine($"Number after swapping first and last digit: {numStr}");
    }
}

List of All Programs