C++ to C

This is a discussion on C++ to C within the C++ Programming forums, part of the General Programming Boards category; Want to convert these C++ to C?? (1) Code: #include <iostream.h> #include <iomanip.h> // Print out the first n Fibonacci ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    C++ to C

    Want to convert these C++ to C??

    (1)

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    
    // Print out the first n Fibonacci numbers. n is assumed to be >= 1.
    void fibo ( int n )
    {
        int current = 1;      // The value of the (i)th fibonacci number 
        int next    = 1;      // The value of the (i+1)th fibonacci number 
    
        cout << "\n\n\t  I \t Fibonacci(I) \n\t=====================" << endl;
        for ( int i = 1; i <= n; i++ ) {
    	cout << "\t" << setw(3) << i <<  "\t\t" 
                 << setw(4) << current << endl;
    	int next2 = current + next;
    	current = next;
    	next    = next2;
         }
    }
    
    int main ( void ) 
    {
        int n;        // The number of fibonacci numbers we will print 
    
        cout << "How many Fibonacci numbers do you want to compute? ";
        cin  >> n;
        if ( n <= 0 )
           cout << "The number should be positive." << endl;
        else 
           fibo ( n );
    
        return 0;
    }
    
    
    
    
    #include <iostream.h>
    
    /* Get a positive integer from user */
    int getPositive(void){
      int current;
    
      while( 1 ){
          cout << "Please enter a positive integer: ";
          cin >> current;
          if (current > 0) return current;
          cout << "It should be a positive integer";
      }
    }
    
    /* Compute GCD */
    int gcd (int dividend, int divisor)
    {
      int remaind;
    
      remaind = dividend % divisor;
      while (remaind != 0) {
           dividend	= divisor;
           divisor	= remaind;
           remaind	= dividend % divisor;
      }
      return divisor;
    }
    
    void main(void)
    {
      int dividend = getPositive();
      int divisor  = getPositive();
    
      cout << "The GCD of " << dividend << " and " << divisor << " is "
           <<  gcd(dividend, divisor) << endl;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > Want to convert these C++ to C??
    I don't, do you?
    How about trying it for yourself.

    Basically, replacing cout with printf() will get a lot of it done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    And declaring variables at the beginnig of each function.

Popular pages Recent additions subscribe to a feed

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