By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,420 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,420 IT Pros & Developers. It's quick & easy.

please check the coding for me..

P: 12
Question...

An approximate value of pi can be calculated usong the series given below:

Pi=4[1-1/3+1/5-1/7+1/9+((-1)n)/(2n+1)]


((-1)n)=n is a power for-1(for your information)

Write a C++ program to calculate the approximate value of pi using this series.The program takes an input n that determines the number of terms in the approximation of value of pi and outputs the approximation.Includes a loop that allows the users to repeat this calculation for new values n until the user says she or he wants to end the program.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. using namespace std;
  3.  
  4. void prime_num(int);
  5. int main()
  6. {
  7.  int retry;
  8.  
  9.    do
  10.    {  
  11.       int x = 100;
  12.       prime_num(x);
  13.    }
  14.  
  15. void prime_num( int n)
  16. {
  17.    bool Prime=true;
  18.  
  19.    for ( int k = 3; k <= n; k++)
  20.    {
  21.       for ( int j = 2; j <= n; j++)
  22.       {
  23.          if ( k!=j &&k % j == 0 )
  24.          {
  25.             Prime=false;
  26.             break; // stop looping
  27.          }
  28.       }
  29.       if (Prime)
  30.       {                    
  31.          cout << " do you want to repeat the process?(Yes/No)"\n";
  32.          cin >> retry;
  33.       } while ( retry='Y'|| retry='y')
  34.  
  35.       cout << "  Prime: " << k <<  ""; 
  36.    }
  37.    Prime = true; 
  38. }
  39.  
  40. }
this coding cannot give me an output n have many error.
Mar 14 '07 #1
Share this Question
Share on Google+
8 Replies


Banfa
Expert Mod 5K+
P: 8,916
Interestingly you problem seems to be about calculating PI but the could you have posted appears to be code to find prime numbers.

May be you would like to

1. Read the Posting Guidelines particularly the part about posting coursework.

2. Post the code actually related to the problem, or post the problem actually related to the code.
Mar 14 '07 #2

P: 12
i'm sorry
the coding is for prime question
but there is no output

this is the question for the prime coding

Write a program that finds and prints all the numbers between 3 and 100.A prime number is a number such that one and itself are the only numbers that evenly divide it(eg.,3,5,7,11,13,17,.....)

one way to solve this problem is to use a doubly nested loop.The outer loop can iterate from 3 to 100 while the inner loop checks to see if the counter value for the outer loop is prime.one way to see if number n is prime is to loop from 2 to n-1 and if any of these numbers evenly divides n,then n cannot be prime.if none of the value from 2 to n-1 evenly divide n,then n must be prime.

can you help me??
I'm sorry to disturb you.

why the program no output?
is it false??
Mar 14 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
i'm sorry
the coding is for prime question
but there is no output

this is the question for the prime coding

Write a program that finds and prints all the numbers between 3 and 100.A prime number is a number such that one and itself are the only numbers that evenly divide it(eg.,3,5,7,11,13,17,.....)

one way to solve this problem is to use a doubly nested loop.The outer loop can iterate from 3 to 100 while the inner loop checks to see if the counter value for the outer loop is prime.one way to see if number n is prime is to loop from 2 to n-1 and if any of these numbers evenly divides n,then n cannot be prime.if none of the value from 2 to n-1 evenly divide n,then n must be prime.

can you help me??
I'm sorry to disturb you.

why the program no output?
is it false??
There is no outoput at all?
Mar 14 '07 #4

P: 12
what should i do to get the output?
it must have an output, isn't it?
Mar 14 '07 #5

Banfa
Expert Mod 5K+
P: 8,916
Your posted code does not compile because you have defined the function prime_num inside the function main and C/C++ does not support nested function definitions.

You need to begin by moving prime_num outside main.
Mar 14 '07 #6

P: 12
Your posted code does not compile because you have defined the function prime_num inside the function main and C/C++ does not support nested function definitions.

You need to begin by moving prime_num outside main.
I move the prime_num already but it still cannot get the output.
There is no output at all.Please help me..
Mar 14 '07 #7

Banfa
Expert Mod 5K+
P: 8,916
You code is such that your assertation that you have fixed the problem with location of prime_num in the posted code does not enable us to tell what the resulting code is as there were several other errors.
Mar 14 '07 #8

Ganon11
Expert 2.5K+
P: 3,652
As Banfa said, there are countless errors in the posted code. Not only is prime_num defined inside main, but it is between the end bracket and the while () of your first loop. Also, the prime_num function is not finished - after you close the second if() statement, you continue with the while(). Rearrange the code to make sense, finish the main() and prime_num() functions, and then see if you are still getting bad output.
Mar 14 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.