how to get the number of digits
-
how to get the number of digits after the decimal point ex : Input : 5.1234 Expected Output : 4 i wrote this code but there are wornings.. what is wrong here ?? double input; int x=input; // so x=the integeral part of input int y=input-x; // so y=the fractional part while(y>0) {count++; input*=10; x=input; y=input-x; } plz help me !! elmahdy ahmed elmahdy
-
how to get the number of digits after the decimal point ex : Input : 5.1234 Expected Output : 4 i wrote this code but there are wornings.. what is wrong here ?? double input; int x=input; // so x=the integeral part of input int y=input-x; // so y=the fractional part while(y>0) {count++; input*=10; x=input; y=input-x; } plz help me !! elmahdy ahmed elmahdy
First, this really sounds like homework. Not that such such posts are prohibited, but you may get a quicker response just reading ahead in your text book. Anyway, two hints: 1)
int
s won't hold the fractional bits of a number. 2) converting a number from one type to another, when the second type is not able to correctly represent all values possible in the first, will cause compiler warnings. :)Shog9
I returned and saw under the sun, that the race is not to the swift, nor the battle to the strong...
-
how to get the number of digits after the decimal point ex : Input : 5.1234 Expected Output : 4 i wrote this code but there are wornings.. what is wrong here ?? double input; int x=input; // so x=the integeral part of input int y=input-x; // so y=the fractional part while(y>0) {count++; input*=10; x=input; y=input-x; } plz help me !! elmahdy ahmed elmahdy
Given that 5.1234 is not going to represented exactly as such in memory (i.e., it's more than likely going to be something like 5.123999), you're going to have a hard time using your current code, assuming it even works. elmahdy wrote: int y=input-x; // so y=the fractional part The problem with this is that
5.1234 - 5 = 0.1234
in memory, but only the0
gets assigned to variabley
. Make sense? You could try converting the number to a string, but most of the conversion routines want to know how many digits to put after the decimal, which sort of defeats the purpose! -
how to get the number of digits after the decimal point ex : Input : 5.1234 Expected Output : 4 i wrote this code but there are wornings.. what is wrong here ?? double input; int x=input; // so x=the integeral part of input int y=input-x; // so y=the fractional part while(y>0) {count++; input*=10; x=input; y=input-x; } plz help me !! elmahdy ahmed elmahdy
Where is the value of input coming from? If you are getting it from the console, then use scanf with a string type specifier. Get the length of that string Start at the 0th char Increment the index until you find the '.' The number of decimal places is the string length - the current index (assuming you pre-increment)
-
how to get the number of digits after the decimal point ex : Input : 5.1234 Expected Output : 4 i wrote this code but there are wornings.. what is wrong here ?? double input; int x=input; // so x=the integeral part of input int y=input-x; // so y=the fractional part while(y>0) {count++; input*=10; x=input; y=input-x; } plz help me !! elmahdy ahmed elmahdy
Here is one solution, but it is fraught with problems, I will let you work out what they are!
#include <math.h> #include <stdio.h> int main(int argc, char* argv[]) { double dVal = 5.1234, dResult = 0.9, dInt = 0.0;; int nKount = 0; // due to rounding errors we can't check for zero, guess a value while(dResult > 0.00001) { dVal *= 10.0; dResult = modf(dVal, &dInt); nKount++; } printf("Value = %d\n", nKount); return 0; }
It is amazing what you can accomplish if you do not care who gets the credit. - Harry S Truman