PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] How can I get rid of zeros, if decimal is entered it prints decimal, but if whole number is entered it prints .00?

 August 11, 2022     c, decimal     No comments   

Issue

This program prints overlapping of two intervals. But the problem is, if I enter, for example numbers : 1.1 -1.1 1.1 1.1, it prints out the whole number. I've tried writing %1.1f in last printf command, but it turned out to be even worse, because then, if I enter 1 2 1 1, it prints out 1.0 and 4.0. How can I get the proper print if I enter decimale or int?

#include <math.h>

int main() {
    float a,b,c,x,derivative;
    printf("Input coefficients a, b i c: ");
    scanf("%f %f %f",&a,&b,&c);
    if((a<(-10)) || (a>10) || (b<(-10)) || (b>10) || (c<(-10)) || (c>10)){
        printf("Coefficients a, b i c do not belong in range of (-10,10).");
        return 1;
    }
    printf("Input point x: ");
    scanf("%f",&x);
    derivative=(2*a*x)+b+(c*0);
    printf("First derivation in point x=%.f je %.f.",x,derivative);
    return 0;
}


Solution

You can use the "%g" format specifier to display floating-point numbers in the shortest possible way:

printf("First derivation in point x=%g je %g",x,derivative);


Answered By - Adrian Mole
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing