0
Can any one help me with this code ?
I want to calculate the inverse of integer For example x=781 Inverse which is nbr in my code case =187
8 Answers
0
#include <stdio.h>
#include <math.h>
int main() {int x,r,nbr,cpt;
do {scanf("%d", &x);} while(x>0);
cpt=0;
while(x!=0){
x=x/10;
cpt=cpt+1;
}
nbr=0;
printf("donner autre");
do {scanf("%d", &x);} while(x>0);
while(x!=0){
r=x%10;
nbr=nbr+r*pow(10,cpt-1);
x=x/10;
}
printf(nbr);
return 0;
}
0
This my code
0
x>0 will accept 0 or negative numbers into x.
So for positive numbers must
do{...} while(x<0);
By your code you need to enter input 2times and same number. Why?
You can copy original..
With your logic it goes like this..
234 =>
cpt=3
4*100=400
400+ 3*100=400+300=700
700+2*100=700+200=900 incorrect..
//cpt,? no need to count number of digits..
It need only..
234=>
4
4*10+3 =43
43*10 +2 =432
Hope it helps you...
Edit: ask me if you don't understand anything in this...
0
No after that it goes with 400+30
Because cpt will be equal to 1
0
You are not updating cpt value anywhere... So it dont change its value...
For that you need to add cpt--; or cpt=cpt-1;
Rami Joy
add c--;
printf("%d", nbr); //printf(nbr); is wrong
0
I did all the changes and still don't work
0
Can u please write the correct form of my code
0
//this is your's corrected code
#include <stdio.h>
#include <math.h>
int main() {int x,r,nbr,cpt;
do {scanf("%d", &x);} while(x<0); //instead taking again this input 2nd time, copy it into another variable int y=x;
cpt=0;
while(x!=0){
x=x/10;
cpt=cpt+1;
}
nbr=0;
printf("donner autre");
do {scanf("%d", &x);} while(x<0);
//you can remove above statement use y next
while(x!=0){
r=x%10;
nbr=nbr+r*pow(10,cpt-1);
cpt--; //you missing this
x=x/10;
}
printf("%d",nbr); //corrected form
return 0;
}
Edit: Rami Joy
// this is my explanation version
#include <stdio.h>
int main() {
int x,r,nbr=0;
scanf("%d",&x);
printf("donner autre");
while(x!=0){
r=x%10;
nbr=nbr*10+r;
x=x/10;
}
printf("%d",nbr);
return 0;
}