Swapping numbers in java – Java Program to Swap First and Last Digits of a Number

Swapping numbers in java: In the previous article, we have discussed about Java Program to Calculate Profit and Loss

In this article we are going to see how to swap first and last digits of a number in java using Java programming language. We include in this article how to swap digits in java? How to do swap function in java? Addition with swapping numbers in java and swapping program in java.

Java Program to Swap First and Last Digits of a Number

  • Program To swap the first and last digits of a number we must first find a way obtain the first and the last digits of the number.
  • Number modulo 10 will give last digit.
  • Getting the first digit is tricky.
  • Here we can use some mathematical tricks to find the digits.
  • The floor value of log10n always gives one less than number of digits present in that number.
  • Then dividing the number with 10 raised to the result obtained from log10n will give the first digit.

Let’s see different ways to swap first and last digits of a number.

Method-1: Java Program to Swap First and Last Digits of a Number By Using Static Input Value

Approach:

  • Initialize the number.
  • Store the last digit using number%10 in a variable.
  • To obtain the first digit, use Math.log10() method and store its floor value in a variable, this will give the (number of digits – 1).
  • Then obtain the first digit by dividing the number with 10number of digits. Use Math.pow() method to get the power.
  • Initialise a variable swappedNum with the last digit.
  • Multiply swappedNum with 10number of digits.
  • Add n % 10number of digits to swappedNum.
    • n % 10number will give all the digits except the first digit.
  • Subtract the last digit from the swappedNum and add the first digit.
  • Print the result.

Program:

public class Main
{
    public static void main(String[] args)
    {
        int n = 12345;
        // find the first digit
        int lastDigit = n % 10;
        // Find total number of digits - 1
        int digitsCount = (int) Math.log10(n);
        // Find first digit
        int firstDigit = (int) (n / Math.pow(10, digitsCount));
        // swap the first and last digits
        int swappedNum = lastDigit;
        swappedNum *= (int) Math.pow(10, digitsCount);
        swappedNum += n % ((int) Math.pow(10, digitsCount));
        swappedNum -= lastDigit;
        swappedNum += firstDigit;
        System.out.println("The Number after Swapping First Digit and Last Digit = " + swappedNum);
    }

}
Output:

The Number after Swapping First Digit and Last Digit = 52341

Method-2: Java Program to Swap First and Last Digits of a Number By Using User Defined Method

Approach:

  • Create Scanner class object.
  • Take user input for the number whose digits are to be swapped.
  • Call the user-defined method to swap the digits.
  • Create the user-defined the place the same logic as in method 1 inside that method

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // prompt user to enter a number
        System.out.print("Enter the number: ");
        int n = sc.nextInt();
        int swappedNum = swapDigits(n);
        System.out.println("The Number after Swapping First Digit and Last Digit = " + swappedNum);
    }

    private static int swapDigits(int n)
    {
        int lastDigit = n % 10;
        // Find total number of digits - 1
        int digitsCount = (int) Math.log10(n);
        // Find first digit
        int firstDigit = (int) (n / Math.pow(10, digitsCount));
        // swap the first and last digits
        int swappedNum = lastDigit;
        swappedNum *= (int) Math.pow(10, digitsCount);
        swappedNum += n % ((int) Math.pow(10, digitsCount));
        swappedNum -= lastDigit;
        swappedNum += firstDigit;
        return swappedNum;
    }

}
Output:

Enter the number: 54321
The Number after Swapping First Digit and Last Digit = 14325

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Here are some of the questions to practice on Java Program to Swap First and Last Digits of a Number:

  1. Write C Program To Swap First And Last Digit Of A Number?
  2. Write A C Program To Swap First And Last Digits Of Any Number.
  3. Write A Python Program To Swap First And Last Digits Of A Number?

Related Java Programs: