C Programming and Data Structures (41 Blogs)

How To Reverse Number In C?

Last updated on Jul 19,2019 200 Views
How To Reverse Number In C?

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

While learning C you will learn all the fundamental concepts of the programming realm. To learn these fundamental concepts there are some standard programs which when implemented give us a brief understanding of all the concepts in a practical way. C program to reverse a number is one of them which gives the learner a deep understanding of While loop and the Arithmetic operators.

Following Pointers will be covered in this article

Let’s Reverse a Number in C

C Program To Reverse A Number: Understanding the Algorithm

First, let’s take a look at the Algorithm, after that we will consider an example and dive deeper into the topic. Before starting with the interesting part, what is the first thing that comes to your mind when you think of reversing a number? Maybe, just flip the position of digits or some other complicated way. Let’s see if it works the way we think.

So, let’s take a look at the Algorithm first.

  • First, we consider a variable ‘Number’ consisting of the number to be reversed after that we will declare a variable named ‘Reverse_Number’.
  •  We will be using the following formula, Reverse_Number = Reverse_Number*10 + Number%10 since initially Reverse_Number is 0 and ‘Number’ consists the number to be reversed. The value of Reverse_Number will be equal to the remainder we get after dividing ‘Number’ by 10.
  • After applying this formula we will divide ‘Number’ by 10 and update it with the value we received after division. It should look something like this, Number = Number / 10.
  • We will repeat steps 2 and 3 until the updated value of ‘Number’ variable is > 0.
  • At the end variable ‘Reverse_Number’ will consist of our reversed number

Sample

Let’s move to the second part and consider an example 87342. We will use the algorithm discussed above to obtain the reverse of 87342

  • Let’s consider ‘Number and ‘Reverse_Number’ to store the original and reversed numbers.
  • Reverse_Number = 0 + 2,  i.e Reverse_Number = 2 and Number = 87342 / 10 which is equal to 8734
  • As we discussed earlier we need to repeat the process until ‘Number’ > 0. Hence, Reverse_Number = 20 + 4, therefore Reverse_Number = 24 and after dividing the Number variable by 10 we get the new ‘Number’ value as 873
  • Reverse_Number = 243 and Number = 87
  • Reverse_Number = 2437 Number = 8
  • Reverse_Number = 24378 Number = 0
  • This is how we obtained our reversed number.

Implementing the Program using While Loop

We discussed the algorithm to find the reverse of a number. We also manually evaluated the results by considering an example. Now, let’s write a program that can help us understand some key programming concepts and save us some time.


#include <stdio.h>
#include <conio.h>
/* The Function we defined to reverse the given number*/
int ReverseTheNumber(int Number)
{
int Reverse_Number = 0;
while(Number > 0) /* loops untill the number is > 0*/
{
Reverse_Number = Reverse_Number*10 + Number%10;
Number = Number/10;
}
return Reverse_Number; /*Returning our reversed number*/
}
int main()
{
int Number;
printf("Enter the number to be reversed:n");
scanf("%d", &Number);
printf("Reverse of no. is %d", ReverseTheNumber(Number)); /*Using the Function*/
getchar();
return 0;
}

Output

The output we received is the same as the result we obtained from our manual implementation. Use the above code to achieve a similar result in fewer lines of code.

With this we come to the end of this blog on ‘C Program To Reverse A Number’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of  this blog and we will get back to you.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.