odd digits

Hello. I have a "long" tipe number. How can I interchange the biggest odd digit with the smallest odd digit? Example: 34921 becomes 34129
You'd have to decompose the number using %10 or sprintf(), then find the max and min, swap them, and finally recompose the new number.
Thanks for your answer helios, but i don't know how to recompose the number.
I will give you a hint ;)

321%10=1
321/10=32 (we're using integers here)
Until now I have this:
#include<iostream.h>
void main()
{
long n, cop
int i, max, min, man;
cout<<"n=";cin>>n;
for(i=9;i>=1;i-=2)
{
cop=n;
while(cop)
{
if(cop%10==i)
{
max=i;
i=0;
cop=0;
}
cop=cop/10;
}
}
for(i=1;i<=9;i+=2)
{
cop=n;
while(cop)
{
if(cop%10==i)
{
min=i;
i=10;
cop=0;
}
cop=cop/10;
}
}
man=max;
max=min;
min=max;



I decomposed the number, found the min and the max, swap them, but I don't know how to recompose the number as helios told me
Last edited on
How to recompose?
Well, let's say you have the numbers 3, 9, 0, 5, 1.
0*10=0
0+3=3
3*10=30
30+9=39
39*10=390
390+0=390
390*10=3900
3900+5=3905
3905*10=39050
39050+1=39051
Last edited on
I think the easiest way is to remember the amount of numbers. In the example of helios you would get:
3 E4+
9 E3+
0 E2+
5 E1+
1 E0
=39051
Topic archived. No new replies allowed.