- Java Basic Programs
- Java Programming Examples
- Java Print Hello World
- Java Get Input from User
- Java Print Integer
- Java Add two Numbers
- Java Check Even or Odd
- Java Check Prime or Not
- Java Check Alphabet or Not
- Java Check Vowel or Not
- Check Reverse equal Original
- Java Fahrenheit to Celsius
- Java Celsius to Fahrenheit
- Java Perfect Number Program
- Java Find Quotient Remainder
- Java Days to Seconds
- Java Count Digits in Number
- Java Binary Number Addition
- Java Discount Program
- Java Compute Courier Charge
- Java Find Telephone Bill
- Java Print ASCII Values
- Java Check Palindrome or Not
- Java Check Armstrong or Not
- Generate Armstrong Numbers
- Add two Numbers using Pointers
- Java Mathematical Programs
- Add Subtract Multiply & Divide
- Java Make Calculator
- Java Add Digits of Number
- Java Check Leap Year or Not
- Java Check Divisibility
- Java Find Simple Interest
- Java Find Compound Interest
- Java Print Fibonacci Series
- Java Find nCr nPr
- Calculate Average & Percentage
- Java Calculate Arithmetic Mean
- Java Calculate Student Grade
- Java Print Table of Number
- Java Print Prime Numbers
- Java Add n Numbers
- Java Interchange two Numbers
- Java Reverse Numbers
- Java Swap two Numbers
- Count Positive Negative & Zero
- Find Largest of two Numbers
- Find Largest of three Numbers
- Java Find Factorial of Number
- Java Find HCF & LCM
- Area & Perimeter of Square
- Area & Perimeter of Rectangle
- Area & Circumference of Circle
- Java Conversion Programs
- Java Decimal to Binary
- Java Decimal to Octal
- Java Decimal to Hexadecimal
- Java Binary to Decimal
- Java Binary to Octal
- Java Binary to Hexadecimal
- Java Octal to Decimal
- Java Octal to Binary
- Java Octal to Hexadecimal
- Java Hexadecimal to Decimal
- Java Hexadecimal to Binary
- Java Hexadecimal to Octal
- Java Pattern Programs
- Java Pattern of Stars
- Java Pattern of Alphabets
- Java Pattern of Numbers
- Java Pyramid of Stars
- Java Pyramid of Alphabets
- Java Pyramid of Numbers
- Java Print Diamond Pattern
- Java Print Floyd Triangle
- Java Print Pascal Triangle
- Java Array Programs
- One Dimensional Array Program
- Java Linear Search
- Java Binary Search
- Find Largest Element in Array
- Find Smallest Element in Array
- Java Reverse Array
- Insert Element in Array
- Delete Element from Array
- Java Merge two Array
- Java Bubble Sort
- Java Selection Sort
- Java Insertion Sort
- Java Find Common Elements
- Java Count Even/Odd Number
- Two Dimensional Array Program
- Java Add two Matrices
- Java Subtract two Matrices
- Java Transpose Matrix
- Multiply two Matrices
- Three Dimension Array Program
- Java String Programs
- Java Print String
- Find Length of String
- Java Compare two String
- Java Copy String
- Java Concatenate String
- Java Reverse String
- Delete Vowels from String
- Delete Words from Sentence
- Find Occurrence of a Character
- Java Find Occurrence of a Word
- Occurrence of Each Character
- Java Occurrence of Each Word
- Java Count Repeated Characters
- Java Count Repeated Words
- Java Capitalize Each Word
- Java Count Vowels/Consonants
- Java Extract Numbers
- Java Count Word in String
- Remove Spaces from String
- Java Sort a String
- Java Uppercase to Lowercase
- Java Lowercase to Uppercase
- Java Swap two Strings
- Java Check Anagram or Not
- Java Check Balance Parentheses
- Java Check Password Strength
- Java File Programs
- Java Read File
- Java Write to File
- Read & Display File Content
- Java Copy File
- Java Append Text to File
- Java Merge two File
- List files in Directory
- Java Delete File
- Java Miscellaneous Programs
- Generate Random Numbers
- Java Print Time & Date
- Java Get IP Address
- Java Shutdown Computer
- Java Programming Tutorial
- Java Tutorial
Java Program to Interchange the Digits of a Number
This article is created to cover multiple programs in Java that interchange or swap the digit of a number, entered by user at run-time of the program. Here are the list of programs covered in this article:
- Interchange the first and last digit of a number using while loop
- Interchange the first and last digit of a number using for loop
- Interchange any two specified digits (by position) of a number
Swap First and Last Digit of a Number in Java
The question is, write a Java program to swap or interchange the first and last digit of a number. The number must be received by user at run-time. The program given below is its answer. This program is created using while loop:
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int num, temp, digits=0, rem, remTemp, rev=0, r, revTemp, digitsTemp; Scanner scan = new Scanner(System.in); System.out.print("Enter the Number: "); num = scan.nextInt(); temp = num; while(temp>0) { digits++; temp = temp/10; } if(digits<2) { System.out.println("\nInterchange not possible."); } else if(digits==2) { temp = num; while(temp>0) { rem = temp%10; rev = (rev*10) + rem; temp = temp/10; } System.out.println("\nFirst and Last Digit Interchanged Successfully!"); System.out.println("\nThe New Number = " +rev); } else { temp = num; while(temp>0) { rem = temp%10; rev = (rev*10) + rem; temp = temp/10; } r = rev; rev = 0; temp = num; digitsTemp = digits; while(temp>0) { remTemp = r%10; if(digitsTemp==digits || digitsTemp==1) { rem = temp%10; rev = (rev*10) + rem; } else { rev = (rev*10) + remTemp; } r = r/10; temp = temp/10; digitsTemp--; } System.out.println("\nFirst and Last Digit Interchanged Successfully!"); System.out.println("\nThe new number = " +rev); } } }
The snapshot given below shows the sample run of above program with user input 28054 as number to swap or interchange its first and last digit:
Interchange First and Last Digit using for Loop
The previous program can also be created using for loop instead of while. I've done some other modifications too, in the program to shorten the code.
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int num, temp, digits=0, rev=0, r, digitsTemp; Scanner scan = new Scanner(System.in); System.out.print("Enter the Number: "); num = scan.nextInt(); for(temp=num; temp>0; temp /= 10) digits++; if(digits<2) System.out.println("\nInterchange not possible."); else if(digits==2) { for(temp=num; temp>0; temp /= 10) rev = (rev*10) + (temp%10); System.out.println("\nFirst and Last Digit Interchanged Successfully!"); System.out.println("\nThe New Number = " +rev); } else { for(temp=num; temp>0; temp /= 10) rev = (rev*10) + (temp%10); r = rev; rev = 0; digitsTemp = digits; for(temp=num; temp>0; temp /= 10) { if(digitsTemp==digits || digitsTemp==1) rev = (rev*10) + (temp%10); else rev = (rev*10) + (r%10); r = r/10; digitsTemp--; } System.out.println("\nFirst and Last Digit Interchanged Successfully!"); System.out.println("\nThe new number = " +rev); } } }
And if a number entered by user is always having more than 2 digits. Or if you want a program that works only for more than 2 digit number. Then remove the if-else block of checking the number of digit, to interchange the first and last digit directly. Here is the complete version of the program, works only for 3 or more than 3-digit number:
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int num, temp, digits=0, rev=0, r, digitsTemp; Scanner scan = new Scanner(System.in); System.out.print("Enter the Number: "); num = scan.nextInt(); for(temp=num; temp>0; temp /= 10) digits++; for(temp=num; temp>0; temp /= 10) rev = (rev*10) + (temp%10); r = rev; rev = 0; digitsTemp = digits; for(temp=num; temp>0; temp /= 10) { if(digitsTemp==digits || digitsTemp==1) rev = (rev*10) + (temp%10); else rev = (rev*10) + (r%10); r = r/10; digitsTemp--; } System.out.println("\nFirst and Last Digit Interchanged Successfully!"); System.out.println("\nThe new number = " +rev); } }
Interchange any Two Digits of a Number in Java
This is the last program, created to interchange any two specified digits of a given number. Since a number may contain two digits with same value. Therefore, this program receives the position of digits, instead of digit's values, to interchange the any two given digits of a number, by position:
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int firstPos, secondPos, temp, rev=0, digits=0, i; Scanner s = new Scanner(System.in); System.out.print("Enter the Number: "); int num = s.nextInt(); for(temp=num; temp>0; temp /= 10) digits++; int[] arr = new int[digits]; if(digits==1) System.out.println("\nInterchange not possible!"); else if(digits==2) System.out.println("\nInterchange with digit-wise is not possible!"); else { System.out.print("\nInterchange the Digit at Position: "); firstPos = s.nextInt(); System.out.print("With Digit at Position: "); secondPos = s.nextInt(); if(firstPos>digits || secondPos>digits) System.out.println("\nInvalid Position!"); else { for(temp=num; temp>0; temp /= 10) rev = (rev*10) + (temp%10); for(i=0; i<digits; i++, rev /= 10) arr[i] = rev%10; temp = arr[firstPos-1]; arr[firstPos-1] = arr[secondPos-1]; arr[secondPos-1] = temp; System.out.println("\nSpecified Digits Interchanged!"); System.out.print("\nThe new number = "); for(i=0; i<digits; i++) System.out.print(arr[i]); } } } }
The sample run of above program with user input 123456 as number, 2 and 5 as digit's position to interchange the digit at position 2 with the digit at position 5, is shown in the snapshot given below:
Same Program in Other Languages
« Previous Program Next Program »