+ Reply to Thread
Results 1 to 2 of 2

Thread: reverse digits of a number

  1. #1
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321

    reverse digits of a number

    Write a function to reverse a number by eliminating duplicates in it?
    Ex:
    input : 2452
    output : 254

  2. #2
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321
    The idea is to have an array of size 10 (having all digits from 0-9) which keeps a note of whether any digit has its duplicate.
    Code:
    int Reverse(int num)
    {
    	int mod=0, ret=0;
    	int arr[10]={0};
    	while(num) {
    		mod=num%10;
    		if(arr[mod]==0) {
    			arr[mod]=1;
    			ret=ret*10+mod;
    		}
    		num=num/10;
    	}
    	return ret;
    }

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 25th July 2010, 18:58
  2. Reverse K nodes of a List
    By TopGun in forum Algorithm/Data Structure Questions
    Replies: 3
    Last Post: 23rd April 2010, 00:35
  3. Reverse each individual words in a sentence
    By TopGun in forum Microsoft
    Replies: 2
    Last Post: 8th January 2009, 21:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts