Home Java Overview Maven Tutorials

Friday 22 December 2017

reverse the string and number in java without using functions

For Video :- Click Here

Hello friends,

In this article we will learn Reverse a String with and without using Java Functions :-

import java.util.Scanner;
public class Stringplay
public static void main(String args[])
String rev = "";
Scanner sc = new Scanner(;
System.out.println("Enter your first string");
String ys = sc.nextLine();
int length = ys.length();
for(int i =length-1; i>=0; i--)
rev = rev + ys.charAt(i);
System.out.println("Your String is: " +ys);
System.out.println("reverse String is: " +rev);
StringBuffer sb = new StringBuffer(ys);
rev = sb.reverse().toString();
System.out.println("simple reverse String is: " +rev);

And here is your output:

Enter your first string
chandan singh
Your String is: chandan singh
reverse String is: hgnis nadnahc
simple reverse String is: hgnis nadnahc

Reverse a number without using Java Functions :-

public class Jtestnew
public void g()
int x = 123%10;
int y = 123/10;
Scanner scn = new Scanner(;
System.out.println("Enter the number");
int num = scn.nextInt();
int rnum = 0;
rnum = rnum*10 + num%10;
num = num/10;
System.out.println("Reversed number is: " +rnum);

No comments:

Post a Comment