fonction qui inverse un nombre
Bonjour,
je dois ecrire une fonction qui inverse un nombre par exemple 12345 doit devenir 54321
j'ai fais un code mais là, je bloque. Le soucis se trouve avec le j, il devrait representer le plus grand i mais là, ... je sais pas
ca marche pour un nombre de 3 mais pas plus
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <iostream>
using namespace std;
#include <stdlib.h>
#include <math.h>
int f(int a);
int s,y, a,j, i,n ;
int x;
int f(int a)
{cin>>a;s=0;
while(a>=10){
x= a/10;
i=1;
while (x >=10)
{
x=x/10;
i++;
j=i;
}
cout<<"i ="<<i<<endl;
cout<<"j ="<<j<<endl;
y= x*int(pow(10,j-i));
a= (a- double (x* pow(10,i)));
cout<<"nouveau a "<<a<<endl;
cout<<"x "<<x<<endl;
cout<<"y ="<<y<<endl;
s=s+y ;
cout<<"s "<<s<<endl;
}x=a;
cout<<"a"<<a<<endl;
cout<<"x "<<x<<endl;
cout<<"j ="<<j<<endl;
double p=double (pow(10,j)); cout<<"p"<<p<<endl;
double e=( x*p);cout<<"e"<<e<<endl;
s=s+e;
return s;
}
int main (void)
{
cout<<"sol = "<<f(a)<<endl;
system("pause");
return 0;
} |