Prepare for your exams

Study with the several resources on Docsity

Earn points to download

Earn points by helping other students or get them with a premium plan

Guidelines and tips

Prepare for your exams

Study with the several resources on Docsity

Earn points to download

Earn points by helping other students or get them with a premium plan

Community

Ask the community

Ask the community for help and clear up your study doubts

University Rankings

Discover the best universities in your country according to Docsity users

Free resources

Our save-the-student-ebooks!

Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors

Answer key to stuff and all you

Typology: Exercises

2023/2024

1 / 18

Download Object Oriented Programming and more Exercises Computer Science in PDF only on Docsity! Christian Hamphrey Rule A. Beto BSCE 2B LEARNING MODULE NO. 6 – POSTTEST Instructions: For each question, encircle the letter of the correct best answer. 1. Develop a program in C++ to find the first 10 natural numbers. Sample output: The natural numbers are: 1 2 3 4 5 6 7 8 9 10 CODE: #include <iostream> using namespace std; int main() { cout << "The natural numbers are:" << endl; for (int i = 1; i <= 10; i++) { cout << i << " "; } cout << endl; return 0; } 2. Develop a program in C++ to find the sum of the first 10 natural numbers. Sample Output: Find the first 10 natural numbers: The natural numbers are: 1 2 3 4 5 6 7 8 9 10 The sum of first 10 natural numbers: 55 CODE: #include <iostream> using namespace std; int main() { int n = 10; int sum = 0; cout << "Find the first " << n << " natural numbers:" << endl; cout << "-------------------------------" << endl; cout << "The natural numbers are:" << endl; for (int i = 1; i <= n; i++) { cout << i << " "; sum += i; } cout << endl; cout << "The sum of the first " << n << " natural numbers: " << sum << endl; return 0; } 3. Develop a program in C++ to display n terms of natural numbers and their sum. Sample Output: Input a number of terms: 7 The natural numbers upto 7th terms are: 1 2 3 4 5 6 7 The sum of the natural numbers is: 28 CODE: #include <iostream> using namespace std; } } return true; } int main() { int num; cout << "Input a number to check prime or not: "; cin >> num; if (isPrime(num)) { cout << "The entered number is a prime number." << endl; } else { cout << "The entered number is not a prime number." << endl; } return 0; } 6. Develop a program in C++ to find a prime number within a range. Input number for starting range: 1 Input number for ending range: 100 The prime numbers between 1 and 100 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 The total number of prime numbers between 1 to 100 is: 25 CODE: #include <iostream> using namespace std; bool isPrime(int n) { if (n <= 1) { return false; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } int main() { int start, end, count = 0; cout << "Input number for starting range: "; cin >> start; cout << "Input number for ending range: "; cin >> end; cout << "The prime numbers between " << start << " and " << end << " are:" << endl; for (int i = start; i <= end; i++) { if (isPrime(i)) { cout << i << " "; count++; } } cout << "\nThe total number of prime numbers between " << start << " and " << end << " is: " << count << endl; return 0; } 7. Develop a program in C++ to find the factorial of a number. Sample output: Input a number to find the factorial: 5 The factorial of the given number is: 120 CODE: #include <iostream> using namespace std; int factorial(int n) { if (n == 0 || n == 1) { return 1; } return n * factorial(n - 1); } int main() { int num; cout << "Input a number to find the factorial: "; cin >> num; if (num < 0) { cout << "Factorial of negative numbers is not defined." << endl; } else { cout << "The factorial of the given number is: " << factorial(num) << endl; } return 0; } cout << "The Greatest Common Divisor is: " << gcd(num1, num2) << endl; return 0; } 10. Develop a program in C++ to find the sum of the digits of a given number. Sample Output: Input a number: 1234 The sum of digits of 1234 is: 10 CODE: #include <iostream> using namespace std; int sumOfDigits(int num) { int sum = 0; while (num > 0) { sum += num % 10; num /= 10; } return sum; } int main() { int number; cout << "Input a number: "; cin >> number; cout << "The sum of digits of " << number << " is: " << sumOfDigits(number) << endl; return 0; } 11. Develop a C++ program that asks the user to enter positive integers in order to process count, maximum, minimum, and average or terminate the process with -1. Sample Output: Your input is for termination. Here is the result below: Number of positive integers is: 4 The maximum value is: 9 The minimum value is: 3 The average is 6.00 CODE: #include <iostream> #include <iomanip> #include <climits> using namespace std; int main() { int num, count = 0, sum = 0, maxNum = INT_MIN, minNum = INT_MAX; cout << "Enter positive integers (enter -1 to terminate):" << endl; while (true) { cout << "Enter a number: "; cin >> num; if (num == -1) { break; } if (num < 0) { cout << "Please enter a positive integer." << endl; continue; } count++; sum += num; if (num > maxNum) { maxNum = num; } if (num < minNum) { minNum = num; } } if (count > 0) { double average = static_cast<double>(sum) / count; cout << "Your input is for termination. Here is the result below:" << endl; cout << "Number of positive integers is: " << count << endl; cout << "The maximum value is: " << maxNum << endl; cout << "The minimum value is: " << minNum << endl; cout << "The average is: " << fixed << setprecision(2) << average << endl; } else { cout << "No positive integers were entered." << endl; } return 0; } cout << "# "; } cout << endl; } return 0; } 14. Develop a program in C++ to display the multiplication table vertically from 1 to n. Sample Output: Input the number up to: 5 Multiplication table from 1 to 5 1x1=1 2x1=2 3x1=3 4x1=4 5x1=5 1x2=2 2x2=4 3x2=6 4x2=8 5x2=10 1x3=3 2x3=6 3x3=9 4x3=12 5x3=15 1x4=4 2x4=8 3x4=12 4x4=16 5x4=20 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 1x10=10 2x10=20 3x10=30 4x10=40 5x10=50 CODE: #include <iostream> using namespace std; int main() { int n; cout << "Input the number up to: "; cin >> n; cout << "Multiplication table from 1 to " << n << ":" << endl; for (int i = 1; i <= 10 * n; i++) { cout << (i % n == 0 ? n : i % n) << " x " << (i / n + (i % n == 0 ? 0 : 1)) << " = " << i << "\t" << (i % n == 0 ? "\n" : ""); } return 0; } CODE: #include <iostream> using namespace std; int main() { int n; cout << "Enter a number (n): "; cin >> n; cout << "Multiplication Table from 1 to " << n << ":" << endl; for (int i = 1; i <= 10; i++) { for (int j = 1; j <= n; j++) { cout << j << " x " << i << " = " << (i * j) << "\t"; } cout << endl; } return 0; } 15. Develop a C++ program that displays the sum of n odd natural numbers. Sample Output: Input number of terms: 5 The odd numbers are: 1 3 5 7 9 The Sum of odd Natural Numbers up to 5 terms: 25 CODE: #include <iostream> using namespace std; int main() { int n; cout << "Input number of terms: "; cin >> n; cout << "The odd numbers are:"; int sum = 0; for (int i = 1; n > 0; i += 2) { cout << " " << i; sum += i; n--; } cout << "\nThe Sum of odd Natural Numbers up to " << n << " terms: " << sum << endl; return 0; }