C program to print reverse of a number
We can reverse a number in c using loop and arithmetic operators. In today’s program, we are getting numbers as input from the user and reversing that number through some lines of code.
#include <stdio.h> #include<conio.h> void main() { int num, rev = 0, rem; clrscr(); printf("\n\t\t\t...Welcome To EduNews.Tech... "); printf("\n\nEnter an integer: "); scanf("%d", &num); while (num != 0) { rem = num % 10; rev = rev * 10 + rem; num /= 10; } printf("Reversed number = %d", rev); printf("\n\n\n\n\t\t\tThankyou for Joining Us !"); printf("\n\t\t\t!Regards EduNews !"); getch(); }
Program Output:
Program Explanation:
- In this program, we take input from the user. Then the while loop is used until num != 0 is false (0).
- In each iteration of the loop, the remainder when num is divided by 10 is calculated and the value of num is reduced by 10 times.
- Inside the loop, the reversed number is computed using:
rev = rev*10 + rem;
I hope this post helps you to understand the Reverse of a number and its implementation in C programming language.
Keep coding 🙂
Nice, easy to use, and efficient code