-3

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 
  • Better put `\n` at end of `printf` format strings (or use `fflush`). Compile with all warnings & debug info: `gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/). Then **use the debugger** `gdb`. Try to get some [MCVE](http://stackoverflow.com/help/mcve) in your question. A *fix-my-code* question is off-topic on SO – Basile Starynkevitch Oct 10 '17 at 11:56
  • 2
    Have you tried to step through the code, line by line, in a debugger? Perhaps you should take some time to read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert, and learn how to use a debugger. – Some programmer dude Oct 10 '17 at 11:56
  • *but it's not working this way* Well, in what way **is** it working? – Andrew Henle Oct 10 '17 at 12:03
  • What is the multiplication factor for? – mrCarnivore Oct 10 '17 at 12:07
  • 2
    Most likely `45.123*1000` is not exactly equal to `45123`. – aschepler Oct 10 '17 at 12:09

1 Answers1

-2

Sorry I misunderstood your code at first. The problem is that you are comparing a float with an int using !=. That you should never do. Try comparing with

if(temp!=0.0f)

Even better would be comparison with a very small number:

if (temp<0.00000001f)

like this:

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.0000001f)
      {
         count=count+1;
      }
      else
      {   
         return 1;
      }
  }
  printf("\nTotal number of decimal points=%d",++count);

}

mrCarnivore
  • 4,638
  • 2
  • 12
  • 29