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. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    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. #2
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Hmmm I remember seeing this question some time last week. Perhaps doing a forum search would benefit you.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    it was me
    i ask about the operation of flipping
    i solved it but this is the main problem

  4. #4
    Banned
    Join Date
    Dec 2008
    Posts
    49
    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. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need to use only integers

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Maybe you need to SAVE the original number, as I'm sure after the loop, i is always zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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("%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. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Using too many variables and loops won't get you anywhere. Here's how it will look like in pseudo-code.
    Code:
    n = 0;
    read i
    while (i is not equal to zero) 
    {
         n = 10 * n + i % 10
         i /= 10
    }
    print n

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by matsp View Post
    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. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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("%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;
    }
    Last edited by transgalactic2; 12-13-2008 at 12:40 AM.

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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;
    }
    Last edited by transgalactic2; 12-13-2008 at 02:34 AM.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'll take a look at it. Shouldn't be tough, but you never know.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing the Indentation draft
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-23-2008, 10:17 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21