# Reversing the order of the digits?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-07-2003
Mak
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
• 10-07-2003
quzah
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.
• 10-07-2003
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.
• 10-07-2003
Mak
Quote:

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);```
• 10-07-2003
quzah
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.
• 10-07-2003
R.Stiltskin
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; }```
• 10-07-2003
Mak
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.
• 10-07-2003
Mak
BTW thanks for you help. I just started taking C at University. So i'm going to need a lot of help. :D

I did not like Java that much but looking back at it now Java looked a lot simpler.
• 10-07-2003
Zainny
R.Stiltskin,

Code:

` scanf("%s",&oldstr);`
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.
• 10-07-2003
R.Stiltskin
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.
:)
• 10-07-2003
Zainny
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.
• 10-07-2003
Mak
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
• 10-07-2003
Zainny
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.
• 10-08-2003
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.

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.
• 10-08-2003
quzah
Quote:

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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last