Code # 01

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

Code # 01:

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter the size of the array: ";

cin >> n;

if (n <= 0) {

cerr << "Invalid array size. Exiting program." << endl;

return 1;

int arr[n];

cout << "Enter " << n << " integers, separated by spaces:" << endl;

for (int i = 0; i < n; ++i) {

cin >> arr[i];

int sum = 0;

for (int i = 0; i < n; ++i) {

sum += arr[i];

double average = static_cast<double>(sum) / n;

cout << "Sum: " << sum << endl;

cout << "Average: " << average << endl;

return 0; // Exit successfully

}
OutPut:

Code # 02:
#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter the size of the array: ";

cin >> n;

if (n <= 0) {

cerr << "Invalid array size. Exiting program." << endl;

return 1;

int arr[n];

cout << "Enter " << n << " integers, separated by spaces:" << endl;

for (int i = 0; i < n; ++i) {

cin >> arr[i];

int max_value = arr[0];

int min_value = arr[0];

int max_position = 0;

int min_position = 0;

for (int i = 1; i < n; ++i) {

if (arr[i] > max_value) {

max_value = arr[i];
max_position = i;

} else if (arr[i] < min_value) {

min_value = arr[i];

min_position = i;

cout << "Maximum value: " << max_value << " at position " << max_position + 1 << endl;

cout << "Minimum value: " << min_value << " at position " << min_position + 1 << endl;

return 0;

OutPut:

Code # 03:
#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter the size of the array: ";

cin >> n;

if (n <= 0) {

cerr << "Invalid array size. Exiting program." << endl;

return 1;

}
int arr[n];

cout << "Enter " << n << " integers, separated by spaces:" << endl;

for (int i = 0; i < n; ++i) {

cin >> arr[i];

cout << "Original Array: ";

for (int i = 0; i < n; ++i) {

cout << arr[i] << " ";

cout << endl;

for (int i = 0; i < n / 2; ++i) {

int temp = arr[i];

arr[i] = arr[n - i - 1];

arr[n - i - 1] = temp;

cout << "Reversed Array: ";

for (int i = 0; i < n; ++i) {

cout << arr[i] << " ";

cout << endl;

return 0;

OutPut:
Code # 04:
#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter the size of the array: ";

cin >> n;

if (n <= 0) {

cerr << "Invalid array size. Exiting program." << endl;

return 1;

int arr[n];

cout << "Enter " << n << " integers, separated by spaces:" << endl;

for (int i = 0; i < n; ++i) {

cin >> arr[i];

int searchValue, replaceValue;

cout << "Enter the value to search for: ";

cin >> searchValue;

cout << "Enter the replacement value: ";

cin >> replaceValue;

bool found = false;

for (int i = 0; i < n; ++i) {

if (arr[i] == searchValue) {

arr[i] = replaceValue;
found = true;

if (found) {

cout << "Array after replacement: ";

for (int i = 0; i < n; ++i) {

cout << arr[i] << " ";

cout << endl;

} else {

cout << "Value not found in the array. No replacement performed." << endl;

return 0;

OutPut:

Code # 05:
#include <iostream>

#include <vector>

using namespace std;

int main() {
int n;

cout << "Enter the size of the array: ";

cin >> n;

if (n <= 0) {

cerr << "Invalid array size. Exiting program." << endl;

return 1; // Exit with an error code

vector<int> arr(n);

cout << "Enter " << n << " integers, separated by spaces:" << endl;

for (int i = 0; i < n; ++i) {

cin >> arr[i];

int evenCount = 0;

int oddCount = 0;

vector<int> evenNumbers;

vector<int> oddNumbers;

for (int i = 0; i < n; ++i) {

if (arr[i] % 2 == 0) {

// Even number

evenCount++;

evenNumbers.push_back(arr[i]);

} else {

// Odd number

oddCount++;
oddNumbers.push_back(arr[i]);

cout << "Count of even numbers: " << evenCount << endl;

cout << "Even numbers: ";

for (int num : evenNumbers) {

cout << num << " ";

cout << endl;

cout << "Count of odd numbers: " << oddCount << endl;

cout << "Odd numbers: ";

for (int num : oddNumbers) {

cout << num << " ";

cout << endl;

return 0;

OutPut:

You might also like