# Thread: Switching 2nd and 4th digit in a double(before comma)

1. ## Switching 2nd and 4th digit in a double(before comma)

The goal of the program is to switch the 2nd and 4th digit in a double(before comma). Lets say you have 28492.45, it should become 29482.45. The program is working fine. I want to add another option, where the user input is a two digit number(before comma) like 56.78, the 3th and 4th digit should be considered as 5600.78. The switched number should be then 5006. Same goes for a 3 digit number. How can i implement this?

Code:
```int main() {

double number;

scanf("%lf", &number);

//printf("%.4f", number);

char arr[sizeof(number)];
snprintf(arr, sizeof(number) + 1, "%f", number);

char aux = arr[1];
arr[1] = arr[3];
arr[3] = aux;

number = atof(arr);
printf("%.4f\n", number);

return 0;
}```

2. Perhaps keep multiplying number by 10 until it's greater than 1000.

3. scanf("%lf", &number);
The l modifier has no effect on f. It is permitted but does nothing. If you wish to read a long double, you need an uppercase l (i.e. "%Lf").

However, the variable number is a double. "%f" reads doubles, not floats as its name may imply. There is no identifier for floats because floating-point values are always read as double and then converted to float if that is the type of the variable to store the input.

So, in your code, regardless of number being a float or double, you just need "%f". And you would need "%Lf" is the variable was long double.

How can i implement this?
If the transformation is always from 2 to 4 digits in the integer portion, 56 transforms into 5006 by means of a modulus operation, followed by an integer division, followed by a addition.

Code:
```int number = 56;

remainder = number % 10; // returns 6
manifold = number / 10 * 100   // note that this is NOT the same as number * 10. You know why?
final = manifold + remainder```
The formula is thus: n / 10 * 100 + n % 10, for any any int n < 100

Then you just need to add the decimal portion. I'll leave to you the task of separating the integer portion from the decimal portion and storing both for use. Then see if you can apply the same principle to a 3 digit integer, 4 digit, etc. and what needs to change. Show some code and we can work from there.

4. Originally Posted by Mario F.
The l modifier has no effect on f. It is permitted but does nothing. If you wish to read a long double, you need an uppercase l (i.e. "%Lf").

However, the variable number is a double. "%f" reads doubles, not floats as its name may imply. There is no identifier for floats because floating-point values are always read as double and then converted to float if that is the type of the variable to store the input.

So, in your code, regardless of number being a float or double, you just need "%f". And you would need "%Lf" is the variable was long double.
You're thinking of printf, where floats are converted to double for printing. But with scanf the %lf format is meaningful (and necessary).

5. Not sure how I made that mistake. Lack of experience for sure. Thanks for the correction.

6. Thanks for helping me out!