My PI program

This is a discussion on My PI program within the C++ Programming forums, part of the General Programming Boards category; Somebody please help me with this. In the last 5 mins. or so, I've been trying to write a program ...

  1. #1
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32

    My PI program

    Somebody please help me with this. In the last 5 mins. or so, I've been trying to write a program that just shows digits in pi. I have the formula for it (1/2.545584412*8).

    Here is my code. Whenever I execute it, it just shows 575 and terminates.

    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>

    int main()
    {
    int pi;
    {
    pi==1/2.545584412*8;
    cout << pi;
    }
    return 0;
    }

    Thanks for any reply.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    pi==1/2.545584412*8;

    This should be


    pi=1/2.545584412*8;

    You want to assign pi to that value, not test for equality.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Originally posted by SilentStrike
    pi==1/2.545584412*8;

    This should be


    pi=1/2.545584412*8;

    You want to assign pi to that value, not test for equality.
    Now it says 3 and terminates. Why no decimal numbers? What am I missing?

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The problem is you defined pi as an int.

  5. #5
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>

    int main()
    {
    int pi;
    {
    cout << 1/2.545584412*8;
    system("pause");
    }
    return 0;
    }

    That is my new code. Now, it shows only 3.1427, how do I get it do display more digits? Also, when beside the equation, I take away the ;, and << /n;, the program does not run, and when it is <<"/n";, It does not work.

  6. #6
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    define pi as 'double' not 'int'
    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <conio.h> 
    
    int main() 
    { 
         double pi = 1/2.545584412*8; 
    
     
         cout << pi; 
         system("pause"); 
    
         return 0; 
    }
    the best things in life are simple.

  7. #7
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Same outcome. only 4 digits after .

  8. #8
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <conio.h> 
    #include <iomanip.h>
    
    int main() 
    { 
         
         setprecision(n);  // where 'n' is how many decimal places you want
    
         double pi = 1/2.545584412*8;      
         cout << pi; 
         system("pause"); 
    
         return 0; 
    }
    the best things in life are simple.

  9. #9
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Originally posted by xlnk
    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <conio.h> 
    #include <iomanip.h>
    
    int main() 
    { 
         
         setprecision(n);  // where 'n' is how many decimal places you want
    
         double pi = 1/2.545584412*8;      
         cout << pi; 
         system("pause"); 
    
         return 0; 
    }
    Still only 4 digits.

  10. #10
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <conio.h> 
    #include <iomanip.h>
    
    int main() 
    { 
         
         cout << setprecision(8);  
    
         double pi = 1/2.545584412*8;      
         cout << pi; 
         system("pause"); 
    
         return 0; 
    }
    sorry i forgot to tell you to put it in a cout statement.
    the best things in life are simple.

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    dammit, every time i clicked reply, someone had already answered the question!!..


  12. #12
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    what can i say, im fast
    Last edited by xlnk; 03-31-2002 at 02:49 PM.
    the best things in life are simple.

  13. #13
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #include <iomanip.h>

    int main()
    {
    int a;
    cout << setprecision(250);

    double pi = 1/2.545584412*8;
    cout << pi;
    cout << "\n";
    system("pause");
    return 0;
    }

    Why do only 50 digits or so come up?

  14. #14
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    the range for 'double' is 1.7e + / - 308 (15 digits)
    for 'long double its 1.2e + / - 4932 (19 digits)
    the best things in life are simple.

  15. #15
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Originally posted by xlnk
    the range for 'double' is 1.7e + / - 308 (15 digits)
    for 'long double its 1.2e + / - 4932 (19 digits)
    Changing from double to long double did not help.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  3. Program that calculates the value of PI
    By noodles in forum C Programming
    Replies: 7
    Last Post: 09-06-2006, 06:20 AM
  4. pi spigot program
    By eehiram in forum C Programming
    Replies: 10
    Last Post: 07-15-2006, 11:20 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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