Reversing the order of the digits?

This is a discussion on Reversing the order of the digits? within the C Programming forums, part of the General Programming Boards category; So far i have : Code: main(){ int num; double s; int i=0; printf("Enter 4 digit integer: "); scanf("%d",&num); s=sqrt(num); ...

  1. #1
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33

    Question Reversing the order of the digits?

    So far i have :
    Code:
    main(){
    int num;
    double s;
     int i=0;
    
    
    printf("Enter 4 digit integer: ");
    scanf("%d",&num);
    
    s=sqrt(num);
    
    printf("Square root of %d is: %6.4f \n", num, s);
    but then i have to reverse num and print the square root of that reversed number. I'm lost. I know that I have to use char or array but i'm not even sure how to start.

    Anybody can point me in the right direction?

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't even need an array, you can, but you don't need one. Give yourself an example to work from, and analize how you'd do it. Here is the general idea:
    Code:
    number = 12345;
    holder = 0;
    
    while number is greater than zero
        holder equals holder times ten
        holder equals holder plus number mod ten
        number equals number divided by ten
    Drawn out:

    holder = 0 * 10
    holder = (0*10) + 5
    number = 1234
    holder = 50
    holder = 50 + 4
    number = 123
    ...
    holder = 54320 + 1
    number = 0

    You can use an array if you feel like it. It's more involved. sprintf the number to an array, and use the same multiplication formulae to increase 'holder' as you work your way backwards through the array.

    Or, sprintf the number to an array, reverse the string, and use atoi on it.

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

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Or, you can scan it in as a string (character array), use strlen() to find out the actual length, and then just traverse the string one element at a time, copying the characters (digits) one at a time in reverse order to a new string.

    Then use atoi() to convert the strings into integers to send to the sqrt() function.

    strlen() is in <string.h>
    atoi() is in <stdlib.h>

    Look 'em up.

  4. #4
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    Originally posted by R.Stiltskin
    Or, you can scan it in as a string (character array), use strlen() to find out the actual length, and then just traverse the string one element at a time, copying the characters (digits) one at a time in reverse order to a new string.

    Then use atoi() to convert the strings into integers to send to the sqrt() function.

    strlen() is in <string.h>
    atoi() is in <stdlib.h>

    Look 'em up.
    I alredy know the lengh...it is always 4

    all of that makes sense except traverse the string part...

    to convert it to a charracter array:
    Code:
    char array_char[4]; 
    sprintf(array_char,"%d",num);

    but how would i traverse it?

    I know after it would be easy..

    Code:
    int num2;
    double ss;
    num2 = atoi (reversed_char);
    printf("Reversed number is:  %d \n", num2);
    ss=sqrt(num2);
    printf("Square root of %d is: %6.4f \n", num2, ss);

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First off, the number would actually have to be five. Because you're using your array as a string, you need to include space for the terminating null character. Thus, you have to add one extra cell for the size.

    Next, do you know how to use a loop? If not, you can do it without, but it's just more lines of code.

    If you do, then simply access each element in the array using a loop. I'll show you the general idea without, and you can apply it to the 'with':
    Code:
    char swapper;
    char array[5] = "1234";
    
    swapper = array[0];
    array[0] = array[3];
    array[3] = swapper;
    There, you have successfully swapped two digits in your string.

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

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Here's a very simple example, inputting the number as a string to begin with. Of course, you can also do it your way, inputting it as an integer & then converting to a string. Either way the traversal would be similar. Note that the array length has to be 5 characters, terminating with the null character '\0'.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
                                                                                    
    int main(){
            char oldstr[5], newstr[5];
            int i;
            printf("Enter a 4-digit number > ");
            scanf("%s",&oldstr);
            printf("The number is %s.\n",oldstr);
            for(i=0;i<4;i++){
                    newstr[i] = oldstr[3-i];
                    newstr[4] = '\0';
            }
            printf("The new number is %s.\n",newstr);
                                                                                    
    return 0;
    }

  7. #7
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    yay...i got it working

    Code:
    #include <stdio.h>
    
                                                                                    
     main(){
            char digit[5];
    		char reversdigit[5];
            int i;
    		double s;
    		double s2;
    		int num;
    		int reversnum;
            printf("Please enter a 4 integer : ");
            scanf("%s",&digit);
            printf("The number is %s.\n",digit);
           
    		num= atoi(digit);
    		s=sqrt(num);
    
    	printf("The square root of %s is: %6.4f \n", digit, s);
    		 
    		  
    		    for(i=0;i<4;i++){
                    reversdigit[i] = digit[3-i];
                    reversdigit[4] = '\0';
            }
            printf("The number in reverse is %s.\n",reversdigit);
    		
    			reversnum= atoi(reversdigit);
    		s2=sqrt(reversnum);
    
    	printf("The square root of %s is: %6.4f \n", reversdigit, s2);
                                                                                    
    
    }
    it works but i still get an error
    "warning: type mismatch in implicit declaration for built-in function `sqrt' " I understand why... converting int into double but it still works so i guess its okay.

  8. #8
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    BTW thanks for you help. I just started taking C at University. So i'm going to need a lot of help.


    I did not like Java that much but looking back at it now Java looked a lot simpler.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    R.Stiltskin,

    Firstly, instead of
    Code:
     scanf("%s",&oldstr);
    it should read
    Code:
     scanf("%s", oldstr);
    Next, there is no need to continually set newstr[4] to '\0', this only needs to be done once at the end, hence it should be outside the loop.

    Regards,
    Zainny.
    Beware the fury of a patient man.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Mak:
    It works, but it complains because sqrt() wants a double as its argument. You can get rid of the error message by making the conversion explicit as in:

    s = sqrt((double)reversnum);

    This casts reversnum as a double before sending it to sqrt().


    Zainny:
    Right, & right.
    Haste makes waste.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    Mak,

    You should make the mentioned changed to your code.

    In addition, use main in the following manner:
    Code:
    int main() {
        /* code here */
    
        return 0;
    }
    Or even better, return EXIT_SUCCESS defined in stdlib.h

    The warning message you were getting was because you are not including the header in your code <math.h> in which the function prototype for sqrt resides. You will need to do this. In addition, if your compiler does not automatically link the math library for you, you may have to tell it to do this (How depends on your compiler and programming environment)

    Note also that sqrt() expects to receive a double argument.

    Cheers,
    Z.
    Beware the fury of a patient man.

  12. #12
    Mak
    Mak is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    oh nice...#include <math.h> did the trick


    Zainny, why do I have to use ??

    Code:
    int main() {
        /* code here */
    
        return 0;
    }
    even if it works without int and return 0; ?

    Do i need it there so later on i could call that program from another program? Even though i would not do that but is that the logic behind it?




    and here is the final program:

    Code:
    #include <stdio.h>
    #include <math.h>
    
                                                                                    
    int main(){
            char digit[5];
    		char reversdigit[5];
            int i;
    		double s;
    		double s2;
    		int num;
    		int reversnum;
            printf("Please enter a 4 integer : ");
            scanf("%s",digit);
            printf("The number is %s.\n",digit);
           
    		num= atoi(digit);
    		s = sqrt((double)num);
    
    	printf("The square root of %s is: %6.4f \n", digit, s);
    		 
    		  
    		    for(i=0;i<4;i++){
                    reversdigit[i] = digit[3-i];
                   
            }
    		 reversdigit[4] = '\0';
            printf("The number in reverse is %s.\n",reversdigit);
    		
    			reversnum= atoi(reversdigit);
    		s2 = sqrt((double)reversnum);;
    
    	printf("The square root of %s is: %6.4f \n", reversdigit, s2);
                                                                                    
    return 0;
    }
    thanks again

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    Mak,

    There are a couple of reasons. A good read is available at
    http://home.att.net/~jackklein/ctips01.html#int_main

    The following local FAQ version also provides some points:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Cheers,
    Z.
    Beware the fury of a patient man.

  14. #14
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    hmm, can't you just reverse 4 digits with 2 loops and arrays, one counting up, one counting down? I have been reading your codes and it looks so difficult.

    It could be also done this way I think:

    Code:
    #define N 4
    int a[N], i;
    
        printf("Enter %d numbers: ", N);
        for (i=0;i<N;i++)
           scanf("%d", &a[i]);
    
        printf("In reverse order: ");
        for (i=N-1;i>=0;i--)
           printf("%d", a[i]);
    It's much easier to understand.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by sjalesho
    hmm, can't you just reverse 4 digits with 2 loops and arrays, one counting up, one counting down? I have been reading your codes and it looks so difficult.
    That would just display them in reverse order. It wouldn't actually reverse the order.

    They could however use your method to create the reverse number. Count down, multiply the sum by ten, add the digit in that cell to it, repeat.

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

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

Similar Threads

  1. Reversing digits in C
    By Digital Thought in forum C Programming
    Replies: 22
    Last Post: 10-15-2010, 06:34 AM
  2. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 11:26 PM
  3. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 05:26 PM
  4. Reversing Digits Project (Help Please)
    By fenixataris182 in forum C++ Programming
    Replies: 9
    Last Post: 07-12-2005, 01:08 PM
  5. reversing digits
    By lizardking3 in forum C++ Programming
    Replies: 15
    Last Post: 03-28-2003, 11:30 AM

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