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

c++ assignment

The document contains a series of programming tasks in C++ that cover various fundamental concepts such as prime number checking, sorting arrays, finding common elements, and calculating the union of two arrays. Each task includes code snippets along with expected outputs, demonstrating the functionality of the programs. The tasks also explore array manipulation, user input handling, and basic conditional logic.

Uploaded by

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

c++ assignment

The document contains a series of programming tasks in C++ that cover various fundamental concepts such as prime number checking, sorting arrays, finding common elements, and calculating the union of two arrays. Each task includes code snippets along with expected outputs, demonstrating the functionality of the programs. The tasks also explore array manipulation, user input handling, and basic conditional logic.

Uploaded by

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

PROGRAMMING FUNDAMENTALS (LAB)

TASK 1
CODE:
#include <iostream>
using namespace std;

int main() {

bool isPrime = true;


int x;
cout << "Enter a number: ";
cin >> x;

if (x == 1) {
isPrime = false;
}
else {
for (int i = 2; i <= x / 2; i++) {
if (x % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
cout << "It is a prime number" << endl;
}
else {
cout << "It is not a prime number" << endl;
}

OUTPUT:

TASK 2
CODE:
#include <iostream>
using namespace std;

int main() {

int n;
cout << "Enter size: ";
cin >> n;
int arr [n];
int number;

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


cout << "Enter a number: ";
cin >> arr[i];
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

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


cout << arr[i] << " ";
}
cout << endl << "Second Largest number is: " << arr [n - 2];
}

OUTPUT:

TASK 3
CODE:
#include <iostream>
using namespace std;

int main() {
int arr1 [5], arr2[5];
for (int i = 0; i < 5; i++) {
cout << "Enter element for first array: ";
cin >> arr1[i];
}

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


cout << "Enter element for second array: ";
cin >> arr2[i];
}
cout << "Common Elements: ";
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (arr1[i] == arr2[j]) {
cout << << arr1[i] << " ";
break;
}
}
}
}

OUTPUT:
TASK 4
CODE:
#include <iostream>
using namespace std;

int main () {

int arr1[5], arr2[5], arr3[10];


int k = 0;

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


cout << "Enter 5 elements for first array: ";
cin >> arr1[i];
arr3[k] = arr1[i];
k++;
}

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


cout << "Enter 5 elements for second array: ";
cin >> arr2[i];
bool isDuplicate = false;

for(int j = 0; j < k; j++) {


if(arr2[i] == arr3[j]) {
isDuplicate = true;
break;
}
}

if(!isDuplicate) {
arr3[k] = arr2[i];
k++;
}
}

cout << "Union of array is: ";


for(int i = 0; i < k; i++) {
cout << arr3[i] << " ";
}
}

OUTPUT:
TASK 5
CODE:
#include <iostream>
using namespace std;

int main(){
float arr[5] = { 1, 2, 4, 3, 5 };
bool a= true;
cout << "Size of bool: " << sizeof(a) << " byte" << endl;
cout << "Size of Array: " << sizeof(arr) << " byte" << endl;
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
return 0;
}

OUTPUT:
TASK 6
CODE:
#include <iostream>
using namespace std;

int main() {
int arr1[5], arr2[5];

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


cout << "Enter elements for first array: ";
cin >> arr1[i];
}

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


cout << "Enter elements for second array: ";
cin >> arr2[i];
}

cout << "Elements present in the first array but not in second array: ";
for(int i = 0; i < 5; i++) {
bool found = false;
for(int j = 0; j < 5; j++) {
if(arr1[i] == arr2[j]) {
found = true;
break;
}
}
if(!found) {
cout << arr1[i] << " ";
}
}
}

OUTPUT:
TASK 7
CODE:
#include <iostream>
using namespace std;

int main() {
int educationYears, experience, publications;

cout << "Enter the number of years of your education: ";


cin >> educationYears;

if (educationYears == 16) {
cout << "You can apply for the position of Lab Instructor.\n";
}
else if (educationYears == 18) {
cout << "Enter the number of years of your teaching experience: ";
cin >> experience;

if (experience >= 5) {
cout << "You can apply for the position of Assistant Professor.\n";
} else {
cout << "You can apply for the position of Lecturer.\n";
}
}
else if (educationYears > 18) {
cout << "Enter the number of your publications: ";
cin >> publications;
if (publications >= 30) {
cout << "You can apply for the position of Professor.\n";
} else {
cout << "You can apply for the position of Associate Professor.\n";
}
}
else {
cout << "You are not eligible for any academic position.\n";
}
}

OUTPUT:

TASK 8
CODE:
#include <iostream>
using namespace std;

int main() {
char str[100];
cout << "Enter a string: ";
cin.getline(str, 100);

int length = 0;
while (str[length] != '\0') {
length++;
}
cout << "Length: " << length << endl;
}

OUTPUT:

TASK 9
CODE:
#include <iostream>
using namespace std;

int main() {
char firstName[50], lastName[50], fullName[100];

cout << "Enter the First Name: ";


cin.getline(firstName, 50);

cout << "Enter the Last Name: ";


cin.getline(lastName, 50);

int i = 0, j = 0;

while (firstName[i] != '\0') {


fullName[i] = firstName[i];
i++;
}

fullName[i] = ' ';


i++;

while (lastName[j] != '\0') {


fullName[i] = lastName[j];
i++;
j++;
}

fullName[i] = '\0';

cout << "Full Name is: " << fullName << endl;
}

OUTPUT:

You might also like