Interview Question
Software DevelopersCountry: India
Interview Type: In-Person
int _tmain(int argc, _TCHAR* argv[])
{
double f = 53.456;
int temp1 = f;
int count1 = 0;
int count2 = 0;
while (temp1 > 0)
{
temp1 = temp1 / 10;
count1++;
}
int temp2 = f;
while ((double)temp2 != f) {
f = f * 10;
temp2 = f;
count2++;
}
printf("Integer Count = %d, Decimal count = %d", count1, count2);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
double f = 53.456;
int temp1 = f;
int count1 = 0;
int count2 = 0;
while (temp1 > 0)
{
temp1 = temp1 / 10;
count1++;
}
int temp2 = f;
while ((double)temp2 != f) {
f = f * 10;
temp2 = f;
count2++;
}
printf("Integer Count = %d, Decimal count = %d", count1, count2);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
double f = 53.456;
int temp1 = f;
int count1 = 0;
int count2 = 0;
while (temp1 > 0)
{
temp1 = temp1 / 10;
count1++;
}
int temp2 = f;
while ((double)temp2 != f) {
f = f * 10;
temp2 = f;
count2++;
}
printf("Integer Count = %d, Decimal count = %d", count1, count2);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
double f = 53.456;
int temp1 = f;
int count1 = 0;
int count2 = 0;
while (temp1 > 0)
{
temp1 = temp1 / 10;
count1++;
}
int temp2 = f;
while ((double)temp2 != f) {
f = f * 10;
temp2 = f;
count2++;
}
printf("Integer Count = %d, Decimal count = %d", count1, count2);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
double f = 53.456;
int temp1 = f;
int count1 = 0;
int count2 = 0;
while (temp1 > 0)
{
temp1 = temp1 / 10;
count1++;
}
int temp2 = f;
while ((double)temp2 != f) {
f = f * 10;
temp2 = f;
count2++;
}
printf("Integer Count = %d, Decimal count = %d", count1, count2);
return 0;
}
c# implementation.
double data type does not work due to loss of precision while rounding values. Use decimal instead.
static private int[] Get( decimal num ) {
int[] res = { 1/*before*/, 0/*after*/ };
var tmpNum = num / 10;
while ( tmpNum > 1 ) { res[ 0 ]++; tmpNum /= 10; }
tmpNum = num * 10;
while ( tmpNum % 10 != 0 ) { res[ 1 ]++; tmpNum *= 10; }
return res;
}
public void countDecimals(double number){
double dupNumber=number;
int i=0;
while(dupNumber>1){
if(dupNumber>1)
i++;
dupNumber=dupNumber/10;
}
System.out.println("Decimals before:"+i);
long temp=(long)number;
BigDecimal difference=BigDecimal.valueOf(number).subtract(BigDecimal.valueOf(temp));
System.out.println("Decimals after:"+difference.precision());
}
public void countDecimals(double number){
double dupNumber=number;
int i=0;
while(dupNumber>1){
if(dupNumber>1)
i++;
dupNumber=dupNumber/10;
}
System.out.println("Decimals before:"+i);
long temp=(long)number;
BigDecimal difference=BigDecimal.valueOf(number).subtract(BigDecimal.valueOf(temp));
System.out.println("Decimals after:"+difference.precision());
}
- Chidigam December 29, 2015