Thread: Program to reverse a number. small error with it..

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    Question Program to reverse a number. small error with it..

    hi! heres my program:

    #include <stdio.h>
    #include <math.h>
    #define NEWLINE '\n'

    int main(void){
    int num;
    int reverse(int n);

    printf("Enter a positve integer: \n");
    scanf("%i \n", &num);

    printf("The reverse number is: %i \n", reverse(num));

    return 0;
    }

    int reverse(int n)
    {
    int rev_num = 0, k = 0,i =2, mult = 1;
    int rev_digit, digitcount;
    int digits(int r);

    digitcount = digits(n);

    while((k <= digitcount) && (digitcount != 0))
    {
    rev_digit = n % 10;

    k++;
    mult = pow(10, (digitcount - k));

    rev_num = rev_num + (rev_digit * mult);
    n = n / 10;
    }
    return rev_num;
    }

    int digits(int r){
    int count = 0;

    while(r != 0)
    {
    r = r/10;
    count++;
    }

    return count;
    }


    Output:
    Enter a postive number:
    8002033 /* for example */
    2
    /* this is where the problem occurs. after i enter 8002033, the program ask for another number, it doesnt matter what i enter, i just need to enter something */

    The reverse is 3302008
    /* this part seems correct */

    So can anyone help me? i dont know why they need me to enter another number after 8002033!!

  2. #2
    Registered User
    Join Date
    Nov 2004
    Posts
    3
    sorry heres the tagged code, i missed the top post

    Code:
    #include <stdio.h>
    #include <math.h>
    #define NEWLINE '\n'
    
    int main(void){
    	int num;
    	int reverse(int n);
    
    	printf("Enter a positive integer: \n");
    	scanf("%i \n", &num); 
    
    	printf("The reverse number is: %i \n", reverse(num));
    	return 0;
    
    }
    
    int reverse(int n){
    	int rev_num = 0, k = 0,i =2, mult = 1, rev_digit, digitcount;
                    int digits(int r);
     
                    digitcount = digits(n);
    
    	while((k <= digitcount) && (digitcount != 0))
                    {
                         rev_digit = n % 10;
    	     k++;
    	     mult = pow(10, (digitcount - k));
    	     rev_num = rev_num + (rev_digit * mult);
                         n = n / 10;	    
    	}
                    return rev_num;            	
    }
    
    int digits(int r){
    	int count = 0;
    
    	while(r != 0)
                    {
    	      r  = r/10;
    	      count++;
                    }
    	return count;
    }

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    scanf("%i \n", &num);
    Try
    Code:
    scanf("%i", &num);

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    3
    YES!!! it worked!!
    thank you!!!
    you are brilliant!!!
    thank you!!!
    thank you!!!
    thank you!!!

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Assuming that you're using unsigned int number as input and ASCII,

    Code:
    void
    print_reverse_number(const unsigned int number)
    {
            const unsigned int      quotient = number / 10;
            const unsigned int      remainder = number % 10;
    
    
            putchar(remainder + '0');
    
            if (quotient != 0)
                    print_reverse_number(quotient);
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    sprintf( buf, "%d", num );
    for( x = strlen( buf ) -1; x > -1; x-- )
        putchar( buf[x] );
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Bah quzah you forgot to check to see if its negative first

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There you go thinking negative again...

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Always

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  4. problem with my prime number program
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-12-2002, 12:30 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM