xisto Community

# Free Source Codes For C++

## Recommended Posts

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

`/* 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);}`

Kind of spam uh?

## Create an account

Register a new account