Thread: Float data type issue

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    20

    Float data type issue

    Float data type outputs the data with 6 decimal points(like 5.00000) even the number doesn't have any decimal points.Why is that so?Can it just output the necessary decimals?



    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    
    main()
    
    {
    	
    	
    	float a,b,c,x;
    	printf("Use this to work out the length of the hypotenuse on a right angled triangle.\n");
    	printf("Please enter a:");
    	scanf("%f",&a);
    	printf("\nPlease enter b:");
    	scanf("%f",&b);
    	
    	// a*a+b*b=c*c;
    	
    	x=a*a+b*b;
    	c=sqrt(x);
    	
    	printf("\nThe triangle's hypotenuse has a length of %f ",c);
    	
    	getch();
    	
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Use %g instead of %f.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    20
    That's cool.Thanks,tabstop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  2. Matrix operations using objects
    By circuitbreaker in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2010, 08:53 AM
  3. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  4. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  5. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM