I defined a random number and i want to check if it is a palindrome java2019 Community Moderator ElectionHow to round a number to n decimal places in JavaHow do I generate random integers within a specific range in Java?How to check if a String is numeric in JavaJava program Reversing a number and determining if it is a PalindromeJava Palindrome program not workingLargest Palindrom Product for JavaJava - Method executed prior to Default ConstructorHow to check the number of palindromes in a sentence?When use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regexWould it make any difference giving arguments using scanner class instead of command line arguments?
How to deal with a cynical class?
When two POV characters meet
What is the likely impact on flights of grounding an entire aircraft series?
What does it mean when multiple 々 marks follow a 、?
Single word request: Harming the benefactor
Touchscreen-controlled dentist office snowman collector game
Playing ONE triplet (not three)
How could a female member of a species produce eggs unto death?
Is it illegal in Germany to take sick leave if you caused your own illness with food?
Does Linux have system calls to access all the features of the file systems it supports?
Potentiometer like component
validation vs test vs training accuracy, which one to compare for claiming overfit?
Extension of Splitting Fields over An Arbitrary Field
Is going from continuous data to categorical always wrong?
Word for a person who has no opinion about whether god exists
Counter-example to the existence of left Bousfield localization of combinatorial model category
US to Europe trip with Montreal layover - is 52 minutes enough?
Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?
Is it true that real estate prices mainly go up?
What is the definition of "Natural Selection"?
Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?
Do I need to leave some extra space available on the disk which my database log files reside, for log backup operations to successfully occur?
What does おとこえしや mean?
What is the dot in “1.2.4."
I defined a random number and i want to check if it is a palindrome java
2019 Community Moderator ElectionHow to round a number to n decimal places in JavaHow do I generate random integers within a specific range in Java?How to check if a String is numeric in JavaJava program Reversing a number and determining if it is a PalindromeJava Palindrome program not workingLargest Palindrom Product for JavaJava - Method executed prior to Default ConstructorHow to check the number of palindromes in a sentence?When use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regexWould it make any difference giving arguments using scanner class instead of command line arguments?
import java.util.Random;
public class Loop6
public static void main(String[] args)
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0)
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
System.out.println("rev is : " + "" + rev);
if(value==rev)
System.out.println("Palindrome");
java palindrome
New contributor
add a comment |
import java.util.Random;
public class Loop6
public static void main(String[] args)
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0)
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
System.out.println("rev is : " + "" + rev);
if(value==rev)
System.out.println("Palindrome");
java palindrome
New contributor
1
Store the value ofvalue
in a temp variable and then compare temp variable withrev
.
– Sudhir Ojha
Mar 7 at 9:57
do not change value,take another variable initialize with value and do calculation on that variable.
– vivekdubey
Mar 7 at 10:01
What is the problem?
– Joop Eggen
Mar 7 at 10:12
add a comment |
import java.util.Random;
public class Loop6
public static void main(String[] args)
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0)
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
System.out.println("rev is : " + "" + rev);
if(value==rev)
System.out.println("Palindrome");
java palindrome
New contributor
import java.util.Random;
public class Loop6
public static void main(String[] args)
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0)
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
System.out.println("rev is : " + "" + rev);
if(value==rev)
System.out.println("Palindrome");
java palindrome
java palindrome
New contributor
New contributor
edited Mar 7 at 10:13
Aditya Narayan Dixit
1,611513
1,611513
New contributor
asked Mar 7 at 9:54
hani hleihilhani hleihil
82
82
New contributor
New contributor
1
Store the value ofvalue
in a temp variable and then compare temp variable withrev
.
– Sudhir Ojha
Mar 7 at 9:57
do not change value,take another variable initialize with value and do calculation on that variable.
– vivekdubey
Mar 7 at 10:01
What is the problem?
– Joop Eggen
Mar 7 at 10:12
add a comment |
1
Store the value ofvalue
in a temp variable and then compare temp variable withrev
.
– Sudhir Ojha
Mar 7 at 9:57
do not change value,take another variable initialize with value and do calculation on that variable.
– vivekdubey
Mar 7 at 10:01
What is the problem?
– Joop Eggen
Mar 7 at 10:12
1
1
Store the value of
value
in a temp variable and then compare temp variable with rev
.– Sudhir Ojha
Mar 7 at 9:57
Store the value of
value
in a temp variable and then compare temp variable with rev
.– Sudhir Ojha
Mar 7 at 9:57
do not change value,take another variable initialize with value and do calculation on that variable.
– vivekdubey
Mar 7 at 10:01
do not change value,take another variable initialize with value and do calculation on that variable.
– vivekdubey
Mar 7 at 10:01
What is the problem?
– Joop Eggen
Mar 7 at 10:12
What is the problem?
– Joop Eggen
Mar 7 at 10:12
add a comment |
3 Answers
3
active
oldest
votes
You code is almost fine, it's just you're updating the initial random number which is not what you want. To keep changes to the code at minimal, I propose you to add a new variable and compare it to the result of your reverse algorithm.
import java.util.Random;
public class Loop6
public static void main(String[] args)
Random number = new Random();
int randomNumber = number.nextInt(1000); // introduce the initial variable
int value = randomNumber; // introduce the variable that will be updated
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0)
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
System.out.println("rev is : " + "" + rev);
if (randomNumber == rev) // compare the initial variable and the reverse result
System.out.println("Palindrome");
add a comment |
Java has built-in methods to reverse the strings.
String originalValue = value + "";
StringBuilder stringBuilder = new StringBuilder(originalValue);
String reverseValue = stringBuilder.reverse().toString();
if (originalValue.equals(reverseValue)) System.out.println("Palindrome");
1
You can doif((value+"").equals(new StringBuilder().append(value).reverse().toString())
+1
– Peter Lawrey
Mar 7 at 10:17
1
agreed, but having in mind Java expertise of the author(strictly IMHO), I wanted a more readable solution.
– Zeta Reticuli
Mar 7 at 10:20
agreed, which is why I commented on your answer to you instead of posting it ;)
– Peter Lawrey
Mar 7 at 10:25
add a comment |
Solution :
public class Test
public static void main(String[] args)
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("Original number : " + " " + value);
int tmp = value;
int result = 0;
while (tmp != 0)
result = result * 10 + (tmp % 10);
tmp = tmp / 10;
System.out.println("Reversed Number : " + " " + result);
if (result != value)
System.out.println("Not Palindrome");
else
System.out.println("Palindrome");
Thanks :)
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
);
);
hani hleihil is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55040801%2fi-defined-a-random-number-and-i-want-to-check-if-it-is-a-palindrome-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
You code is almost fine, it's just you're updating the initial random number which is not what you want. To keep changes to the code at minimal, I propose you to add a new variable and compare it to the result of your reverse algorithm.
import java.util.Random;
public class Loop6
public static void main(String[] args)
Random number = new Random();
int randomNumber = number.nextInt(1000); // introduce the initial variable
int value = randomNumber; // introduce the variable that will be updated
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0)
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
System.out.println("rev is : " + "" + rev);
if (randomNumber == rev) // compare the initial variable and the reverse result
System.out.println("Palindrome");
add a comment |
You code is almost fine, it's just you're updating the initial random number which is not what you want. To keep changes to the code at minimal, I propose you to add a new variable and compare it to the result of your reverse algorithm.
import java.util.Random;
public class Loop6
public static void main(String[] args)
Random number = new Random();
int randomNumber = number.nextInt(1000); // introduce the initial variable
int value = randomNumber; // introduce the variable that will be updated
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0)
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
System.out.println("rev is : " + "" + rev);
if (randomNumber == rev) // compare the initial variable and the reverse result
System.out.println("Palindrome");
add a comment |
You code is almost fine, it's just you're updating the initial random number which is not what you want. To keep changes to the code at minimal, I propose you to add a new variable and compare it to the result of your reverse algorithm.
import java.util.Random;
public class Loop6
public static void main(String[] args)
Random number = new Random();
int randomNumber = number.nextInt(1000); // introduce the initial variable
int value = randomNumber; // introduce the variable that will be updated
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0)
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
System.out.println("rev is : " + "" + rev);
if (randomNumber == rev) // compare the initial variable and the reverse result
System.out.println("Palindrome");
You code is almost fine, it's just you're updating the initial random number which is not what you want. To keep changes to the code at minimal, I propose you to add a new variable and compare it to the result of your reverse algorithm.
import java.util.Random;
public class Loop6
public static void main(String[] args)
Random number = new Random();
int randomNumber = number.nextInt(1000); // introduce the initial variable
int value = randomNumber; // introduce the variable that will be updated
System.out.println("random number : " + " " + value);
int rev = 0;
int dig;
while (value > 0)
dig = value % 10;
rev = rev * 10;
rev = rev + dig;
value = value / 10;
System.out.println("rev is : " + "" + rev);
if (randomNumber == rev) // compare the initial variable and the reverse result
System.out.println("Palindrome");
edited Mar 7 at 11:18
answered Mar 7 at 11:00
EugeneEugene
5351711
5351711
add a comment |
add a comment |
Java has built-in methods to reverse the strings.
String originalValue = value + "";
StringBuilder stringBuilder = new StringBuilder(originalValue);
String reverseValue = stringBuilder.reverse().toString();
if (originalValue.equals(reverseValue)) System.out.println("Palindrome");
1
You can doif((value+"").equals(new StringBuilder().append(value).reverse().toString())
+1
– Peter Lawrey
Mar 7 at 10:17
1
agreed, but having in mind Java expertise of the author(strictly IMHO), I wanted a more readable solution.
– Zeta Reticuli
Mar 7 at 10:20
agreed, which is why I commented on your answer to you instead of posting it ;)
– Peter Lawrey
Mar 7 at 10:25
add a comment |
Java has built-in methods to reverse the strings.
String originalValue = value + "";
StringBuilder stringBuilder = new StringBuilder(originalValue);
String reverseValue = stringBuilder.reverse().toString();
if (originalValue.equals(reverseValue)) System.out.println("Palindrome");
1
You can doif((value+"").equals(new StringBuilder().append(value).reverse().toString())
+1
– Peter Lawrey
Mar 7 at 10:17
1
agreed, but having in mind Java expertise of the author(strictly IMHO), I wanted a more readable solution.
– Zeta Reticuli
Mar 7 at 10:20
agreed, which is why I commented on your answer to you instead of posting it ;)
– Peter Lawrey
Mar 7 at 10:25
add a comment |
Java has built-in methods to reverse the strings.
String originalValue = value + "";
StringBuilder stringBuilder = new StringBuilder(originalValue);
String reverseValue = stringBuilder.reverse().toString();
if (originalValue.equals(reverseValue)) System.out.println("Palindrome");
Java has built-in methods to reverse the strings.
String originalValue = value + "";
StringBuilder stringBuilder = new StringBuilder(originalValue);
String reverseValue = stringBuilder.reverse().toString();
if (originalValue.equals(reverseValue)) System.out.println("Palindrome");
answered Mar 7 at 10:01
Zeta ReticuliZeta Reticuli
279212
279212
1
You can doif((value+"").equals(new StringBuilder().append(value).reverse().toString())
+1
– Peter Lawrey
Mar 7 at 10:17
1
agreed, but having in mind Java expertise of the author(strictly IMHO), I wanted a more readable solution.
– Zeta Reticuli
Mar 7 at 10:20
agreed, which is why I commented on your answer to you instead of posting it ;)
– Peter Lawrey
Mar 7 at 10:25
add a comment |
1
You can doif((value+"").equals(new StringBuilder().append(value).reverse().toString())
+1
– Peter Lawrey
Mar 7 at 10:17
1
agreed, but having in mind Java expertise of the author(strictly IMHO), I wanted a more readable solution.
– Zeta Reticuli
Mar 7 at 10:20
agreed, which is why I commented on your answer to you instead of posting it ;)
– Peter Lawrey
Mar 7 at 10:25
1
1
You can do
if((value+"").equals(new StringBuilder().append(value).reverse().toString())
+1– Peter Lawrey
Mar 7 at 10:17
You can do
if((value+"").equals(new StringBuilder().append(value).reverse().toString())
+1– Peter Lawrey
Mar 7 at 10:17
1
1
agreed, but having in mind Java expertise of the author(strictly IMHO), I wanted a more readable solution.
– Zeta Reticuli
Mar 7 at 10:20
agreed, but having in mind Java expertise of the author(strictly IMHO), I wanted a more readable solution.
– Zeta Reticuli
Mar 7 at 10:20
agreed, which is why I commented on your answer to you instead of posting it ;)
– Peter Lawrey
Mar 7 at 10:25
agreed, which is why I commented on your answer to you instead of posting it ;)
– Peter Lawrey
Mar 7 at 10:25
add a comment |
Solution :
public class Test
public static void main(String[] args)
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("Original number : " + " " + value);
int tmp = value;
int result = 0;
while (tmp != 0)
result = result * 10 + (tmp % 10);
tmp = tmp / 10;
System.out.println("Reversed Number : " + " " + result);
if (result != value)
System.out.println("Not Palindrome");
else
System.out.println("Palindrome");
Thanks :)
add a comment |
Solution :
public class Test
public static void main(String[] args)
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("Original number : " + " " + value);
int tmp = value;
int result = 0;
while (tmp != 0)
result = result * 10 + (tmp % 10);
tmp = tmp / 10;
System.out.println("Reversed Number : " + " " + result);
if (result != value)
System.out.println("Not Palindrome");
else
System.out.println("Palindrome");
Thanks :)
add a comment |
Solution :
public class Test
public static void main(String[] args)
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("Original number : " + " " + value);
int tmp = value;
int result = 0;
while (tmp != 0)
result = result * 10 + (tmp % 10);
tmp = tmp / 10;
System.out.println("Reversed Number : " + " " + result);
if (result != value)
System.out.println("Not Palindrome");
else
System.out.println("Palindrome");
Thanks :)
Solution :
public class Test
public static void main(String[] args)
Random number = new Random();
int value = number.nextInt(1000);
System.out.println("Original number : " + " " + value);
int tmp = value;
int result = 0;
while (tmp != 0)
result = result * 10 + (tmp % 10);
tmp = tmp / 10;
System.out.println("Reversed Number : " + " " + result);
if (result != value)
System.out.println("Not Palindrome");
else
System.out.println("Palindrome");
Thanks :)
answered Mar 7 at 10:30
Anish B.Anish B.
18611
18611
add a comment |
add a comment |
hani hleihil is a new contributor. Be nice, and check out our Code of Conduct.
hani hleihil is a new contributor. Be nice, and check out our Code of Conduct.
hani hleihil is a new contributor. Be nice, and check out our Code of Conduct.
hani hleihil is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55040801%2fi-defined-a-random-number-and-i-want-to-check-if-it-is-a-palindrome-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
1
Store the value of
value
in a temp variable and then compare temp variable withrev
.– Sudhir Ojha
Mar 7 at 9:57
do not change value,take another variable initialize with value and do calculation on that variable.
– vivekdubey
Mar 7 at 10:01
What is the problem?
– Joop Eggen
Mar 7 at 10:12