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?










0















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");














share|improve this question









New contributor




hani hleihil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    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











  • What is the problem?

    – Joop Eggen
    Mar 7 at 10:12















0















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");














share|improve this question









New contributor




hani hleihil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    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











  • What is the problem?

    – Joop Eggen
    Mar 7 at 10:12













0












0








0








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");














share|improve this question









New contributor




hani hleihil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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






share|improve this question









New contributor




hani hleihil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




hani hleihil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Mar 7 at 10:13









Aditya Narayan Dixit

1,611513




1,611513






New contributor




hani hleihil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 7 at 9:54









hani hleihilhani hleihil

82




82




New contributor




hani hleihil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





hani hleihil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






hani hleihil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1





    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











  • What is the problem?

    – Joop Eggen
    Mar 7 at 10:12












  • 1





    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











  • 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












3 Answers
3






active

oldest

votes


















0














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");










share|improve this answer
































    1














    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");





    share|improve this answer


















    • 1





      You can do if((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


















    0














    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 :)






    share|improve this answer






















      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.









      draft saved

      draft discarded


















      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









      0














      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");










      share|improve this answer





























        0














        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");










        share|improve this answer



























          0












          0








          0







          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");










          share|improve this answer















          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");











          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 7 at 11:18

























          answered Mar 7 at 11:00









          EugeneEugene

          5351711




          5351711























              1














              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");





              share|improve this answer


















              • 1





                You can do if((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














              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");





              share|improve this answer


















              • 1





                You can do if((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








              1







              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");





              share|improve this answer













              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");






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 7 at 10:01









              Zeta ReticuliZeta Reticuli

              279212




              279212







              • 1





                You can do if((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





                You can do if((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











              0














              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 :)






              share|improve this answer



























                0














                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 :)






                share|improve this answer

























                  0












                  0








                  0







                  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 :)






                  share|improve this answer













                  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 :)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 7 at 10:30









                  Anish B.Anish B.

                  18611




                  18611




















                      hani hleihil is a new contributor. Be nice, and check out our Code of Conduct.









                      draft saved

                      draft discarded


















                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme