1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/* Reverse Number using Java This Java Reverse Number Example shows how to reverse a given number. */ public class ReverseNumber { public static void main(String[] args) { //original number int number = 1234; int reversedNumber = 0; int temp = 0; while(number > 0){ //use modulus operator to strip off the last digit temp = number%10; //create the reversed number reversedNumber = reversedNumber * 10 + temp; number = number/10; } //output the reversed number System.out.println("Reversed Number is: " + reversedNumber); } } /* Output of this Number Reverse program would be Reversed Number is: 4321 */ |
LOVELY SITE……
VRY USEFULL…..SIMPLE COADING APPROACH…..RELIABLE EXPLANATIONS..THNX
good………………..
Thanks a lot programs are helpful
nice it is very nice
why it only number is reverse
good…
can u please explain the logic
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 🙂
what is the purpose of temp variable
used to store a value temporily,it may not be used for future purpose….
niceeeee
Yeah but next time try fixing the indententation of your brackets. Makes it even easier to see visually.
nice stuff….very helpful
very very usefull to beginners of java program.
Really very good job ….
Benifit a lot for the very beginners,,,,
thanks 4 sharing
these programs are really helpful… thanks…
12345 54321
1234 4321
123 321
12 21
1 1
12 21
123 321
1234 4321
12345 54321
solution???????
number = number / 10 (this line is for what?????)
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;
how come 1234/10=123? shouldn’t it be 1234/10=123.4?
now i see, number is int )
What if the user inputs 980? the zero will not be displayed im sure
hadd he yaar sab khud se banane chahiye.. ban this site
itna aasan prog ni ban raha he to coding chod do sab log
hi can you help me with this question …
write a java program to find the square root of every number between 1 and 20 ???
System.out.println(new StringBuilder(String.valueOf(1234)).reverse());
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]);
}
}
}
How is it friends .. Try it
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
How to reverse array? Without using any util.
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.
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();
}
}
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
that’s really great.
this will not work for number’s ending with zero(0)..
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
please explain it line by line
can someone please explain the math logic behind this
Extra lengthy solution nd extra adding loop