Flip integer reverse digits of a number

Source: Internet
Author: User

You can flip an integer in either of the following ways: sequential flip and recursive flip.

Overflow is not considered here


Recursion enables reverse processing, that is, processing from the lowest end of the recursive stack, which can be obtained through drawing.

For Rec (Num/10 ):

12345

1234

123

12

1 <-- recursion makes this first Process


Package recursion; public class reverse_digits_of_a_number {public static void main (string [] ARGs) {int num = 123; system. out. println (reverse (Num); system. out. println (Rec (Num);} public static int reverse (INT num) {int REV = 0; while (num! = 0) {REV = rev * 10 + num % 10; num/= 10; // 10 shifts one digit to the right in decimal format} return Rev ;} static int revnum = 0; static int base = 1; public static int Rec (INT num) {If (num = 0) {return num;} Rec (Num/10 ); // It is equivalent to the block here until the recursion ends, for example, num = 123-> 12-> 1-> 0 revnum = revnum + base * (Num % 10 ); base * = 10; return revnum ;}}

Http://www.geeksforgeeks.org/write-a-c-program-to-reverse-digits-of-a-number/




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.