In this section you will learn how to reverse a numbers in c using loop. We will use three variable for reverse the numbers. Just simple mathematic operation we will perform to reverse the number. In this program we are taking the input from user.
Reverse mean shifting the first value to last value and shifting the last value to first value.
Let’s suppose a user will input a number ‘386’, then we will perform reverse operation on it.
#include<stdio.h> #include<conio.h> //REVERSE OF A NUMBER WITH LOOP void main() { int n, temp, rev = 0; clrscr(); printf("ENTER A NUMBER TO REVERSE:- "); scanf("%d",&n); temp = n; while(n != 0) { rev = rev *10; rev = rev + n%10; n = n/10; } printf("REVERSE OF %d IS:- %d",temp,rev); getch(); }
ENTER A NUMBER TO REVERSE:- 386 REVERSE OF 465 IS:- 683