WAP to Convert 01230 To 03210

What We Are Learn:

WAP to Convert 01230 To 03210: This Java program is designed to reverse a given integer represented as a string, add leading zeros if necessary, and print the reversed number. The program demonstrates how to format an integer with leading zeros using String.format, convert an integer to a string, iterate through a string in reverse order, and concatenate characters to form the reversed number.

Convert 01230 To 03210

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms15 
{
	/*
	 * Input: 01230 
	 * Output: 03210
	 */
	public static void main(String[] args) 
	{
		int n = 1230 ;
		String s = String.format ( "%05d",n); // to get 1230 to 01230
		System.out.println ( s ) ;
		int len = s.length ( ) ;
		String rev = "" ;
		for ( int i = len - 1 ; i >= 0 ; i-- )
		{
			rev = rev + s.charAt ( i ) ;
		}
		System.out.println ( rev ) ; 
	}
}

Output:

01230
03210

Description of the program:

  • The program defines a class named InterviewPrograms15.
  • Inside the class, there is a main method, which serves as the entry point for the program.
  • The input number is initialized as an integer n with the value 1230.
  • The program uses String.format to convert the integer n to a string s with leading zeros. The format specifier %05d ensures that the resulting string has a length of 5, padding the number with zeros if necessary. The resulting string will be “01230”.
  • The program prints the value of the string s, which will be “01230”.
  • The program calculates the length of the string s using the length method and stores it in the integer variable len.
  • An empty string rev is initialized to store the reversed number.
  • The program uses a for loop to iterate through each character of the string s in reverse order. The loop starts from the last character (index len-1) and goes backward until the first character (index 0).
  • Inside the loop, each character is retrieved using s.charAt(i) and appended to the string rev, effectively reversing the order of the digits.
  • Finally, the program prints the reversed number stored in the string rev, which will be “03210”.

In summary, this program takes an integer, converts it to a string with leading zeros, and then reverses the order of the digits to generate the output. It showcases the use of String.format to format integers as strings with leading zeros, string manipulation, iteration through strings, and character retrieval to achieve the desired result. The output will be the reversed number “03210” for the input “01230”.

Alternative 1:

This Java program takes an input string “01230” and reverses its characters to produce the output “03210”. The Java program function demonstrates how to convert a string into a character array, iterate through the character array in reverse order, and then concatenate the characters to generate the reversed string.

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms15_1 
{
	/*
	 * Input: 01230 
	 * Output: 03210
	 */
	public static void main(String[] args) 
	{
		String s= "01230";
		System.out.println("Input: "+s);
		String newStr = " " ;
		char c [ ] =s.toCharArray ( ) ;         
		for ( int i = c.length - 1 ; i >= 0 ; i-- )
		{
			newStr= newStr + c [ i ] ;
		}
		System.out.println ("Output"+newStr);
	}
}

Output:

01230
03210

Explanation of the program section by section:

  • The program defines a class named InterviewPrograms15_1.
  • Inside the class, there is a main method, which serves as the entry point for the program.
  • The input string “01230” is initialized as s.
  • The program prints the input string with the message “Input: ” using System.out.println.
  • An empty string newStr is initialized to store the reversed string.
  • The program converts the input string s into a character array c using the toCharArray method of the String class. This step allows easy iteration through the characters.
  • The program uses a for loop to iterate through each character of the character array c in reverse order. The loop starts from the last character (index c.length – 1) and goes backward until the first character (index 0).
  • Inside the loop, each character is retrieved using c[i], and it is concatenated to the newStr string. This step effectively reverses the order of the characters.
  • Finally, the program prints the reversed string stored in the newStr string with the message “Output” using System.out.println. The output will be “Output: 03210”.

In summary, this program takes an input string, converts it into a character array, and then reverses the order of the characters to produce the output string. It showcases string manipulation, character array conversion, iteration through the character array, and string concatenation to achieve the desired result. The output will be “Output: 03210” for the input “01230”.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment