0% found this document useful (0 votes)
4 views

Muhammad Hamid OOP Lab Assignment

Muhammad Hamid OOP Lab Assignment (1)

Uploaded by

Mian Hamid
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Muhammad Hamid OOP Lab Assignment

Muhammad Hamid OOP Lab Assignment (1)

Uploaded by

Mian Hamid
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Muhammad Hamid - OOP Lab

Assignment
Roll Number: B-29624
Subject: OOP (Lab)

Q1: Write a program that takes 10 integers as input from the user, stores
them in an array, and displays them.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[10];
cout << "Enter 10 integers: ";
for (int i = 0; i < 10; i++) {
cin >> arr[i];
}
cout << "Your Numbers Are: ";
for (int i = 0; i < 10; i++) {
cout << arr[i] << " ";
}
return 0;
}

Q2: Create a program that accepts 5 numbers from the user, stores them
in an array, calculates the sum of all the numbers, and displays the result.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[5], sum = 0;
cout << "Enter 5 integers: ";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
sum += arr[i];
}
cout << "Sum of numbers: " << sum;
return 0;
}

Q3: Input 8 integers from the user, store them in an array, and display the
maximum value in the array.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[8], max;
cout << "Enter 8 integers: ";
for (int i = 0; i < 8; i++) {
cin >> arr[i];
}
max = arr[0];
for (int i = 1; i < 8; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
cout << "Maximum value: " << max;
return 0;
}

Q4: Write a program that takes 10 integers as input from the user, stores
them in an array, and counts how many numbers are even and how many
are odd.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[10], even = 0, odd = 0;
cout << "Enter 10 integers: ";
for (int i = 0; i < 10; i++) {
cin >> arr[i];
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
cout << "Even numbers: " << even << "\nOdd numbers: " << odd;
return 0;
}

Q5: Allow the user to input 7 numbers, store them in an array, and then
ask the user for a number to search for. Display whether or not the
number is found in the array.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[7], search;
bool found = false;
cout << "Enter 7 integers: ";
for (int i = 0; i < 7; i++) {
cin >> arr[i];
}
cout << "Enter number to search: ";
cin >> search;
for (int i = 0; i < 7; i++) {
if (arr[i] == search) {
found = true;
break;
}
}
if (found)
cout << search << " is found in the array.";
else
cout << search << " is not found in the array.";
return 0;
}

Q6: Take 5 integers as input from the user, store them in an array, and
display the elements in reverse order.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[5];
cout << "Enter 5 integers: ";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << "Reversed order: ";
for (int i = 4; i >= 0; i--) {
cout << arr[i] << " ";
}
return 0;
}

Q7: Ask the user to input 6 numbers into an array, then ask for a number
to remove. Create a new array that excludes the specified number and
display it.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[6], remove;
cout << "Enter 6 integers: ";
for (int i = 0; i < 6; i++) {
cin >> arr[i];
}
cout << "Enter number to remove: ";
cin >> remove;
cout << "Updated array: ";
for (int i = 0; i < 6; i++) {
if (arr[i] != remove) {
cout << arr[i] << " ";
}
}
return 0;
}

Q8: Allow the user to enter 5 numbers into an array. Then ask the user for
a number and the position to insert that number. Display the updated
array.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[6], num, pos;
cout << "Enter 5 integers: ";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << "Enter a number to insert: ";
cin >> num;
cout << "Enter position to insert (1 to 5): ";
cin >> pos;
for (int i = 5; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = num;
cout << "Updated array: ";
for (int i = 0; i < 6; i++) {
cout << arr[i] << " ";
}
return 0;
}

Q9: Take input for 10 numbers from the user, store them in an array, and
calculate the average of all the numbers. Display the average.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[10], sum = 0;
float avg;
cout << "Enter 10 integers: ";
for (int i = 0; i < 10; i++) {
cin >> arr[i];
sum += arr[i];
}
avg = sum / 10.0;
cout << "Average: " << avg;
return 0;
}

Q10: Ask the user to input 10 integers, store them in an array, and then
allow the user to input another number. Display how many times that
number occurs in the array.
Solution:
#include <iostream>
using namespace std;
int main() {
int arr[10], search, count = 0;
cout << "Enter 10 integers: ";
for (int i = 0; i < 10; i++) {
cin >> arr[i];
}
cout << "Enter a number to search: ";
cin >> search;
for (int i = 0; i < 10; i++) {
if (arr[i] == search) {
count++;
}
}
cout << "Occurrences: " << count;
return 0;
}

You might also like