I have created a code in C language to find total number of digits beyond the decimal point for a long double number and this is the code -
#include <stdio.h>
int main(void)
{
long double number,temp; int num,count=0;
printf("\nEnter a number=");
scanf("%Lf",&number);
printf("\nEnter multiplication factor=");
scanf("%d",&num);
if(num>10)
{
printf("\nPlease enter a multiplicative factor <10");
return 1;
}
if(number>0)
{
for(int i=0;i<num;i++)
{
number=number*10;
temp=number-(int)number;
printf("\ntemp=%Lf\n",temp);
if(temp!=0)
{
count=count+1;
}
else
{
return 1;
}
}
printf("\nTotal number of decimal points=%d",++count);
}
else if (number<0)
{
number=(-1)*number;
for(int i=0;i<num;i++)
{
number=number*10;
temp=number-(int)number;
printf("\ntemp=%Lf\n",temp);
if(temp!=0)
{
count=count+1;
}
else
{
return 1;
}
}
printf("\nTotal number of decimal points=%d",++count);
}
else
{
printf("\nNumber has no decimal points\n");
}
}
Where I have used the logic which could be understood by the example -
If number=45.123
and multiplication factor=5
then number=45.123*10=451.23 and temp= 451.23-451=0.23
=451.23*10=4512.3 and temp= 4512.3-4512=0.3
=4512.3*10=45123 and temp= 45123.0-45123=0.0
And this is where the program should have terminated because I have used return 1 but it's not working this way as the program is multiplying number with 10 as many times multiplicative factor has been used.
Here, is the output -
aalpanigrahi@aalpanigrahi-HP-Pavilion-g4-Notebook-PC:~/Desktop/Daily programs$ ./decimal
Enter a number=45.123
Enter multiplication factor=5
temp=0.230000
temp=0.300000
temp=0.000000
temp=0.000000
temp=0.000000
Total number of decimal points=6