Thread: Trailing Zeroes

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Trailing Zeroes


    Is there anyway I can edit my program so that the output has no trailing zeroes?

    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
         int n;
         double a,b;
         scanf("%d", &n);
         while(n--)
         {
              scanf("%lf %lf", &a,&b);
              printf("%.4lf\n",pow((b*a)/(b+a),2));
         }
         return 0;
    }
    Example:

    2.3452 ---> 2.3452
    3.500 ---> 3.5
    3.4530 --->3.453
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Code:
              printf("%.4g\n",pow((b*a)/(b+a),2));
    Results in
    Code:
    $ ./a.out 
    4
    1 1
    0.25
    2 2
    1
    3 3
    2.25
    4 4
    4
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Thanks for the reply Salem :) Unfortunately I don't get the desired results when I input values for 'a' and 'b' that are distinct. I was thinking about converting the floating value into a string and determining up to what values I should print. Any other suggestions?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    I'm thinking you should post an example terminal session like I did, and then comment exactly on what you would like to see, as opposed to merely saying "that didn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo