Homework question i cant figure out (in C)

Discussion in 'Programming' started by whiteblue1942, Mar 10, 2009.

  1. #1
    I need to write a simple function in C for homework, and this is my question....

    Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367.

    If anyone can help me i would greatly appreciate it, thanks!:D
     
    whiteblue1942, Mar 10, 2009 IP
  2. whiteblue1942

    whiteblue1942 Peon

    Messages:
    573
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    nvm i got it
     
    whiteblue1942, Mar 10, 2009 IP
  3. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #3
    
    void reverse(int i) {
    if (i) {
    printf("%d",i);
    reverse(i/10);
    }
    }
    
    Code (markup):
     
    it career, Mar 11, 2009 IP
  4. shubhamjain1

    shubhamjain1 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Don't Confuse him with recursion.!!! :D
     
    shubhamjain1, Mar 11, 2009 IP
  5. rakesh_kpn

    rakesh_kpn Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    #include <stdio.h>
    int reverse(int digit);
    int main()
    {
    printf("%d",reverse(7643));
    return 0;
    }
    int reverse(int digit)
    {
    int reversedigit = 0;

    while(digit)
    {
    reversedigit += digit%10;
    reversedigit*= 10;//10
    digit /=10;
    }
    reversedigit /= 10
    return reversedigit;
    }
     
    rakesh_kpn, Mar 11, 2009 IP