Download C++ Basics Exercises and more Exercises Computer Science in PDF only on Docsity!
C++
Algorithm Development - Control Statements – Logical Operators
Practice exercises.
1. Write a C++ program that inputs a series of 10 numbers and determines and prints
the largest of the numbers.
2. The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and
is defined as follows: n! = n * (n-1) * (n-2) * ... * 1 and n! = 1 for n = 0
For example, 5! = 5 * 4 * 3 * 2 * 1 which is 120.
Write a program that reads a nonnegative integer and computes and prints its
factorial.
#include using namespace std; int main() { int counter = 0, number, largest; // declare variables cout << "Enter first number: "; // prompt user cin >> largest; //read "largest" from user while (++counter < 10) { // loop 10 times cout << "Enter next number: "; // prompt user cin >> number; // read "number" from user if (number > largest) { // if value of "number" > "largest" largest = number; // then update value of "largest" } // end if } // end while // display result cout << "Largest is: " << largest << endl; return 0; // indicate program ended successfully } // end function main using namespace std; #include int main() { int num, factorial = 1; do { cout << "Enter a positive integer: "; cin >> num; } while (num <= 0); for (int x = 1; x <= num; x++) factorial *= x; cout << num << "! = " << factorial; }
Another method :
3. Write a program that evaluates the factorials of the integers from 1 to a positive
number entered by the user. Print the results in a table.
#include using namespace std; int main() { int n = 0, number; // declare variables unsigned factorial = 1; // declare variable // "do/while" loop to insure that the user enters a positive integer do { cout << "Enter positive integer: "; // prompt user cin >> number; // read "number" from user } while (number < 0); // while value of "number" // is negative, loop again // while loop to count from "n" to value of "number-1". First it // tests if "n" is < than "number", then it increments "n" by 1 while (n++ < number) { factorial = factorial * n; // calculate "factorial" } // end while // output result cout << number << "! is " << factorial << endl; return 0; // indicate program ended successfully } // end function main #include using namespace std; #include int main() { int num, factorial = 1; do { cout << "Enter a positive integer: "; cin >> num; } while (num <= 0); cout << setw(7) << "Number" << setw(15) << "Factorial" << endl; for (int x = 1; x <= num; x++) { factorial *= x; cout << setw(7) << x << setw(15) << factorial << endl; } }
Another Method:
5. Write a program that reads an integer from the keyboard then determines all the
prime numbers between 1 and that integer.
#include using namespace std; void main() { int number, counter = 1; cout << "enter a positive number :"; cin >> number; for (int i = 2; i < number; i++) { if (number % i == 0) counter++; } if (counter == 1) cout << " this number is prime "; else cout << " this number is not prime "; } #include using namespace std; int main() { int Number, Flag = 0; // declare variables, and initialize "Flag" to 0 cout << "Enter a positive number: "; // prompt user cin >> Number; // read "number" from user cout << "The prime numbers from 1 to "<< Number << " are:" << endl; // "for" loop that count from 2 to "Number", // and for each iteration it tests if the value of "i" is a prime or not for (int i = 2; i <= Number; i++) { Flag = 0; // for each iteration reset the value of "Flag" to 0 // "for" loop to count from 2 to i/2, // and for each value of "j" test if "i" is dividable by "j" for (int j = 2; j <= i / 2; j++) { // test if "i" is dividable by "j" by checking // if the remainder of "i" divided by "j" is equal to 0 if ((i % j) == 0) { Flag = 1; // if yes, then set the value of "Flag" to 1 } // end if } // end for if (Flag == 0) { // test if "Flag" is still equal to 0 cout << i << endl; // if yes, output "i" to the screen } // end if } // end for return 0; // indicate program ended successfully } // end function main
6. What is the output of the following programs?
#include using namespace std; void main() { int x = 10; while (x) { cout << --x << " "; } cout << endl; }
OUTPUT: 9 8 7 6 5 4 3 2 1 0
#include using namespace std; void main() { int x = 10; while (x) { cout << x-- << " "; } cout << endl; }
OUTPUT: 10 9 8 7 6 5 4 3 2 1
#include using namespace std; int main() { int count = 0; while (count++ <= 5) cout << (count % 2? "***" : "---") << endl; cout << count << endl << endl; return 0; }
OUTPUT:
If we had: cout << (count % 2 == 0? "*" : "---") << endl; Then the answer would be:
7**
#include using namespace std; void main() { int j; for (j = 1; j <= 10; j++) { if ((j < 5) || (j >= 7)) cout << j << endl; } cout << endl; } OUTPUT: 1 2 3 4 7 8 9 10 #include using namespace std; void main() { int j = 10, b = 5; for (; b <= j; j - = 1) { if ((j % b == 0) && (j * 5 <= 25)) continue; if ((j < 7) || (j >= 9)) cout << j << endl; } cout << endl; } OUTPUT: 10 9 6 #include using namespace std; void main() { for (int i = 45; i > 1; i = i / 5) cout << i++ << endl; cout << "continue"; cout << endl << endl; } OUTPUT: 45 9 2 continue #include using namespace std; void main() { int i, j, k; int x = 0; for (i = 1; i <= 5; i++) for (j = 1; j <= 7; j += 2) for (k = 1; k < 15; k += 3) x++; cout << x << endl << endl; } OUTPUT: 100
#include using namespace std; void main() { int j; for (j = 1; j <= 10; j++) { if ((j > 5) || (j <= 7)) cout << j << endl; } cout << endl; } OUTPUT: 1 2 3 4 5 6 7 8 9 10 #include using namespace std; void main() { int i, j, k; for (i = 1; i <= 2; i++) for (j = 1; j <= 2; j++) for (k = 1; k <= 3; k++) cout << k << " "; cout << endl << endl; } OUTPUT: 1 2 3 1 2 3 1 2 3 1 2 3 #include using namespace std; void main() { int x; for (x = 1; x <= 5; x++) { switch (x) { case 1: cout << x << endl; case 2: cout << x << endl; case 3: cout << x << endl; break; default: cout << x + 1 << endl; } } cout << endl; } OUTPUT: 1 1 1 2 2 3 5 6