In this blog, we will discuss the C program to reverse a number in an iterative and recursive manner. We will see the algorithms and code implementation accordingly, If you are a programming beginner, this article will be for you. We will solve this problem in different ways.

```
Example:
Input: 6789
Output: 9876
```

## Algorithm to C program to reverse a number

- Take the input from the user
- Perform remainder = number % 10
- Print remainder
- Perform number/10
- Repeat steps 2 and 4 until the input number cannot be further divided.

### Method 1: An iterative method to reverse a number in C

In this method, we will use a while loop to reverse a number.

### Code Implementation to reverse a number in C by iterative method

#include <stdio.h> int main() { int number, rev = 0, remainder; printf("\nEnter a number: "); scanf("%d",&number); printf("\nReversed Number: "); while(number != 0) { remainder = number%10; rev = rev*10 + remainder; number /= 10; } printf("%d",rev); return 0; }

```
Output:
Enter a number: 6789
Reversed number: 9876
```

**Time Complexity of reverse a number in C Language:** O(log(N))

### Method 2: Recursive program to reverse a number in C

In this method, we will use a recursive function to apply the reverse number in which the function calls itself from its definition part.

### Algorithm to C program to reverse a number recursively

- Create a reverse(int n), a recursive function of void type.
- The base condition will be: if (n <10), then print(n) and return.
- Otherwise, print(n%10) and call function reverse(n/10).

### Code Implementation to reverse a number in C recursively

#include <stdio.h> int reverse_digits(int num) { static int rev_num = 0; static int base_pos = 1; if(num > 0) { reverse_digits(num/10); rev_num += (num%10)*base_pos; base_pos *= 10; } return rev_num; } int main() { int num; printf("\nEnter a number: "); scanf("%d",&num); printf("\nReversed number: %d",reverse_digits(num)); return 0; }

```
Output:
Enter a number: 6789
Reversed number: 9876
```

**Conclusion**

In this article, we have discussed the most efficient and effective way for C program to reverse a number. we have discussed two methods with their respective algorithms and time complexities. We hope this blog will help you to clear your fundamentals. Also, you can practice more questions on our MYCODE platform. These questions are created by our experts so we hope this article will build your logic-building skills and enhance your programming skills.

**Other C Programs**

C program to calculate percentage of 5 subjects

C program to convert binary number to decimal number

C program to convert celsius to fahrenheit

C program to convert infix to postfix

C program to find area of circle

C program to find roots of quadratic equation

C program to add two numbers

C program for merge sort for linked lists

C program for performing bubble sort on linked list

C program to reverse a linked list