hi.
is there anyone who can write a full program that reverse a float number in C language?
I want to use from following commands:
1. if/for/while loops
2. I dont want use from any Function.please write a code with basic commands.
I wrote a code,but it's not true for some numbers.
for example : 12.001
I want to reverse a float number that is entered by user in the input.
float number = integer section . decimal section
for example if you entered 13.546 in the input, you have to receive 645.31 in the output.
my code work well for many float numbers,but in some cases (such as when decimal section include several zeros) dont work and decimal section doesnt reverse truly.
how remove extra digits in decimal section in a float number that appear in C language?
for example when you enter 12.001 in the input,C language receive 12.0010001.
Due to the way a floating point number is stored, it is difficult to rearrange the bits to reverse it. I would suggest thge best method is to convert it to a character string, reverse that and then convert it back to a float.
It has been a while since I last used C language. I have tried to do it but it is hard because of the zeros in the decimal part. I am using Visual Studio 2010 and in that, the number 12.001 is stored as 12.001000 (they add number of zeros to have 6 digits in the decimal part).
I think your problem could be solved by solving another problem first which is to find out how many digits (integer and decimal) have the number introduced by the user and then is easy to solve.
Other solution might be by cheating. Let me explain what I mean with "cheating".
I mean that you know how your program work when scan a float number. I will explain it with an example using my programme and then try to apply this to your:
So my programme adds the necesarry number of zeros to have 6 digits on the decimal part.
If I introduce 12.1, they store it like 12.100000. If I introduce 12.12345 is stored 12.123450. If I introduce 12.123456 is stored like 12.123456 (no more zeros... this is the clue).
So the cheating means understand how the program stores the float number.
Now that you know that, because you don't know how many digits the user will introduce, just compare the decimal part of the number introduced by the user and some decimal parts...
This could be done by multiplying the decimal part by 10^(6) and get every digit of it and start comparing one by one.
Yes... is not the "engineering solution" but it might work... With float numbers is difficult because in real life a float number have infinite zeros on the decimal part.. so you must know how your program store the number.