حل lab4 برمجة1 جامعة الامام
- جامعة الامام محمد بن سعود الاسلامية
- Computer Programming CS107 - برمجة 1
- برمجة سي بلس بلس | C++ programming
- 2025-10-01
توصيف
Lab 4 (1 page, 6 Exercises)
Write a C++ program for each of the following exercises where you call the specified function.
Exercise 1
Write a C++ function f that computes f(x) where f(x) = x2 + x + 1.
#include <iostream>
using namespace std;
// Function to compute f(x) = x^2 + x + 1
int f(int x) {
return x * x + x + 1;
}
int main() {
int x;
cout << "Enter an integer value for x: ";
cin >> x;
int result = f(x);
cout << "f(" << x << ") = " << result << endl;
return 0;
}
Exercise 2
Write a C++ function gcd that computes the greatest common divisor of two numbers n and p.
#include <iostream>
using namespace std;
// Function to compute the GCD of two integers using Euclidean algorithm
int gcd(int n, int p) {
while (p != 0) {
int temp = p;
p = n % p;
n = temp;
}
return n;
}
int main() {
int n, p;
cout << "Enter two integers: ";
cin >> n >> p;
int result = gcd(n, p);
cout << "GCD of " << n << " and " << p << " is: " << result << endl;
return 0;
}
Exercise 3
Write a C++ function that computes the maximum of three numbers x, y, and z.
#include <iostream>
using namespace std;
// Function to compute the maximum of three numbers
int maxOfThree(int x, int y, int z) {
int max = x;
if (y > max)
max = y;
if (z > max)
max = z;
return max;
}
int main() {
int x, y, z;
cout << "Enter three integers: ";
cin >> x >> y >> z;
int result = maxOfThree(x, y, z);
cout << "The maximum of " << x << ", " << y << ", and " << z << " is: " << result << endl;
return 0;
}
Exercise 4
Write a C++ function that computes the area and the perimeter of a rectangle of length l and width w.
#include <iostream>
using namespace std;
// Function to compute the area and perimeter of a rectangle
void computeRectangle(int l, int w, int &area, int &perimeter) {
area = l * w;
perimeter = 2 * (l + w);
}
int main() {
int length, width;
int area, perimeter;
cout << "Enter the length and width of the rectangle: ";
cin >> length >> width;
computeRectangle(length, width, area, perimeter);
cout << "Area: " << area << endl;
cout << "Perimeter: " << perimeter << endl;
return 0;
}
Exercise 5
Write a C++ iterative function that computes Un for a given n for the following sequence:
U0 = 2
Un+1 = Un+3
#include <iostream>
using namespace std;
// Iterative function to compute U_n
int computeUn(int n) {
int Un = 2; // U0
for (int i = 1; i <= n; ++i) {
Un += 3;
}
return Un;
}
int main() {
int n;
cout << "Enter the value of n: ";
cin >> n;
int result = computeUn(n);
cout << "U_" << n << " = " << result << endl;
return 0;
}
Exercise 6
Write a C++ recursive function that computes Un for a given n for the following sequence:
U0 = 2
Un+1 = Un+3
#include <iostream>
using namespace std;
// Recursive function to compute U_n
int computeUn(int n) {
if (n == 0)
return 2; // Base case: U0 = 2
else
return computeUn(n - 1) + 3; // Recursive case
}
int main() {
int n;
cout << "Enter the value of n: ";
cin >> n;
int result = computeUn(n);
cout << "U_" << n << " = " << result << endl;
return 0;
}