446,420 Members | 1,133 Online 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 #include   using namespace std;   void prime_num(int); int main() {  int retry;      do    {         int x = 100;       prime_num(x);    }   void prime_num( int n) {    bool Prime=true;      for ( int k = 3; k <= n; k++)    {       for ( int j = 2; j <= n; j++)       {          if ( k!=j &&k % j == 0 )          {             Prime=false;             break; // stop looping          }       }       if (Prime)       {                              cout << " do you want to repeat the process?(Yes/No)"\n";          cin >> retry;       } while ( retry='Y'|| retry='y')         cout << "  Prime: " << k <<  "";     }    Prime = true;  }   } this coding cannot give me an output n have many error. Mar 14 '07 #1
8 Replies

 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

 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

 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

 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

 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 