0% found this document useful (0 votes)
8 views12 pages

lab5

The document contains multiple exercises from a programming lab, each focusing on different coding tasks such as checking for even/odd numbers, primality, calculating the cube of a number, converting between binary and decimal, and finding prime numbers in a range. Additional exercises include calculating properties of circles, swapping values, finding the largest element in an array, and implementing a basic calculator. Each exercise includes C++ code snippets demonstrating the implementation of the respective functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

lab5

The document contains multiple exercises from a programming lab, each focusing on different coding tasks such as checking for even/odd numbers, primality, calculating the cube of a number, converting between binary and decimal, and finding prime numbers in a range. Additional exercises include calculating properties of circles, swapping values, finding the largest element in an array, and implementing a basic calculator. Each exercise includes C++ code snippets demonstrating the implementation of the respective functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab 5

Exc 1
//Exc 1: Parity
#include <iostream>
using namespace std;

bool isEven(int num) {


return num % 2 == 0;
}

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;

int cube(int num) {


return num * num * num;
}

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;

int bin_dec(string bin) {


int l = bin.length();
if (l == 0) {
return 0;
}
if (bin[0] == '0') {
return bin_dec(bin.substr(1, l - 1));
} else {
return pow(2, l - 1) + bin_dec(bin.substr(1, l - 1));
}
}

string dec_bin (int x) {


if (x==0) {
return "";

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);

cout<<"\n\nenter your dec:\n";


int x;
cin>>x;
cout<<"convert dec to bin: "<<dec_bin(x);

return 0;
}

Exc 5
//Exc 5: primes in range
#include <iostream>
using namespace std;

// Function to check if a number is prime


bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) return false;

Lab 5 4
}
return true;
}

// Function to find and print prime numbers in a range


void findPrimesInRange(int start, int end) {
cout << "Prime numbers between " << start << " and " << end
for (int i = start; i <= end; ++i) {
if (isPrime(i)) {
cout << i << " ";
}
}
cout << endl;
}

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;

// Function to calculate the diameter of a circle


double calculateDiameter(double radius) {
return 2 * radius;
}

// Function to calculate the circumference of a circle


double calculateCircumference(double radius) {
return 2 * PI * radius;
}

// Function to calculate the area of a circle


double calculateArea(double radius) {
return PI * radius * radius;
}

int main() {
double radius;
cout << "Enter the radius of the circle: ";
cin >> radius;

cout << "Diameter: " << calculateDiameter(radius) << endl;


cout << "Circumference: " << calculateCircumference(radius)
cout << "Area: " << calculateArea(radius) << endl;

return 0;
}

Exc 7

Lab 5 6
//Exc 7; square
#include <iostream>
using namespace std;

// Function to find the square of a number


int square(int num) {
return num * num;
}

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;

void swap(int &a, int &b) {


int c = a;
a = b;
b = c;
}
int main() {
int a,b;

Lab 5 7
cin>>a>>b;

swap(a,b);
cout<<a<<" "<<b;

return 0;
}

Exc 9
//Exc 9: largest
#include <iostream>
using namespace std;

// Function to find the largest element in an array


int getLargest(int arr[], int size) {
int largest = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}

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;

// Function to find maximum and minimum


void findMaxMin(int arr[], int size, int &max, int &min) {
max = arr[0];
min = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
}

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
}

findMaxMin(arr, n, max, min);

cout << "Maximum value: " << max << endl;


cout << "Minimum value: " << min << endl;

return 0;
}

Exc 11
//Exc 11: reverse number
#include <iostream>
using namespace std;

// Function to reverse a number


int reverseNumber(int num) {
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return reversed;
}

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;

// Functions for basic operations


int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { return b != 0 ? a / b : 0; }
int modulus(int a, int b) { return b != 0 ? a % b : 0; }

int main() {
char choice;
do {
int op, a, b, result;

cout << "MENU\n";


cout << "1. Add\n";
cout << "2. Subtract\n";
cout << "3. Multiply\n";
cout << "4. Divide\n";
cout << "5. Modulus\n";
cout << "Enter your choice: ";
cin >> op;

cout << "Enter your two numbers: ";


cin >> a >> b;

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
}

cout << "Result: " << result << endl;


cout << "Continue? (y/n): ";
cin >> choice;

} while (choice == 'y' || choice == 'Y');

return 0;
}

Lab 5 12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy