can anyone help me to invert c++ code to c code....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • benjaminwan
    New Member
    • Nov 2009
    • 1

    can anyone help me to invert c++ code to c code....

    Code:
    #include<stdio.h>
    int is_prime(int x);
    int main(){
        char again;
        int x = 17;
        int count = 0;
        again = 'y';
    
        cout << "The prime numbers less than or equal to "<< x <<" are:" << endl;
                        for(int i=2 ; i<=x ; i++){
                                if(is_prime(i)){
                                cout << "\t\t" <<i << endl;
                                count ++;}
                                }
                        cout << "The total count of prime numbers is: "<< count<<endl << endl << endl;      
    
        while (again == 'y' || again == 'Y')
        {     
              cout << "Enter a 3-digit positive integer: ";
              cin >> x;
              int count = 0 ;
              for(int i=2; i<=x ; i++){
                                if(is_prime(i)){
                                cout << "\t\t" <<i << endl;
                                count++;}
                                }
                        cout << "The total count of prime numbers is: "<< count <<endl << endl << endl;      
              cout << "Would you like enter the number again? (y/n) " ;
              cin >> again;
              cout << endl;
    
              if(again == 'n' || again =='N')
               cout << "<<<<< Program Completed >>>>>" << endl;
              }  
     return 0;   
    }
    
    
    int is_prime(int x){
         if (x==2)
            return 1;
         for (int i = 2; i < x ; i++){
             if(x%i==0)
                 return 0;}
         return 1;
         }
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Glancing through your code, the only things that I see that will need changing are the cin and cout statements.

    For the C alternatives, see printf() (cout) and scanf() (cin).

    Mark.

    P.S. Please see the posting guidelines on how to ask a question, specifically the part on using [CODE] tags.

    Comment

    • spiralfire
      New Member
      • Nov 2009
      • 8

      #3
      Originally posted by Markus
      Glancing through your code, the only things that I see that will need changing are the cin and cout statements.

      For the C alternatives, see printf() (cout) and scanf() (cin).

      Mark.

      P.S. Please see the posting guidelines on how to ask a question, specifically the part on using [CODE] tags.
      Yep, in c there is no cin and cout, so use printf and scanf.

      But theres something else:
      * On pure C variables declarations are the first lines of each function.
      * this statement:
      Code:
      for(int i=2 ; i<=x ; i++){
      isn't allowed in C, you can't declare a variable in the for statement, in the beginning of the function as stated before.

      Comment

      • JonathanS
        New Member
        • Jan 2008
        • 37

        #4
        C99 allows that type of declaration:

        it depends on with what standard of C you are looking to comply.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          That's true JonathonS but most C compilers, even if the are C99 compliant*, still default to C89 and so wont support this unless they have been specifically configured to.


          * Or at least as compliant as possible since there are no fully C99 compliant compilers yet.

          Comment

          Working...