نازك (١)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

#include <iostream>

using namespace std;

int findMax(int arr[], int n);

int main() {
int n;
cout << "Enter the number of
elements in the array: ";
cin >> n;

int arr[n];
for (int i = 0; i < n; i++) {
cout << "Enter element " <<
i+1 << ": ";
cin >> arr[i];
}

int max = findMax(arr, n);


cout << "The maximum value in
the array is: " << max << endl;
return 0;
}

int findMax(int arr[], int n) {


if (n == 1) {
return arr[0];
} else {
int max = findMax(arr, n-1);
if (max > arr[n-1]) {
return max;
} else {
return arr[n-1];
}
}
}
#include <iostream>
#include <unordered_set>
using namespace std;

int main() {
int n;
cout << "Enter the number of
elements in the array: ";
cin >> n;

int arr[n];
cout << "Enter " << n << "
integers:" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

int negativeOddCount = 0;
unordered_set<int> uniqueSet;
for (int i = 0; i < n; i++) {
if (arr[i] < 0 && arr[i] % 2 != 0) {
negativeOddCount++;
}
uniqueSet.insert(arr[i]);
}

int uniqueCount =
uniqueSet.size();
cout << "The number of negative
odd elements is: " <<
negativeOddCount << endl;
cout << "The number of unique
elements is: " << uniqueCount <<
endl;

return 0;
}
#include <iostream>
#include <stack>
using namespace std;

int main() {
stack<int> originalStack;
stack<int>
multiplesOfThreeStack;
stack<int> primeStack;

int n;
cout << "Enter the number of
elements to add to the stack: ";
cin >> n;

cout << "Enter " << n << "


integers to add to the stack:" <<
endl;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
originalStack.push(num);
}
while (!originalStack.empty()) {
int num = originalStack.top();
originalStack.pop();
if (num % 3 == 0) {
multiplesOfThreeStack.push
(num * 3);
}

// Check if num is prime


bool isPrime = true;
if (num == 0 || num == 1) {
isPrime = false;
} else {
for (int i = 2; i <= num / 2; i+
+) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
primeStack.push(num);
}
}

cout << "Multiples of 3 stack:" <<


endl;
while (!
multiplesOfThreeStack.empty()) {
cout <<
multiplesOfThreeStack.top() <<
endl;
multiplesOfThreeStack.pop();
}

cout << "Prime numbers stack:"


<< endl;
while (!primeStack.empty()) {
cout << primeStack.top() <<
endl;
primeStack.pop();
}
return 0;
}

You might also like