Need help in writing function in C.

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Need help in writing function in C.

1. Ronin Guest

## 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.

2. Danny Kalev Guest

## 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.

3. Yap Ee Huey Guest

## 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.

4. bababa Guest

## 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.

5. Andy Guest

## 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.

6. marc Guest

## 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...;)

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise