حل lab3 برمجة1 جامعة الامام
- جامعة الامام محمد بن سعود الاسلامية
- Computer Programming CS107 - برمجة 1
- برمجة سي بلس بلس | C++ programming
- 2025-10-01
توصيف
Lab 3 (1 page, 6 Exercises)
Exercise 1
Write a C++ program that computes n^p where n and p are two numbers given by the user.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n, p,power=1;
cout << "Enter n and p: ";
cin >> n >> p;
for(int i=1;i<=p;i++)
power=power*n;
cout << n << "^" << p << " = " << power << endl;
return 0;
}
Exercise 2
Write a C++ program that computes the sum of the cubes between 1 and n^3 where n is given by the user.
#include <iostream>
using namespace std;
int main() {
int n;
int sum = 0;
cout << "Enter a positive integer n: ";
cin >> n;
if (n <= 0) {
cout << "Please enter a positive integer greater than 0." << endl;
return 1;
}
for (int i = 1; i <= n; i++) {
sum += i * i * i;
}
cout << "The sum of cubes from 1^3 to " << n << "^3 is: " << sum << endl;
return 0;
}
Exercise 3
Write a C++ program that counts the number of divisors of a number n given by the user.
#include <iostream>
using namespace std;
int main() {
int n;
int count = 0;
cout << "Enter a positive integer n: ";
cin >> n;
if (n <= 0) {
cout << "Please enter a positive integer greater than 0." << endl;
return 1;
}
for (int i = 1; i <= n; ++i) {
if (n % i == 0) {
count++;
}
}
cout << "The number of divisors of " << n << " is: " << count << endl;
return 0;
}
Exercise 4
Write a C++ program that determines whether two numbers n and p given by the user are co-prime or not.
#include <iostream>
using namespace std;
int main() {
int n, p;
int a, b;
cout << "Enter the first number n: ";
cin >> n;
cout << "Enter the second number p: ";
cin >> p;
if (n <= 0 || p <= 0) {
cout << "Please enter positive integers only." << endl;
return 1;
}
a = n;
b = p;
// Compute GCD using Euclid's algorithm inside main
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
if (a == 1) {
cout << n << " and " << p << " are co-prime." << endl;
} else {
cout << n << " and " << p << " are NOT co-prime." << endl;
}
return 0;
}
Exercise 5
Write a C++ program that prints the list of characters between two characters a and b given by the user.
#include <iostream>
using namespace std;
int main() {
char a, b;
cout << "Enter the first character a: ";
cin >> a;
cout << "Enter the second character b: ";
cin >> b;
if (a > b) {
cout << "First character must come before or be equal to the second character." << endl;
return 1;
}
cout << "Characters from " << a << " to " << b << " are: ";
for (char c = a; c <= b; ++c) {
cout << c << " ";
}
cout << endl;
return 0;
}
Exercise 6
Write a C++ program that computes nth term of the following sequence:
U0 = 1; Un+1 = 2*Un+1.
#include <iostream>
using namespace std;
int main() {
int n;
int U = 1; // U0 = 1
cout << "Enter the value of n (n >= 0): ";
cin >> n;
if (n < 0) {
cout << "Please enter a non-negative integer." << endl;
return 1;
}
for (int i = 0; i < n; ++i) {
U = 2 * U + 1;
}
cout << "The " << n << "-th term of the sequence is: " << U << endl;
return 0;
}