lab5
lab5
Exc 1
//Exc 1: Parity
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isEven(num)) {
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
return 0;
}
Exc 2
//Exc 2: Primality
#include <iostream>
#include <cmath>
using namespace std;
Lab 5 1
bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isPrime(num)) {
cout << num << " is a prime number." << endl;
} else {
cout << num << " is not a prime number." << endl;
}
return 0;
}
Exc 3
//Exc 3: cube
#include <iostream>
using namespace std;
Lab 5 2
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "The cube of " << num << " is " << cube(num) << "."
return 0;
}
Exc 4
// Exc 4: bin <-> dec
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
Lab 5 3
}
if (x%2 ==0) {
return dec_bin(x/2) + "0";
} else {
return dec_bin(x/2) + "1";
}
}
int main() {
string bin;
cout<<"enter your binary:\n";
getline(cin, bin);
cout <<"convert bin to dec: "<< bin_dec(bin);
return 0;
}
Exc 5
//Exc 5: primes in range
#include <iostream>
using namespace std;
Lab 5 4
}
return true;
}
int main() {
int start, end;
cout << "Enter the start of the range: ";
cin >> start;
cout << "Enter the end of the range: ";
cin >> end;
findPrimesInRange(start, end);
return 0;
}
Exc 6:
//Exc 6: Circle
#include <iostream>
#include <cmath>
using namespace std;
Lab 5 5
const double PI = 3.14159;
int main() {
double radius;
cout << "Enter the radius of the circle: ";
cin >> radius;
return 0;
}
Exc 7
Lab 5 6
//Exc 7; square
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "The square of " << num << " is " << square(num) <<
return 0;
}
Exc 8
// Exc 8: swap
#include <cmath>
#include <iostream>
using namespace std;
Lab 5 7
cin>>a>>b;
swap(a,b);
cout<<a<<" "<<b;
return 0;
}
Exc 9
//Exc 9: largest
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; ++i) {
Lab 5 8
cin >> arr[i];
}
cout << "The largest element in the array is: " << getLarges
return 0;
}
Exc 10:
//Exc 10: min max
#include <iostream>
using namespace std;
int main() {
int n, max, min;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
Lab 5 9
}
return 0;
}
Exc 11
//Exc 11: reverse number
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "The reversed number is: " << reverseNumber(num) <<
Lab 5 10
return 0;
}
Exc 12:
//Exc 12: calculator
#include <iostream>
using namespace std;
int main() {
char choice;
do {
int op, a, b, result;
Lab 5 11
switch (op) {
case 1: result = add(a, b); break;
case 2: result = subtract(a, b); break;
case 3: result = multiply(a, b); break;
case 4: result = divide(a, b); break;
case 5: result = modulus(a, b); break;
default: cout << "Invalid choice!" << endl; continue
}
return 0;
}
Lab 5 12