c++ assignment
c++ assignment
TASK 1
CODE:
#include <iostream>
using namespace std;
int main() {
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;
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];
}
OUTPUT:
TASK 4
CODE:
#include <iostream>
using namespace std;
int main () {
if(!isDuplicate) {
arr3[k] = arr2[i];
k++;
}
}
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];
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;
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];
int i = 0, j = 0;
fullName[i] = '\0';
cout << "Full Name is: " << fullName << endl;
}
OUTPUT: