Thread: Circles Anyone?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    Circles Anyone?

    I'm trying to do this problem but am stuck once again.
    The problem basically states

    "Write a program that reads in the radius of a circle and prints the circle's diameter, circumference and area. use the constant value 3.14159 for pi. Perform each of these calculations inside the printf statements and use the conversion specifier %f." Computing in C 4th Edition.


    I am still on chapter 2 and we have not learn about conversion specifiers yet. I think it starts on the next chapter. But below is what I came up with. The only problem is, I did not include the %f in there because I do not know how to use it. I am not sure why the book is wanting us to use something that is taught later on in the chapters. But anyway, another problem I have is, my calculations seems to be off when I checked it with a calculator. Does anyone know whats wrong with the code?

    Code:
     #include <stdio.h>
    
    int main()
    {
           int r, pi;
           
           pi = 3.14159;
           
           
    
              printf( "Enter the radius of the circle: " );
              scanf( "%d", &r );
    
             
              printf( "The diameter is %d\n", 2 * r);
              printf( "The circumference is %d\n", pi * 2 * r);
              printf( "The area is %d\n",   pi * r * r );
    
    system ( "pause" );
    return 0;
    }
    Last edited by Nightsky; 09-03-2006 at 08:08 PM.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Change printf( "The circumference is %d\n", pi * 2 * r);
    to printf( "The circumference is %f\n", pi * 2 * r);

    Same for the area.

    Also, how far off are the calculations? Will you post some examples?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    Quote Originally Posted by twomers
    Change printf( "The circumference is %d\n", pi * 2 * r);
    to printf( "The circumference is %f\n", pi * 2 * r);

    Same for the area.

    Also, how far off are the calculations? Will you post some examples?

    When I changed %d to %f, the values went to zero. Am I suppose to define %f somewhere? I'm not familiar with the %f.


    As for the calculations when I had it at %d, it seems like the bigger the radius, the further off the area and circumference. For example, if I entered a radius of 3, it would give me diametere of 6, circumference of 18, and area of 27.

    But when I enter 48 as radius, I got diameter of 96, circumference of 288, and area of 6912.

    The actual values should be:

    Radius: 3 then Diameter: 6, Circumference: 18.85, Area: 28.27

    Radius: 48 then Diametere:96, Circumference: 301.59, Area: 7238.23

    I assuming it has something to do with the decimals in pi.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You might want to make pi a float, as an int it's 3.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Perhaps you should make pi a float?

    ie

    int r;
    float pi = 3.14.........;

    Edit:: Beaten to it

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    Thumbs up

    I got it. This board is very helpful. I learn so much more here as oppose to in school with the i drink at hooters teacher. lol. Anyway, Thanks!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The getting lost and walking in circles
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 05-07-2008, 09:53 AM
  2. Intersection between 2 circles
    By darkskylight in forum C++ Programming
    Replies: 3
    Last Post: 01-29-2006, 06:24 AM
  3. intersecting circles (trilateration)
    By X PaYnE X in forum Windows Programming
    Replies: 3
    Last Post: 02-13-2005, 10:31 AM
  4. How to check if two circles intersects each other?
    By Mikro in forum C++ Programming
    Replies: 16
    Last Post: 09-01-2004, 09:38 AM
  5. Array of circles
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-15-2003, 11:12 AM