#include<stdio.h>
#include<stdlib.h>
main()
{
      float num,a,b,c,d,e,f;
      do
      {
           scanf("%f",&num);
           getchar();
      }
      while (num>=0.75);
      a=0.0;
      b=0.0;
      c=0.0;
      d=0.0;
      e=0.0;
      f=0.0;
      if (num%2.0!==0)
      {
           a=num/2.0;
           num=num%2.0;
           if (num%1.0!=0)
           {
                b=num/1.0;
                num=num%1.0;           
                if (num%0.50!==0)
                {
                     c=num/0.50;
                     num=num%0.50; 
                     if (num%0.20!==0)
                     {
                          d=num/0.20;
                          num=num%0.20;
                          if (num%0.10!==0)
                          {
                              e=num/0.10;
                              num=num%0.10;
                              if (num%0.05!==0)
                              {
                                  f=num/0.05;
                              }
                          }
                     }
                }
           }
      }                                 
      change=a+b+c+d+e+f
      printf("the change :%f\n",resta);
      getchar();
}

what is wrong with my program?

Recommended Answers

All 2 Replies

exept printf("the change :%f\n",resta); ofc

Welcome ThePolid,

Unfortunately it's hard to tell what might be wrong with your program, since we don't know what your intention is. What is your program supposed to do? There's an appalling lack of documentation and intelligently-named variables. Please don't make us do this much work just to get started!

That said, your while() loop at line 11 does nothing. In fact, if num starts out >= 0.75, it will get stuck there forever. Try:

while (num >= 0.75)
{
    ...
}

Also, the modulo-operator ("%") gives "the remainder when dividing a by b". I'm not sure that's meaningful for floating-point numbers, and regardless, testing floating-point results for exact equality is dangerous, since many values which seem obvious enough (like 0.1, for instance) can't be exactly represented in binary, and computations tend to introduce tiny amounts of unexpected error.

Since the arbitrary numeric constants appear to reflect denominations of U.S. currency, I'd instead recommend converting the floating-point input to an integer number of cents: int cents = int(num * 100 + 0.5); (The addition of 0.5 provides rounding correction so that 0.99999997 gets converted to 100 cents, not 99 cents).

Then work with integers to do what you need.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.