• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
August 13, 2024 |1.1K Views

Write a C Program to Reverse a Number

  Share  1 Like
Description
Discussion

In this video, we will write a C program on how to reverse a number.

Examples:
Input N= 54321
Output n= 12345

Here, we reverse a number using two different methods:
1. Using While loop
2. Using the recursion function

Using While loop:

Step 1: Take the number’s modulo by 10.
Step 2: Multiply the reverse number by 10 and add the modulo value to the reverse number.
Step 3: Divide the number by 10.
Step 4: Repeat the above steps until the number becomes zero.

Using the recursion method:
The recursion method allows you to repeat an instruction or statement a set number of times.

Step 1: If the number becomes zero then terminate the recursion, this will be the base condition.
Step 2: Take the modulo and add it with the ‘rev*10’ multiplying.
Step 3: Divide the number by 10 and call the reverse function on this number after updating it to number/10.

Apart from that, we will see the time and space complexity of both the methods, i.e
Time complexity = O(log(N))
Space complexity = O(log(N))
 
C Program to reverse the digits of a number using recursion
https://www.geeksforgeeks.org/c-program-to-reverse-the-digits-of-a-number-using-recursion/