To reverse a given number

Here is the program to mathematically reverse the entered integer. The program uses simple library functions, and an easy flow.

Logic :  The main idea here is to trace the entered number till its length, and slicing up in each of the iterations. The program asks the user to enter the number to reverse. Sets two variable flags to hold each digits and the final reversed number.
The while loop slices down the given number to digits, and appends to the reversed flag ‘rev’. Finally it prints out the reversed number after exiting from the while loop.

The similar logic for slicing up the given number is used in other programs like, finding the sum of digits and, palindrome checking programs

void main()
long int num, rev = 0, dig;
printf(“\n\n\t ENTER A NUMBER…: “);
scanf(“%ld”, &num);
dig = num % 10;
rev = rev * 10 + dig;
num = num / 10;
printf(“\n\t REVERSED NUMBER IS…: %ld”, rev);
Download exe and source code here.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

3 thoughts on “To reverse a given number

  1. sir its very helpful but I need reverse of a number using call by value function pls if u can help me with this sir

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.