# Need help in writing function in C.

• 01-25-2001, 12:05 AM
Ronin
Need help in writing function in C.

Hello,

I need to know the code in C to write a function.
"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.
• 01-25-2001, 06:49 AM
Danny Kalev
Re: Need help in writing function in C.
we usually don't do homework here so I'll only give a few hints: ask the
user to provide a number, convert the input number to a string, and then
reverse that string. display the result.

Danny

Ronin wrote:
>
> Hello,
>
> I need to know the code in C to write a function.
> "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.

• 01-25-2001, 10:18 PM
Yap Ee Huey
Re: Need help in writing function in C.

I would say using mod 10, get the last digit
and then divide the input by 10,continues to
mod and divide until zero.
Then, you reverse the order of the integers
and return the value.

bababa

>Ronin wrote:
>>
>> Hello,
>>
>> I need to know the code in C to write a function.
>> "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.

• 01-25-2001, 10:19 PM
bababa
Re: Need help in writing function in C.

I would say using mod 10, get the last digit
and then divide the input by 10,continues to
mod and divide until zero.
Then, you reverse the order of the integers
and return the value.

bababa

>Ronin wrote:
>>
>> Hello,
>>
>> I need to know the code in C to write a function.
>> "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.

• 01-26-2001, 05:45 PM
Andy
Re: Need help in writing function in C.

"Ronin" <ronin_21_2000@hotmail.com> wrote:
>
>Hello,
>
>I need to know the code in C to write a function.
>"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.

Convert it to a string.

Use standard string manipulation functions to reverse the string
If you are using MFC CString::MakeReverse will work nicely.

Convert it back to an integer using atoi or something similar.

• 01-29-2001, 01:52 PM
marc
Re: Need help in writing function in C.

"Andy" <Andy@Charter.net> wrote:
>
>"Ronin" <ronin_21_2000@hotmail.com> wrote:
>>
>>Hello,
>>
>>I need to know the code in C to write a function.
>>"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.

>
>Convert it to a string.
>
>Use standard string manipulation functions to reverse the string
>If you are using MFC CString::MakeReverse will work nicely.
>
>Convert it back to an integer using atoi or something similar.
>
>

Try:

int i, CurNum, NewNum ;
for( i = CurNum, NewNum = 0 ; i > 0 ; i /= 10 )
{ NewNum = ( NewNum * 10 ) + i % 10 ; }

It works...;)