










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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
A collection of c++ programming exercises designed to reinforce fundamental programming concepts and introduce basic algorithms. Each exercise includes a clear problem statement, sample output, and a complete c++ code solution. The exercises cover topics such as finding natural numbers, calculating sums, determining prime numbers, finding factorials, and implementing basic algorithms like finding the greatest common divisor (gcd) and calculating the sum of digits. These exercises are suitable for beginners learning c++ and provide practical examples to solidify their understanding of core programming principles.
Typology: Exercises
1 / 18
This page cannot be seen from the preview
Don't miss anything!











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 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
#include 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 using namespace std;
cout << "The perfect numbers between 1 and 500 are:" << endl; for (int num = 1; num <= 500; num++) { int sum = 0; for (int i = 1; i <= num / 2; i++) { if (num % i == 0) { sum += i; } } if (sum == num) { cout << num << endl; } } return 0; }
5. Develop a program in C++ to check whether a number is prime or not. Sample Output: Input a number to check prime or not: 13 The entered number is a prime number. CODE: #include 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 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 using namespace std; bool isPrime(int n) { if (n <= 1) { return false;
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 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; }
8. Develop a program in C++ to find the last prime number that occurs before the entered number. Sample Output: Input a number to find the last prime number occurs before the number: 50 47 is the last prime number before 50 CODE : #include 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 num; cout << "Input a number to find the last prime number occurs before the number: "; cin >> num; for (int i = num - 1; i > 1; i--) { if (isPrime(i)) { cout << i << " is the last prime number before " << num << endl;
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 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. CODE: #include #include #include 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; }
12. Develop a C++ program to list non-prime numbers from 1 to an upper bound. Sample Output: Input the upper limit: 25 The non-prime numbers are: 4 6 8 9 10 12 14 15 16 18 20 21 22 24 25 CODE: #include using namespace std; bool isPrime(int num) { if (num <= 1) { return false; } for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return true; } int main() { int upperLimit; cout << "Input the upper limit: "; cin >> upperLimit; cout << "The non-prime numbers are:" << endl; for (int i = 2; i <= upperLimit; i++) {
if (!isPrime(i)) { cout << i << " "; } } cout << endl; return 0; }
13. Develop a program in C++ to print a square pattern with the # character. Sample Output: Print a pattern like square with # character: Input the number of characters for a side: 4 # # # #
CODE: #include using namespace std; int main() { int sideLength; cout << "Input the number of characters for a side: "; cin >> sideLength; cout << "Print a pattern like square with # character:" << endl; for (int i = 0; i < sideLength; i++) { for (int j = 0; j < sideLength; j++) {
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 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; }