please check the coding for me..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yana
    New Member
    • Mar 2007
    • 12

    please check the coding for me..

    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.I ncludes 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.

    Code:
    #include <iostream> 
    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.
    Last edited by Ganon11; Mar 14 '07, 02:04 PM. Reason: code tags added, re-indented
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • yana
      New Member
      • Mar 2007
      • 12

      #3
      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??
      Last edited by yana; Mar 14 '07, 03:05 PM. Reason: add more

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by yana
        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?

        Comment

        • yana
          New Member
          • Mar 2007
          • 12

          #5
          what should i do to get the output?
          it must have an output, isn't it?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            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.

            Comment

            • yana
              New Member
              • Mar 2007
              • 12

              #7
              Originally posted by Banfa
              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..

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                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.

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  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.

                  Comment

                  Working...