Making a number in reverse using for loop java
So I'm trying to reverse a number in java using a forloop, I get the right value but I'm not sure if thats the best way of doing it.
package forloops;
/*
% prints the last number in the sequence
/ prints every number except for the last one
*/
public class modulusForLoops {
public static void main(Stringargs) {
int orig = 123456789;
int num = orig;
for (int i = 0; i < 1; i++) {
num = orig % 10; //9
int secondDigit = orig / 10; //12345678
int secondDigitPrinted = secondDigit % 10; //8
int thirdDigit = secondDigit / 10; //1234567
int thirdDigitPrinted = thirdDigit % 10; //7
int fourthDigit = thirdDigit / 10; //123456
int fourthDigitPrinted = fourthDigit % 10; //6
int fifthDigit = fourthDigit / 10; //12345
int fifthDigitPrinted = fifthDigit % 10;
int sixthDigit = fifthDigit / 10; //1234
int sixthDigitPrinted = sixthDigit % 10; //4
int seventhDigit = sixthDigit / 10; //123
int seventhDigitPrinted = seventhDigit % 10; //3
int eigthDigit = seventhDigit / 10; //12
int eigthDigitPrinted = eigthDigit % 10; //2
int lastDigit = eigthDigit / 10; //1
System.out.println(orig + " reversed is " + num + secondDigitPrinted + thirdDigitPrinted + fourthDigitPrinted + fifthDigitPrinted + sixthDigitPrinted + seventhDigitPrinted + eigthDigitPrinted + lastDigit);
}
}
}
java
add a comment |
So I'm trying to reverse a number in java using a forloop, I get the right value but I'm not sure if thats the best way of doing it.
package forloops;
/*
% prints the last number in the sequence
/ prints every number except for the last one
*/
public class modulusForLoops {
public static void main(Stringargs) {
int orig = 123456789;
int num = orig;
for (int i = 0; i < 1; i++) {
num = orig % 10; //9
int secondDigit = orig / 10; //12345678
int secondDigitPrinted = secondDigit % 10; //8
int thirdDigit = secondDigit / 10; //1234567
int thirdDigitPrinted = thirdDigit % 10; //7
int fourthDigit = thirdDigit / 10; //123456
int fourthDigitPrinted = fourthDigit % 10; //6
int fifthDigit = fourthDigit / 10; //12345
int fifthDigitPrinted = fifthDigit % 10;
int sixthDigit = fifthDigit / 10; //1234
int sixthDigitPrinted = sixthDigit % 10; //4
int seventhDigit = sixthDigit / 10; //123
int seventhDigitPrinted = seventhDigit % 10; //3
int eigthDigit = seventhDigit / 10; //12
int eigthDigitPrinted = eigthDigit % 10; //2
int lastDigit = eigthDigit / 10; //1
System.out.println(orig + " reversed is " + num + secondDigitPrinted + thirdDigitPrinted + fourthDigitPrinted + fifthDigitPrinted + sixthDigitPrinted + seventhDigitPrinted + eigthDigitPrinted + lastDigit);
}
}
}
java
It is the worst way of doing it. You are limited to fixed length number, and theforloop have run just once. So you may roll your similar statement into one, and useforto unroll them.
– Geno Chen
Nov 28 '18 at 3:34
Looks like you are doing a homework task. I won't provide you with an answer but will try and point you in the right direction.The way you are doing it is not correct at all the for loop is essentially pointless. You might want to think about how you could iterate over each number. You might need to put the number into a different data structure.
– Ben Avery
Nov 28 '18 at 3:35
Oh thanks. Maybe I could use a string?
– user10113279
Nov 28 '18 at 3:38
add a comment |
So I'm trying to reverse a number in java using a forloop, I get the right value but I'm not sure if thats the best way of doing it.
package forloops;
/*
% prints the last number in the sequence
/ prints every number except for the last one
*/
public class modulusForLoops {
public static void main(Stringargs) {
int orig = 123456789;
int num = orig;
for (int i = 0; i < 1; i++) {
num = orig % 10; //9
int secondDigit = orig / 10; //12345678
int secondDigitPrinted = secondDigit % 10; //8
int thirdDigit = secondDigit / 10; //1234567
int thirdDigitPrinted = thirdDigit % 10; //7
int fourthDigit = thirdDigit / 10; //123456
int fourthDigitPrinted = fourthDigit % 10; //6
int fifthDigit = fourthDigit / 10; //12345
int fifthDigitPrinted = fifthDigit % 10;
int sixthDigit = fifthDigit / 10; //1234
int sixthDigitPrinted = sixthDigit % 10; //4
int seventhDigit = sixthDigit / 10; //123
int seventhDigitPrinted = seventhDigit % 10; //3
int eigthDigit = seventhDigit / 10; //12
int eigthDigitPrinted = eigthDigit % 10; //2
int lastDigit = eigthDigit / 10; //1
System.out.println(orig + " reversed is " + num + secondDigitPrinted + thirdDigitPrinted + fourthDigitPrinted + fifthDigitPrinted + sixthDigitPrinted + seventhDigitPrinted + eigthDigitPrinted + lastDigit);
}
}
}
java
So I'm trying to reverse a number in java using a forloop, I get the right value but I'm not sure if thats the best way of doing it.
package forloops;
/*
% prints the last number in the sequence
/ prints every number except for the last one
*/
public class modulusForLoops {
public static void main(Stringargs) {
int orig = 123456789;
int num = orig;
for (int i = 0; i < 1; i++) {
num = orig % 10; //9
int secondDigit = orig / 10; //12345678
int secondDigitPrinted = secondDigit % 10; //8
int thirdDigit = secondDigit / 10; //1234567
int thirdDigitPrinted = thirdDigit % 10; //7
int fourthDigit = thirdDigit / 10; //123456
int fourthDigitPrinted = fourthDigit % 10; //6
int fifthDigit = fourthDigit / 10; //12345
int fifthDigitPrinted = fifthDigit % 10;
int sixthDigit = fifthDigit / 10; //1234
int sixthDigitPrinted = sixthDigit % 10; //4
int seventhDigit = sixthDigit / 10; //123
int seventhDigitPrinted = seventhDigit % 10; //3
int eigthDigit = seventhDigit / 10; //12
int eigthDigitPrinted = eigthDigit % 10; //2
int lastDigit = eigthDigit / 10; //1
System.out.println(orig + " reversed is " + num + secondDigitPrinted + thirdDigitPrinted + fourthDigitPrinted + fifthDigitPrinted + sixthDigitPrinted + seventhDigitPrinted + eigthDigitPrinted + lastDigit);
}
}
}
java
java
asked Nov 28 '18 at 3:28
user10113279
It is the worst way of doing it. You are limited to fixed length number, and theforloop have run just once. So you may roll your similar statement into one, and useforto unroll them.
– Geno Chen
Nov 28 '18 at 3:34
Looks like you are doing a homework task. I won't provide you with an answer but will try and point you in the right direction.The way you are doing it is not correct at all the for loop is essentially pointless. You might want to think about how you could iterate over each number. You might need to put the number into a different data structure.
– Ben Avery
Nov 28 '18 at 3:35
Oh thanks. Maybe I could use a string?
– user10113279
Nov 28 '18 at 3:38
add a comment |
It is the worst way of doing it. You are limited to fixed length number, and theforloop have run just once. So you may roll your similar statement into one, and useforto unroll them.
– Geno Chen
Nov 28 '18 at 3:34
Looks like you are doing a homework task. I won't provide you with an answer but will try and point you in the right direction.The way you are doing it is not correct at all the for loop is essentially pointless. You might want to think about how you could iterate over each number. You might need to put the number into a different data structure.
– Ben Avery
Nov 28 '18 at 3:35
Oh thanks. Maybe I could use a string?
– user10113279
Nov 28 '18 at 3:38
It is the worst way of doing it. You are limited to fixed length number, and the
for loop have run just once. So you may roll your similar statement into one, and use for to unroll them.– Geno Chen
Nov 28 '18 at 3:34
It is the worst way of doing it. You are limited to fixed length number, and the
for loop have run just once. So you may roll your similar statement into one, and use for to unroll them.– Geno Chen
Nov 28 '18 at 3:34
Looks like you are doing a homework task. I won't provide you with an answer but will try and point you in the right direction.The way you are doing it is not correct at all the for loop is essentially pointless. You might want to think about how you could iterate over each number. You might need to put the number into a different data structure.
– Ben Avery
Nov 28 '18 at 3:35
Looks like you are doing a homework task. I won't provide you with an answer but will try and point you in the right direction.The way you are doing it is not correct at all the for loop is essentially pointless. You might want to think about how you could iterate over each number. You might need to put the number into a different data structure.
– Ben Avery
Nov 28 '18 at 3:35
Oh thanks. Maybe I could use a string?
– user10113279
Nov 28 '18 at 3:38
Oh thanks. Maybe I could use a string?
– user10113279
Nov 28 '18 at 3:38
add a comment |
3 Answers
3
active
oldest
votes
Let's stick with the logic you have in mind to reverse any number. For better understanding, let's list out the algorithm steps that you are using.
Repeat below steps until there are no digits left in the given number:
I) get the last digit from the number, i.e. lastDigit = number % 10
II) remove the last digit from the number, i.e. numberWithoutLast = number / 10
When we want to go through a sequence of steps multiple times, i.e. repeat them, we make use of the looping structures like for, while or do...while
Therefore, if we were to rewrite your program-the loop part-it would be as follows:
public static void main(String ar) {
int orig = 123456789;
int lastDigit = 0;
/* we'll use the copy of original number for step I & II
* instead of messing with the original number
*/
int numberWithoutLast = orig;
String reversed = ""; // we'll use this to store every last digit
for(int i = 0;
i < Integer.toString(orig).length(); /* this will repeat the loop for number of digits in "orig" */
i++) {
lastDigit = numberWithoutLast % 10;
reversed += Integer.toString(lastDigit);
numberWithoutLast = numberWithoutLast / 10;
}
// lastly we print the reversed number
System.out.println("Reversed Number: " + reversed);
}
This was the manual way of reversing an integer. For an automatic way, you can have a look at @Andreas's answer.
add a comment |
You could simply convert it to String and using java.lang.StringBuilder reverse the string.
int orig = 123456789;
String numString = Integer.toString(orig);
String reversed = "";
for (int i = numString.length() - 1; i >= 0; i--) { // loop through the string from back to front
reversed += numString.charAt(i); // add each character to the resulting string
}
System.out.println(reversed);
Or alternatively
int orig = 123456789;
String numString = Integer.toString(orig); // convert int to String
String reversed = new StringBuilder(numString).reverse().toString(); // reverse string
System.out.println(reversed);
Edited my answer to includefor-loop
– Andreas
Nov 28 '18 at 3:54
Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.
– Sikorski
Nov 28 '18 at 3:56
I believe modulus is not the main requirement since OP has even considered using string in the question's comment. In addition to that, modulus has never been mentioned in the question, and it is implied that OP is asking for a better way of doing it. As for for-loop, I already include that in the answer.
– Andreas
Nov 28 '18 at 3:59
Read the class name in his program, and he's using mod in code
– Sikorski
Nov 28 '18 at 5:05
add a comment |
Just in case you want to know how to do it by modulus and loop. The idea is to pop the unit digit from the source and push it to the destination in every iteration, in a number way.
int orig = 123456789; //assume > 0
int num = 0;
for(int temp = orig;temp > 0;temp/=10)
{
num = num * 10 + temp % 10;
}
System.out.println(orig + " reversed is " + num);
I figured it out before thanks.
– user10113279
Nov 28 '18 at 4:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511663%2fmaking-a-number-in-reverse-using-for-loop-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let's stick with the logic you have in mind to reverse any number. For better understanding, let's list out the algorithm steps that you are using.
Repeat below steps until there are no digits left in the given number:
I) get the last digit from the number, i.e. lastDigit = number % 10
II) remove the last digit from the number, i.e. numberWithoutLast = number / 10
When we want to go through a sequence of steps multiple times, i.e. repeat them, we make use of the looping structures like for, while or do...while
Therefore, if we were to rewrite your program-the loop part-it would be as follows:
public static void main(String ar) {
int orig = 123456789;
int lastDigit = 0;
/* we'll use the copy of original number for step I & II
* instead of messing with the original number
*/
int numberWithoutLast = orig;
String reversed = ""; // we'll use this to store every last digit
for(int i = 0;
i < Integer.toString(orig).length(); /* this will repeat the loop for number of digits in "orig" */
i++) {
lastDigit = numberWithoutLast % 10;
reversed += Integer.toString(lastDigit);
numberWithoutLast = numberWithoutLast / 10;
}
// lastly we print the reversed number
System.out.println("Reversed Number: " + reversed);
}
This was the manual way of reversing an integer. For an automatic way, you can have a look at @Andreas's answer.
add a comment |
Let's stick with the logic you have in mind to reverse any number. For better understanding, let's list out the algorithm steps that you are using.
Repeat below steps until there are no digits left in the given number:
I) get the last digit from the number, i.e. lastDigit = number % 10
II) remove the last digit from the number, i.e. numberWithoutLast = number / 10
When we want to go through a sequence of steps multiple times, i.e. repeat them, we make use of the looping structures like for, while or do...while
Therefore, if we were to rewrite your program-the loop part-it would be as follows:
public static void main(String ar) {
int orig = 123456789;
int lastDigit = 0;
/* we'll use the copy of original number for step I & II
* instead of messing with the original number
*/
int numberWithoutLast = orig;
String reversed = ""; // we'll use this to store every last digit
for(int i = 0;
i < Integer.toString(orig).length(); /* this will repeat the loop for number of digits in "orig" */
i++) {
lastDigit = numberWithoutLast % 10;
reversed += Integer.toString(lastDigit);
numberWithoutLast = numberWithoutLast / 10;
}
// lastly we print the reversed number
System.out.println("Reversed Number: " + reversed);
}
This was the manual way of reversing an integer. For an automatic way, you can have a look at @Andreas's answer.
add a comment |
Let's stick with the logic you have in mind to reverse any number. For better understanding, let's list out the algorithm steps that you are using.
Repeat below steps until there are no digits left in the given number:
I) get the last digit from the number, i.e. lastDigit = number % 10
II) remove the last digit from the number, i.e. numberWithoutLast = number / 10
When we want to go through a sequence of steps multiple times, i.e. repeat them, we make use of the looping structures like for, while or do...while
Therefore, if we were to rewrite your program-the loop part-it would be as follows:
public static void main(String ar) {
int orig = 123456789;
int lastDigit = 0;
/* we'll use the copy of original number for step I & II
* instead of messing with the original number
*/
int numberWithoutLast = orig;
String reversed = ""; // we'll use this to store every last digit
for(int i = 0;
i < Integer.toString(orig).length(); /* this will repeat the loop for number of digits in "orig" */
i++) {
lastDigit = numberWithoutLast % 10;
reversed += Integer.toString(lastDigit);
numberWithoutLast = numberWithoutLast / 10;
}
// lastly we print the reversed number
System.out.println("Reversed Number: " + reversed);
}
This was the manual way of reversing an integer. For an automatic way, you can have a look at @Andreas's answer.
Let's stick with the logic you have in mind to reverse any number. For better understanding, let's list out the algorithm steps that you are using.
Repeat below steps until there are no digits left in the given number:
I) get the last digit from the number, i.e. lastDigit = number % 10
II) remove the last digit from the number, i.e. numberWithoutLast = number / 10
When we want to go through a sequence of steps multiple times, i.e. repeat them, we make use of the looping structures like for, while or do...while
Therefore, if we were to rewrite your program-the loop part-it would be as follows:
public static void main(String ar) {
int orig = 123456789;
int lastDigit = 0;
/* we'll use the copy of original number for step I & II
* instead of messing with the original number
*/
int numberWithoutLast = orig;
String reversed = ""; // we'll use this to store every last digit
for(int i = 0;
i < Integer.toString(orig).length(); /* this will repeat the loop for number of digits in "orig" */
i++) {
lastDigit = numberWithoutLast % 10;
reversed += Integer.toString(lastDigit);
numberWithoutLast = numberWithoutLast / 10;
}
// lastly we print the reversed number
System.out.println("Reversed Number: " + reversed);
}
This was the manual way of reversing an integer. For an automatic way, you can have a look at @Andreas's answer.
answered Nov 28 '18 at 4:42
Dhruv JoshiDhruv Joshi
6625
6625
add a comment |
add a comment |
You could simply convert it to String and using java.lang.StringBuilder reverse the string.
int orig = 123456789;
String numString = Integer.toString(orig);
String reversed = "";
for (int i = numString.length() - 1; i >= 0; i--) { // loop through the string from back to front
reversed += numString.charAt(i); // add each character to the resulting string
}
System.out.println(reversed);
Or alternatively
int orig = 123456789;
String numString = Integer.toString(orig); // convert int to String
String reversed = new StringBuilder(numString).reverse().toString(); // reverse string
System.out.println(reversed);
Edited my answer to includefor-loop
– Andreas
Nov 28 '18 at 3:54
Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.
– Sikorski
Nov 28 '18 at 3:56
I believe modulus is not the main requirement since OP has even considered using string in the question's comment. In addition to that, modulus has never been mentioned in the question, and it is implied that OP is asking for a better way of doing it. As for for-loop, I already include that in the answer.
– Andreas
Nov 28 '18 at 3:59
Read the class name in his program, and he's using mod in code
– Sikorski
Nov 28 '18 at 5:05
add a comment |
You could simply convert it to String and using java.lang.StringBuilder reverse the string.
int orig = 123456789;
String numString = Integer.toString(orig);
String reversed = "";
for (int i = numString.length() - 1; i >= 0; i--) { // loop through the string from back to front
reversed += numString.charAt(i); // add each character to the resulting string
}
System.out.println(reversed);
Or alternatively
int orig = 123456789;
String numString = Integer.toString(orig); // convert int to String
String reversed = new StringBuilder(numString).reverse().toString(); // reverse string
System.out.println(reversed);
Edited my answer to includefor-loop
– Andreas
Nov 28 '18 at 3:54
Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.
– Sikorski
Nov 28 '18 at 3:56
I believe modulus is not the main requirement since OP has even considered using string in the question's comment. In addition to that, modulus has never been mentioned in the question, and it is implied that OP is asking for a better way of doing it. As for for-loop, I already include that in the answer.
– Andreas
Nov 28 '18 at 3:59
Read the class name in his program, and he's using mod in code
– Sikorski
Nov 28 '18 at 5:05
add a comment |
You could simply convert it to String and using java.lang.StringBuilder reverse the string.
int orig = 123456789;
String numString = Integer.toString(orig);
String reversed = "";
for (int i = numString.length() - 1; i >= 0; i--) { // loop through the string from back to front
reversed += numString.charAt(i); // add each character to the resulting string
}
System.out.println(reversed);
Or alternatively
int orig = 123456789;
String numString = Integer.toString(orig); // convert int to String
String reversed = new StringBuilder(numString).reverse().toString(); // reverse string
System.out.println(reversed);
You could simply convert it to String and using java.lang.StringBuilder reverse the string.
int orig = 123456789;
String numString = Integer.toString(orig);
String reversed = "";
for (int i = numString.length() - 1; i >= 0; i--) { // loop through the string from back to front
reversed += numString.charAt(i); // add each character to the resulting string
}
System.out.println(reversed);
Or alternatively
int orig = 123456789;
String numString = Integer.toString(orig); // convert int to String
String reversed = new StringBuilder(numString).reverse().toString(); // reverse string
System.out.println(reversed);
edited Nov 28 '18 at 3:53
answered Nov 28 '18 at 3:43
AndreasAndreas
2,05931223
2,05931223
Edited my answer to includefor-loop
– Andreas
Nov 28 '18 at 3:54
Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.
– Sikorski
Nov 28 '18 at 3:56
I believe modulus is not the main requirement since OP has even considered using string in the question's comment. In addition to that, modulus has never been mentioned in the question, and it is implied that OP is asking for a better way of doing it. As for for-loop, I already include that in the answer.
– Andreas
Nov 28 '18 at 3:59
Read the class name in his program, and he's using mod in code
– Sikorski
Nov 28 '18 at 5:05
add a comment |
Edited my answer to includefor-loop
– Andreas
Nov 28 '18 at 3:54
Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.
– Sikorski
Nov 28 '18 at 3:56
I believe modulus is not the main requirement since OP has even considered using string in the question's comment. In addition to that, modulus has never been mentioned in the question, and it is implied that OP is asking for a better way of doing it. As for for-loop, I already include that in the answer.
– Andreas
Nov 28 '18 at 3:59
Read the class name in his program, and he's using mod in code
– Sikorski
Nov 28 '18 at 5:05
Edited my answer to include
for-loop– Andreas
Nov 28 '18 at 3:54
Edited my answer to include
for-loop– Andreas
Nov 28 '18 at 3:54
Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.
– Sikorski
Nov 28 '18 at 3:56
Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.
– Sikorski
Nov 28 '18 at 3:56
I believe modulus is not the main requirement since OP has even considered using string in the question's comment. In addition to that, modulus has never been mentioned in the question, and it is implied that OP is asking for a better way of doing it. As for for-loop, I already include that in the answer.
– Andreas
Nov 28 '18 at 3:59
I believe modulus is not the main requirement since OP has even considered using string in the question's comment. In addition to that, modulus has never been mentioned in the question, and it is implied that OP is asking for a better way of doing it. As for for-loop, I already include that in the answer.
– Andreas
Nov 28 '18 at 3:59
Read the class name in his program, and he's using mod in code
– Sikorski
Nov 28 '18 at 5:05
Read the class name in his program, and he's using mod in code
– Sikorski
Nov 28 '18 at 5:05
add a comment |
Just in case you want to know how to do it by modulus and loop. The idea is to pop the unit digit from the source and push it to the destination in every iteration, in a number way.
int orig = 123456789; //assume > 0
int num = 0;
for(int temp = orig;temp > 0;temp/=10)
{
num = num * 10 + temp % 10;
}
System.out.println(orig + " reversed is " + num);
I figured it out before thanks.
– user10113279
Nov 28 '18 at 4:35
add a comment |
Just in case you want to know how to do it by modulus and loop. The idea is to pop the unit digit from the source and push it to the destination in every iteration, in a number way.
int orig = 123456789; //assume > 0
int num = 0;
for(int temp = orig;temp > 0;temp/=10)
{
num = num * 10 + temp % 10;
}
System.out.println(orig + " reversed is " + num);
I figured it out before thanks.
– user10113279
Nov 28 '18 at 4:35
add a comment |
Just in case you want to know how to do it by modulus and loop. The idea is to pop the unit digit from the source and push it to the destination in every iteration, in a number way.
int orig = 123456789; //assume > 0
int num = 0;
for(int temp = orig;temp > 0;temp/=10)
{
num = num * 10 + temp % 10;
}
System.out.println(orig + " reversed is " + num);
Just in case you want to know how to do it by modulus and loop. The idea is to pop the unit digit from the source and push it to the destination in every iteration, in a number way.
int orig = 123456789; //assume > 0
int num = 0;
for(int temp = orig;temp > 0;temp/=10)
{
num = num * 10 + temp % 10;
}
System.out.println(orig + " reversed is " + num);
answered Nov 28 '18 at 4:15
Ricky MoRicky Mo
1,7471212
1,7471212
I figured it out before thanks.
– user10113279
Nov 28 '18 at 4:35
add a comment |
I figured it out before thanks.
– user10113279
Nov 28 '18 at 4:35
I figured it out before thanks.
– user10113279
Nov 28 '18 at 4:35
I figured it out before thanks.
– user10113279
Nov 28 '18 at 4:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511663%2fmaking-a-number-in-reverse-using-for-loop-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It is the worst way of doing it. You are limited to fixed length number, and the
forloop have run just once. So you may roll your similar statement into one, and useforto unroll them.– Geno Chen
Nov 28 '18 at 3:34
Looks like you are doing a homework task. I won't provide you with an answer but will try and point you in the right direction.The way you are doing it is not correct at all the for loop is essentially pointless. You might want to think about how you could iterate over each number. You might need to put the number into a different data structure.
– Ben Avery
Nov 28 '18 at 3:35
Oh thanks. Maybe I could use a string?
– user10113279
Nov 28 '18 at 3:38