Nov 22, 2009

Free Source Codes For C++

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

Free Source Codes For C++

demolaynyc
Hey, just posting some exercises from Chapter 4 of the old book.

CODE

/* Chapter 4 Exercise 11a by Albert Villaroman 11-27-06 */

#include <iostream.h>

int main () {
    cout <<"Enter a non-negative integer: ";
        int u;
        cin >>u;
    while (u<0) {
        cout <<"Enter non-negative integer: ";
          cin >>u;
    }

    
    int potentialPrimes=0, loops=0;
    for (int f=2; f<u; f++) {
        if (u%f != 0) {
            potentialPrimes++;
        }
        loops++;
    }
    
    if (potentialPrimes == loops)
        cout <<"prime" <<endl;
    else
        cout <<"not prime" <<endl;
    
    

    return(0);
}





/* Chapter 4 Exercise 11b by Albert Villaroman 11-27-06 */

#include <iostream.h>

int main () {
    cout <<"Enter starting number: ";
        int u;
        cin >>u;
    while (u<0) {
        cout <<"Enter a non-negative integer: ";
          cin >>u;
    }

    cout <<"Enter ending number: ";
        int e;
        cin >>e;
    while (e<0) {
        cout <<"Enter a non-negative integer: ";
          cin >>e;
    }
    
    
    int primesInBetween=0;
    
    
    for (u; u<=e; u++) {
        int potentialPrimesInLoop = 0, loopsInCurrU = 0;
    
        for (int f=2; f<u; f++) {
            if (u%f != 0)
                potentialPrimesInLoop++;
            loopsInCurrU++;
        } //end for
    
        if (potentialPrimesInLoop == loopsInCurrU)
            primesInBetween++;
    }//end for


        
    cout <<"Number of primes in range: " <<primesInBetween <<"\n\n";
    
    
    return(0);
}





/* Chapter 4 Exercise 12 by Albert Villaroman 12-5-06 */

#include <iostream.h>

int main() {
    cout <<"Enter a number: ";
      int userInt;
      cin >>userInt;
    
    int counter=2;
    cout <<"Prime factors: ";
    while (counter <= userInt) {
          if (userInt%counter == 0) {
                              cout <<counter <<" ";
                              userInt /= counter;
                              }
          else
              counter++;
    }
    cout <<endl;
    return(0);
}
        




/* Chapter 4 Exercise 13 by Albert Villaroman 12-5-06 */

#include <iostream.h>

int main() {
    int user1, user2;
    
    cout <<"Enter an integer: ";
      cin >>user1;
    while (user1 < 0) {
          cout <<"Enter a non-negative integer: ";
            cin >>user1;
          }
    
    cout <<"Enter another integer: ";
      cin >>user2;
    while (user2 < 0) {
          cout <<"Enter a non-negative integer: ";
            cin >>user2;
          }
          
    int GCD=0; bool stop=false;
    
    for (int i=2; (i<user1 || i<user2); i++) {
        if (user1%i==0 && user2%i==0)
        GCD = i;
        }
    

    cout <<"GCD is " <<GCD <<endl;
                
    return(0);
}



 

 

 


Comment/Reply (w/o sign-up)

borlafu
Kind of spam uh?

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for source, codes, c
advertisement



Free Source Codes For C++

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com