# help in fixing this code..

This is a discussion on help in fixing this code.. within the C Programming forums, part of the General Programming Boards category; i need to write a code that flips a number and sums it with the previous form of the number,till ...

1. ## help in fixing this code..

i need to write a code that flips a number and sums it with the previous form of the number,till i get a number that if i will flip it
i will get the same number
i need to output the number of steps which took the program to transform the input number into the number that if we flip it ,it will remain the same.
for example if i input

679 + 976 = 1655
1655 + 5561 = 7216
7216 + 6127 = 13343
13343 + 34331 = 47674
4 steps

i wrote the code which flips i put a loop over it but its not working
??
Code:
```int main(){
int sum=0;
int counter=0;
int i;
int n = 0;
scanf("%d",&i);
counter=0;

while(n!=i) {//start while

while(i)
{
n = 10 * n + i % 10;
i /= 10;
}
n=i+n;

counter++;
}//end while
printf("%d %d",n,counter);
return 0;
}```

2. Hmmm I remember seeing this question some time last week. Perhaps doing a forum search would benefit you.

3. it was me
i solved it but this is the main problem

4. Why not just take the number as a string, then flip the string? I think its computationally less complex since theoretically you have it as a string to begin with.

5. i need to use only integers

6. Maybe you need to SAVE the original number, as I'm sure after the loop, i is always zero.

--
Mats

7. Thats a small problem.

my main problem is making this process of summing and flipping like i showed in the example.
i tried to build this proccess in this code but its not working.
Code:
```int main(){
int sum=0;
int counter=0;
int i;
int n = 0;
scanf("&#37;d",&i);
counter=0;

while(n!=i) {//start while

while(i)
{
n = 10 * n + i % 10;
i /= 10;
}
n=i+n;

counter++;
}//end while
printf("%d %d",n,counter);
return 0;
}```

8. A small problem that you haven't fixed, as it looks. Do you want me to repeat it: "the variable i is zero after your inner while-loop".

--
Mats

9. Using too many variables and loops won't get you anywhere. Here's how it will look like in pseudo-code.
Code:
```n = 0;
while (i is not equal to zero)
{
n = 10 * n + i % 10
i /= 10
}
print n```

10. Originally Posted by matsp
A small problem that you haven't fixed, as it looks. Do you want me to repeat it: "the variable i is zero after your inner while-loop".

--
Mats
I need to reset the value of variable "i" to its original value(the one i entered the scanf)
??

Theoreticly i want to flip each time the sum of the number and its flipped form
that why i did
i=i+n;

But the original value of "i" needs to be changes each time by sum
so i cant put each time the scanf value in "i".

?

11. No, you need to SAVE the value before your invert it, and then use the saved value after the loop [which may be roughly the same as what you said, but it's not the value you ENTERED that you need to save, but the previous n from the sum - or the initial value].

You obviously also need to check if the reversed value is equal to the current value, and if so, you are done. Currently, you check it after you have added the values together, which won't work - you'll go one step too far [if you are lucky, it will still get a number that is the same backwards - but not always].

--
Mats

12. i changed the code like you said
still not working
??
Code:
```int main(){
int sum=0;
int counter=0;
int i;
int n = 0;
scanf("&#37;d",&i);
counter=0;

do {//start while
while(i)
{
n = 10 * n + i % 10;
i /= 10;
}

if (n!=i){
n=i+n;
counter++;
}

}while(n!=i);//end while
printf("%d %d",n,counter);
return 0;
}```

13. I tried it with for loop

not working
??

Code:
```#include <stdio.h>

int main(){

int num;  //a number
int f_num = 0;  //flipped number
scanf("&#37;d",&num);

for(f_num=0;f_num!=num;f_num=f_num+num){

while(num)
{
f_num = 10 *f_num + num % 10;
num /= 10;
}
}
printf("%d",f_num);
return 0;
}```

14. ok i understand that i need to keep the num value.
I tried to do it here.Its not working.

Code:
```#include <stdio.h>

int main(){
int count=0;
int num;  //a number
int temp;
int f_num = 0;  //flipped number
scanf("&#37;d",&num);

do{
if ((num!=f_num)&&(count>0)){
num=num+f_num;
temp=num;
printf("num=%d f_num=%d temp=%d count=%d\n",num,f_num,temp,count);
}
temp=num;
while(num)
{
f_num = 10 *f_num + num % 10;
num /= 10;
}
count++;
printf("num=%d f_num=%d temp=%d count=%d\n",num,f_num,temp,count);
num=temp;
printf("num=%d f_num=%d temp=%d count=%d\n",num,f_num,temp,count);
}while (num!=f_num);
printf("%d",f_num);
return 0;
}```

15. I'll take a look at it. Shouldn't be tough, but you never know.

Page 1 of 2 12 Last