Pages

When Programming is Life

Sunday, 8 February 2015

Reverse a Number in C Programming

Reverse a Number in C Programming
Reverse a Number in C Programming


Hello guys, welcome back to Standard Programming. Today I am gonna share here how to reverse a given number in C programming. This is a very basic and common problem in every C Programming Book. Whether you are asked to reverse a five digit number or more than that, using this code you can reverse any number of any length. So let's get started !

Reversing of a number in C programming can be done by many methods. You can reverse a number by using While Loop, For Loop or even using Recursion.The first two methods are more or less same. Recursion method is little bit different,you have already guessed that I think. In each case we will first see the code then will examine each line for explanation.

Reverse a Number using While Loop


/*Reverse a Number using While Loop
C Programming Code
by ©Anuran Barman.2015.All Rights Reserved.*/

#include <stdio.h>
#include <conio.h>

int main()
{
   int n, reverse = 0;

   printf("Enter a number to reverse\n");
   scanf("%d", &n);

   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n       = n/10;
   }

   printf("Reverse of entered number is = %d\n", reverse);

   getch();
   return 0;
}

here we declare two variables. One to contain the input number and second one to contain the reversed number. Then we run a While Loop until n !=0. In the loop, we store the reverse number in the reverse variable and each time we divide the number with 10 to cut the last digit of it. 

Example: Let's take 123 as example. Here first 123 is the number.The while loop will continue till n !=0. At the first step of while statement reverse is 0 as 0 multiplied by something is always zero (we initialized reverse variable to zero). In the second step, reverse = 0+3. 3 comes from 123%10. After that we make the number 123 to 12 by dividing it by 10. In next loop the number is 12 this time. In the previous step reverse was 3, so now reverse will be 3*10=30 and after that adding the remainder it will be 30+12%10=32. Now reverse is 32 and n is 1 (n /=10=>12/10=1).In the next step 1 will be added to 320 which will make the reverse 321. Voila ! We have successfully reversed the number. 



Reverse a Number using For Loop

/*Reverse a Number using For Loop
C Programming Code
by ©Anuran Barman.2015.All Rights Reserved.*/

#include<stdio.h>
#include<conio.h>
int main(){
    int num,r,reverse=0;

    printf("Enter any number: ");
    scanf("%d",&num);

    for(;num!=0;num=num/10){
         r=num%10;
         reverse=reverse*10+r;
    }

    printf("Reversed of number: %d",reverse);

    getch();
    return 0;
}


as you can see the For Loop logic for reversing the number is same. The only difference between those is the for expression which is self explanatory. If you have problem with that you can ask me in comments.


Reverse a Number using Recusrion

/*Reverse a Number using Recursion
C Programming Code
by ©Anuran Barman.2015.All Rights Reserved.*/

#include<stdio.h>
#include<conio.h>
long reverse(long);

int main()
{
   long n, r;

   scanf("%ld", &n);

   r = reverse(n);

   printf("%ld\n", r);

   getch();
   return 0;
}

long reverse(long n) {
   static long r = 0;

   if (n == 0)
      return 0;

   r = r * 10;
   r = r + n % 10;
   reverse(n/10);
   return r;
}


as you can see in Recursion technique the logic is still same. Here we make a recursion function where we do the same thing, find the reminder,add with another variable after multiplying the later by 10. After than we do a recursion call for n/10.



That's it Guys.These are most common used methods to reverse a number in C programming. If you have a better algorithm to reverse a number in C programming you can share that with me in the comment section below. Thanks for visiting Standard Programming.
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

1 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
Posts RSSComments RSSBack to top
© 2011 Standard Programming ∙ Designed by BlogThietKe
Released under Creative Commons 3.0 CC BY-NC 3.0