Decimal question

This is a discussion on Decimal question within the C Programming forums, part of the General Programming Boards category; Hi I just started learning c yesterday and I wrote this simple c program to find the area of a ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    5

    Decimal question

    Hi I just started learning c yesterday and I wrote this simple c program to find the area of a circle when you input the radius.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int radius, area;
    
    int main()
    {
      printf( "Enter radius (i.e. 10): " );
      scanf( "%d", &radius );
      area = (int) (3.14159 * radius * radius);
      printf( "\n\nArea = %d\n", area );
                                  
      system("PAUSE");	
      return 0;
    }
    The problem is that it displays the radius as a whole number rounded down. Is there a way to make it display decimals, or better yet is there a way to make it round up or down depending upon the number? ex. 19.4=19, 19.5=20.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Your area variable is an int. Make it a float or double if you want decimals. Get rid of the int typecast in your calculation too. You'll have to change printf() to use %f instead of %d as well.
    If you understand what you're doing, you're not learning anything.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    For rounding Ints (ints round down always) add .5 before you cast the answer as an int.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    integers have no decimal.
    It also doesn't exactly round down. It does something called truncate the variable. Also, global variables (variables outside of main) can get tricky especially if you don't need them (it's hard to keep track) You can do this
    Code:
    #include <stdio.h>
    
    int main(void){
         int radius;
         float area;
         printf( "Enter radius (i.e. 10): " );
         fflush(stdout); /*need to make sure this is actually displayed*/
         scanf( "%d", &radius ); /*remember this is only integers*/
         area =  (3.14159 * radius * radius);
         printf( "\n\nArea = %.2f\n", area );
         system("PAUSE");	
         return 0;
    }
    If you user inputs a float for radius ex: 2.3 it will truncate it to 2 because it is an integer. the %.2f only displays 2 places after the decimal.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    5
    Cool! It displays decimals now. Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 PM
  2. Replacing a Decimal with a letter in a numeric variable.
    By Dobermin in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2003, 01:38 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21