Java Reverse Order

  1. Int Value Reverse Order
  2. Float Value Reverse Order
  3. String Value Reverse Order

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Int Value Reverse Order


Reverse_Int.java

import java.util.Scanner;
 
public class Reverse_Int {
 
	int number, sum = 0;
	
	void getVal() {
		
		System.out.println("Int Value Reverse Order");
		
		System.out.println("\nEnter the number : ");
		Scanner scan = new Scanner(System.in);
		
		number = Integer.parseInt(scan.nextLine());
	}
	
	void reverseOrder() {
		
		int temp = number;
		int mod;
		
		while(temp != 0) {
			
			mod = temp % 10;
			sum = sum * 10 + mod;
			temp = temp / 10;
		}
		
		System.out.println("\nThe Reversed Order is : \n" + sum);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Reverse_Int obj = new Reverse_Int();
		
		obj.getVal();
		obj.reverseOrder();
	}
}

Sample Output

Int Value Reverse Order

Enter the number : 
123456789

The Reversed Order is : 
987654321




B. Float Value Reverse Order


Reverse_Float.java

import java.util.Scanner;
 
public class Reverse_Float {
 
	float number;
	
	void getVal() {
		
		System.out.println("Float Value Reverse Order");
		
		System.out.println("\nEnter the number : ");
		Scanner scan = new Scanner(System.in);
		
		number = Float.parseFloat(scan.nextLine());
	}
	
	void divide() {
		
		float temp = number;
		String st = String.valueOf(temp);
				
		String str[] = st.split("\\.");
        
        	int before = Integer.parseInt(str[0]);
		int after = Integer.parseInt(str[1]);
		
		String str1 = String.valueOf(revere(before));
		String str2 = String.valueOf(revere(after));
		
		String str3 = str1.concat(".").concat(str2);
		
		float rev = Float.parseFloat(str3);
		System.out.println("\nReversed Order is : \n" + rev);
	}
 
	private int revere(int value) {
		
		int mod;
		int sum = 0;
        
        	while(value != 0) {
			
			mod = value % 10;
			sum = sum * 10 + mod;
			
			value = value / 10;
		}
                
        	return sum;
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Reverse_Float obj = new Reverse_Float();
		
		obj.getVal();
		obj.divide();
	}
}

Sample Output

Float Value Reverse Order

Enter the number : 
123.456

Reversed Order is : 
321.654




C. String Value Reverse Order


Reverse_String.java

import java.util.Scanner;
 
public class Reverse_String {
	
	void reverse(String name) {
		
		char[] c1 = new char[name.length()];		
		String rev = "";
				
		for(int i=0; i<name.length(); i++) {
			
			c1[i] = name.charAt((name.length()-1) - i);
			rev = rev + c1[i];
		}
		
		System.out.println("\nReverse order is : \n" + rev);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Reverse_String obj = new Reverse_String();
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("String Value Reverse Order");
		
		System.out.println("\nEnetr the String :");
		String name = scan.nextLine();
		
		obj.reverse(name);
	}
}

Sample Output

String Value Reverse Order

Enetr the String :
samplecodez

Reverse order is : 
zedocelpmas






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com