Thread: help in fixing this code..

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I tried to trace it.
    and it works on the first loop run.
    then on the second loop it give a number which its not supposed.

    http://img372.imageshack.us/img372/8783/96751135or0.gif

    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 03:34 AM.

  2. #17
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    I don't even know where to start to try and help you with your code. Try this:

    Code:
    #include <stdio.h>
    
    int flip (int num);
    
    int main (void) {
      int i, j, c = 0;
    
      fprintf(stdout, "\nEnter the number to flip: ");
      fscanf(stdin, "%d", &i);
    
      while (i != flip(i)) {
        j = flip(i);
        fprintf(stdout, "%d + %d = %d\n", i, j, i+j);
        c++;
        i += j;
      }
    
      fprintf(stdout, "Completed in %d steps.\n", c);
      return 0;
    }
    
    int flip (int num) {
      int digit, result = 0;
    
      while (num != 0) {
        digit = num % 10;
        result = result * 10 + digit;
        num /= 10;
      }
    
      return result;
    }

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,866
    Using the original posted code as the template:


    Code:
    /*flip (reverse) the number entered and add it to the last tally. Count the 
    steps needed (and numbers), until you reach a sum that is the reverse
    of itself:
    
    eg: 125 entered: 125+521=646, 
    646 == it's reverse, so only 1 step was needed.
    
    679 + 976 = 1655
    1655 + 5561 = 7216
    7216 + 6127 = 13343
    13343 + 34331 = 47674
    4 steps
    */
    
    #include <stdio.h>
    
    int a[10] = { 0 };
    
    int main()  {
       int count=0, i, j, bad;
       unsigned long int temp, sum;
       unsigned long int num ;       
       unsigned long int f_num = 0;  //flipped number
       printf("\n\n\n Enter a Number: ");
       scanf("&#37;U",&num);
       
       do{
          num=num+f_num;
          f_num = 0;
          temp=num;
          while(temp)  {                    
             f_num = 10 *f_num + temp % 10;
             temp /= 10;
          }
          count++;
          printf("\n num=%lu f_num=%lu count=%d",num,f_num,count);
    
          //flip the sum into array a[]
          sum = num + f_num;
          temp = sum;
          for(i = 0; i < 10; i++)
             a[i] = 0;
          i = 0;
          while(temp)  {
             a[i] = 10 * a[i] + temp % 10;
             i++;
             temp /= 10;
          }
          i -= 1;
          for(j = 0, bad = 0;j < i; j++, i--)  {
             if(a[j] != a[i]) {
                bad = 1;
                break;
             }
          }
       } while (bad);
    
       printf("  sum: %lu", sum);
    
       getch();
       return 0;
    }
    Last edited by Adak; 12-13-2008 at 05:26 AM.

  4. #19
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant use a function call
    so i need to this all in the main function

    i tried to change it
    but its not working
    Code:
    #include <stdio.h>
    
    
    
    int main (void) {
      int i, j, c = 0;
    int digit, result = 0;
      printf(  "\nEnter the number to flip: ");
       scanf( "&#37;d", &i);
    
      while (i != result) {
    
    
    
    
    
      while (i != 0) {
        digit = i % 10;
        result = result * 10 + digit;
        i /= 10;
      }
    
        j=result;
         printf("%d + %d = %d\n", i, j, i+j);
        c++;
        i += j;
      }
    
      printf( "Completed in %d steps.\n", c);
      return 0;
    }

  5. #20
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by Adak View Post
    Using the original posted code as the template:


    Code:
    /*flip (reverse) the number entered and add it to the last tally. Count the 
    steps needed (and numbers), until you reach a sum that is the reverse
    of itself:
    
    eg: 125 entered: 125+521=646, 
    646 == it's reverse, so only 1 step was needed.
    
    679 + 976 = 1655
    1655 + 5561 = 7216
    7216 + 6127 = 13343
    13343 + 34331 = 47674
    4 steps
    */
    
    #include <stdio.h>
    
    int a[10] = { 0 };
    
    int main()  {
       int count=0, i, j, bad;
       unsigned long int temp, sum;
       unsigned long int num ;       
       unsigned long int f_num = 0;  //flipped number
       printf("\n\n\n Enter a Number: ");
       scanf("%U",&num);
       
       do{
          num=num+f_num;
          f_num = 0;
          temp=num;
          while(temp)  {                    
             f_num = 10 *f_num + temp % 10;
             temp /= 10;
          }
          count++;
          printf("\n num=%lu f_num=%lu count=%d",num,f_num,count);
    
          //flip the sum into array a[]
          sum = num + f_num;
          temp = sum;
          for(i = 0; i < 10; i++)
             a[i] = 0;
          i = 0;
          while(temp)  {
             a[i] = 10 * a[i] + temp % 10;
             i++;
             temp /= 10;
          }
          i -= 1;
          for(j = 0, bad = 0;j < i; j++, i--)  {
             if(a[j] != a[i]) {
                bad = 1;
                break;
             }
          }
       } while (bad);
    
       printf("  sum: %lu", sum);
    
       getch();
       return 0;
    }
    i cant use array
    and getch()

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is the working code but i need to replace the function calls

    Code:
    #include <stdio.h>
    
    
    
    int main (void) {
      int i, j, c = 0;
    int digit, result = 0;
      printf(  "\nEnter the number to flip: ");
       scanf( "&#37;d", &i);
    
      while (i != result) {
    
    
    
    
    
      while (i != 0) {
        digit = i % 10;
        result = result * 10 + digit;
        i /= 10;
      }
    
        j=result;
         printf("%d + %d = %d\n", i, j, i+j);
        c++;
        i += j;
      }
    
      printf( "Completed in %d steps.\n", c);
      return 0;
    }
    Code:
    #include <stdio.h>
    
    int flip (int num);
    
    int main (void) {
      int i, j, c,num = 0;
    int digit, result = 0;
    
      printf(  "\nEnter the number to flip: ");
       scanf( "%d", &num);
    
      while (num != 0) {
        digit = num % 10;
        result = result * 10 + digit;
        num /= 10;
      }
    
    
      while (i != result) {
    
            while (num != 0) {
        digit = num % 10;
        result = result * 10 + digit;
        num /= 10;
      }
    
        j = result;
    
         printf("%d + %d = %d\n", i, j, i+j);
        c++;
        i += j;
      }
    
      printf( "Completed in %d steps.\n", c);
      return 0;
    }

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, 11: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, 06:05 PM