Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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
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.
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
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.