Basic Java Examples

# Reverse Number using Java

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

• Anonymous says:

LOVELY SITE……

• Anonymous says:

• Anonymous says:

good………………..

• Anonymous says:

Thanks a lot programs are helpful

• Anonymous says:

nice it is very nice

• Anonymous says:

why it only number is reverse

• Anonymous says:

good…

• Anonymous says:

can u please explain the logic

• Anonymous says:

public class ReverseNumber2
{
public static void main(String args[])
{
int []n = {1,2,3,4};
String temp = “”;
for(int i = 3; i>=0;i–)
{
System.out.println(n[i]);
temp = temp + n[i];
}
System.out.println(temp);
}
}

//hope this can help our understanding to ðŸ™‚

• dderyty says:

what is the purpose of temp variable

• Jagan Kumar says:

used to store a value temporily,it may not be used for future purpose….

• dinesh degala says:

niceeeee

• MasterWalpha says:

Yeah but next time try fixing the indententation of your brackets. Makes it even easier to see visually.

• Anonymous says:

• Anonymous says:

very very usefull to beginners of java program.

• Anonymous says:

Really very good job ….

• Anonymous says:

Benifit a lot for the very beginners,,,,

• Anonymous says:

thanks 4 sharing

• Anonymous says:

these programs are really helpful… thanks…

• somtirtha mitra says:

12345 54321

1234 4321

123 321

12 21

1 1

12 21

123 321

1234 4321

12345 54321

solution???????

• Prashant Dev Verma says:

number = number / 10 (this line is for what?????)

• shubham rai says:

first the number is 1234.
Now in next iteration you want number to be 123 to extract 3 from 123.
this is achieved by number=number/10.
In short it is used to remove 1 value from last of the value.
eg) 1234/10=123
123/10=12
12/10=1;

• temirbek says:

how come 1234/10=123? shouldn’t it be 1234/10=123.4?

• temirbek says:

now i see, number is int )

• ckjframe2012 says:

What if the user inputs 980? the zero will not be displayed im sure

• disqus_AzSfHzv204 says:

hadd he yaar sab khud se banane chahiye.. ban this site

• disqus_AzSfHzv204 says:

itna aasan prog ni ban raha he to coding chod do sab log

• Huwaida Fallatah says:

hi can you help me with this question …

write a java program to find the square root of every number between 1 and 20 ???

• disqus_g2loYZRSVH says:

System.out.println(new StringBuilder(String.valueOf(1234)).reverse());

• GURU says:

public reverse

{

public static void main(String args[])

{

int a[]=new int[50];

int b[]=new int[50];

a[]={1,2,3,4,5};

int n=a.lenght();

for(int i=0;i<=n;i++)

{

b[n-1-i]=a[i];

}

for(int i=0;i<=n;i++)
{

System.out.println(b[i]);

}

}
}

• GURU says:

How is it friends .. Try it

• Fannaster says:

how woul you do this if user needs to input 10 numbers and reverse them
like this
User input : 1,2,3,4,5,6,7,8,9,10
result after reversing 10,9,8,7,6,5,4,3,2,1

• Rien Zeke says:

How to reverse array? Without using any util.

• Rashmi says:

Can any one help me write a java program in following way

1
12
123
1234
12345
1234
123
12
1

i need this solution ASAP. n e help will be really appreciated . Many Thanks in Advance.

• Rahit Kumar says:

public static void main(String args[]) {

int v = 10;
int n = 0;
int t = 0;
for (int x = 1; x <= v; x++) {
for (int y = 1; y <= x – n; y++) {
System.out.print(y + ” “);
}
if (v % 2 == 0) {
t = (v / 2);
} else {
t = (v / 2) + 1;
}
if (x >= t) {
n = n + 2;
}
System.out.println();
}
}

• Rashmi says:

class Pyramid
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
System.out.print(” “+j);
System.out.print(“\n”);
}
for(i=4;i>=1;i–)
{
for(j=1;j<=i;j++)
System.out.print(” “+j);
System.out.print(“\n”);
}
}
}

Out Put :-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

• JavaUser says:

that’s really great.

• musheer says:

this will not work for number’s ending with zero(0)..

• Vipin Shukla says:

i have a problem ,,want to find output as::: first program should asked number of line to enter if we enter 5 then following output should display
2
2 2
2 4 2
2 6 6 2
2 8 12 8 2

• kailash kore says:

please explain it line by line

• Shivapriya says:

can someone please explain the math logic behind this

• Prity gupta says:

Extra lengthy solution nd extra adding loop