Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Free Source Codes For C++
demolaynyc
post Dec 19 2006, 06:22 PM
Post #1


Premium Member
Group Icon

Group: Members
Posts: 330
Joined: 2-February 06
Member No.: 11,040



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);
}



Go to the top of the page
 
+Quote Post
borlafu
post Dec 20 2006, 07:26 PM
Post #2


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 58
Joined: 9-November 06
Member No.: 17,133



Kind of spam uh?
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics


 



- Lo-Fi Version Time is now: 2nd December 2008 - 11:38 AM