C Programming Example to Reverse a Number

Algorithm


  1. Take number n.
  2. Initial rev = 0
  3. Find Reminder = n % 10
  4. rev = rev * 10 + remainder
  5. n = n/10
  6. Continue step 3-5 until number is not equal zero.

 

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <stdio.h>

int main() {
    int n, rev = 0, remainder;
    
    printf("Enter a number: ");
    scanf("%d", &n);
    
    while (n != 0) {
        remainder = n % 10;
        rev = rev * 10 + remainder;
        n /= 10;
    }
    
    printf("Reversed number = %d", rev);
    
    return 0;
}.
Copy The Code & Try With Live Editor

Input

x
+
cmd
Enter a number: 123

Output

x
+
cmd
Reversed number = 321

Demonstration


C Programming Example to Reverse a Number.

Get more related example of reverse number is palindrome number - https://devsenv.com/codes/palindrome-number-solution-in-c-programming-coding-example-many-examples-in-c-for-palindrome-check

 

Practice Online of C programming - https://devsenv.com/page/run-c-programming