13.integer To Binary
13.integer To Binary
integer to binary
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
int binary[32];
int i = 0;
while (n > 0) {
binary[i] = n % 2;
n /= 2; 19.
i++; #include <iostream>
} using namespace std;
cout << "Binary representation: "; int main() {
for (int j = i - 1; j >= 0; j--) { int rows, cols;
cout << binary[j]; cout << "Enter the number of rows and columns of the
} matrices: ";
return 0; cin >> rows >> cols;
} int matrix1[rows][cols], matrix2[rows][cols], sum[rows]
[cols];
14.xy plane cout << "Enter the elements of first matrix: " << endl;
#include <iostream> for(int i = 0; i < rows; i++) {
#include <cmath> for(int j = 0; j < cols; j++) {
using namespace std; cin >> matrix1[i][j];
int main() { }
double x1, y1, x2, y2; }
cout << "Enter the coordinates of the first point (x1, y1): "; cout << "Enter the elements of second matrix: " << endl;
cin >> x1 >> y1; for(int i = 0; i < rows; i++) {
cout << "Enter the coordinates of the second point (x2, y2): "; for(int j = 0; j < cols; j++) {
cin >> x2 >> y2; cin >> matrix2[i][j];
double dot_product = x1 * x2 + y1 * y2; }
double euclidean_distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); }
cout << "Dot product: " << dot_product << endl; for(int i = 0; i < rows; i++) {
cout << "Euclidean distance: " << euclidean_distance << endl; for(int j = 0; j < cols; j++) {
return 0; sum[i][j] = matrix1[i][j] + matrix2[i][j];
} }
}
15.list common multiple cout << "The sum of the two matrices is: " << endl;
#include <iostream> for(int i = 0; i < rows; i++) {
using namespace std; for(int j = 0; j < cols; j++) {
int gcd(int a, int b) { cout << sum[i][j] << " ";
if (b == 0) { }
return a; cout << endl;
} }
return gcd(b, a % b); return 0;
} }
int lcm(int a, int b) { 20.
return (a * b) / gcd(a, b); #include <iostream>
} using namespace std;
int main() { int main() {
int a, b; int rows1, cols1, rows2, cols2;
cout << "Enter two integers: "; cout << "Enter the number of rows and columns of the first
cin >> a >> b; matrix: ";
cout << "LCM of " << a << " and " << b << " is " << lcm(a, b) << endl; cin >> rows1 >> cols1;
return 0; } cout << "Enter the number of rows and columns of the
second matrix: ";
16. accept integer com aver cin >> rows2 >> cols2;
#include <iostream>
#include <cmath> if(cols1 != rows2) {
using namespace std; cout << "Error: The number of columns of the first matrix
int main() { must be equal to the number of rows of the second matrix."
int n; << endl;
cout << "Enter the number of integers: "; return 0;
cin >> n; }
int arr[n]; int matrix1[rows1][cols1], matrix2[rows2][cols2],
cout << "Enter the integers: "; product[rows1][cols2];
for(int i = 0; i < n; i++) { cout << "Enter the elements of first matrix: " << endl;
cin >> arr[i]; for(int i = 0; i < rows1; i++) {
} for(int j = 0; j < cols1; j++) {
// Compute the average cin >> matrix1[i][j];
double sum = 0; }
for(int i = 0; i < n; i++) { }
sum += arr[i]; } cout << "Enter the elements of second matrix: " << endl;
double average = sum / n; for(int i = 0; i < rows2; i++) {
// Compute the standard deviation for(int j = 0; j < cols2; j++) {
double variance = 0; cin >> matrix2[i][j];
for(int i = 0; i < n; i++) { }
variance += pow(arr[i] - average, 2); } }
double stdDev = sqrt(variance / n); for(int i = 0; i < rows1; i++) {
// Display the results for(int j = 0; j < cols2; j++) {
cout << "The average is: " << average << endl; product[i][j] = 0;
cout << "The standard deviation is: " << stdDev << endl; for(int k = 0; k < cols1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
return 0; }} }
} cout << "The product of the two matrices is: " << endl;
for(int i = 0; i < rows1; i++) {
for(int j = 0; j < cols2; j++) {
cout << product[i][j] << " ";
}
cout << endl;
}
return 0;
}
1.Absolute value fun 6.factorial 10.Maximum,min
#include <iostream> #include <iostream> #include <iostream>
using namespace std; using namespace std; using namespace std;
int absoluteValue(int num) { int factorial(int n) { void findMinMax(int arr[], int size, int& min,
if (num < 0) { if (n == 0) { int& max) {
return -num; return 1; min = arr[0];
} else { } else { max = arr[0];
return num; return n * factorial(n-1); for (int i = 1; i < size; i++) {
} } if (arr[i] < min) {
} } min = arr[i];
int main() { int main() { }
int num; int n; if (arr[i] > max) {
cout << "Enter an integer: "; cout << "Enter a positive integer: "; max = arr[i]; } } }
cin >> num; cin >> n; int main() {
cout << "The absolute value of " << num << " is " << cout << "Factorial of " << n << " is " << int arr[100], size, min, max;
absoluteValue(num) << endl; factorial(n) << endl; cout << "Enter the size of the array: ";
return 0; return 0; cin >> size;
} } cout << "Enter the elements of the array: ";
for (int i = 0; i < size; i++) {
2.Height conversion 7.Circle area premeter cin >> arr[i];
#include <iostream> #include <iostream> }
using namespace std; using namespace std; findMinMax(arr, size, min, max);
double cmToMeter(double cm) { const double PI = 3.14159; cout << "Minimum number in the array: "
return cm / 100.0; double circleArea(double radius) { << min << endl;
} return PI * radius * radius; cout << "Maximum number in the array: "
int main() { } << max << endl;
double height; double circlePerimeter(double radius) { return 0;
cout << "Enter height in cm: "; return 2 * PI * radius; }
cin >> height; }
cout << "Height in meters: " << int main() { 11.Sort ascending
cmToMeter(height) << endl; double radius; #include <iostream>
return 0; cout << "Enter the radius of the circle: "; using namespace std;
} cin >> radius; void bubbleSort(int arr[], int n) {
cout << "Area of the circle: " << for(int i = 0; i < n - 1; i++) {
3.time conversation circleArea(radius) << endl; for(int j = 0; j < n - i - 1; j++) {
#include <iostream> cout << "Perimeter of the circle: " << if(arr[j] > arr[j+1]) {
using namespace std; circlePerimeter(radius) << endl; int temp = arr[j];
double minutesToHours(double minutes) { return 0; arr[j] = arr[j+1];
return minutes / 60.0; } arr[j+1] = temp;} } } }
} int main() {
int main() { 8.Prime or not int n;
double time; #include <iostream> cout << "Enter the size of the array: ";
cout << "Enter time in minutes: "; using namespace std; cin >> n;
cin >> time; bool isPrime(int n) { int arr[n];
cout << "Time in hours: " << if (n <= 1) { cout << "Enter the elements of the array: ";
minutesToHours(time) << endl; return false; for(int i = 0; i < n; i++) {
return 0; } cin >> arr[i];
} for (int i = 2; i <= n/2; i++) { }
if (n % i == 0) { // Call the bubbleSort function to sort the
4.swap fun return false; array
#include <iostream> } bubbleSort(arr, n);
using namespace std; } // Display the sorted array
void swap(int& a, int& b) { return true; cout << "The sorted array in ascending
int temp = a; } order is: ";
a = b; int main() { for(int i = 0; i < n; i++) {
b = temp; int n; cout << arr[i] << " ";
} cout << "Enter a positive integer: "; }
int main() { cin >> n; cout << endl;
int num1, num2; if (isPrime(n)) {
cout << "Enter two integers: "; cout << n << " is a prime number" << endl; return 0;
cin >> num1 >> num2; } else { }
cout << "Before swapping: num1 = " << num1 cout << n << " is not a prime number" <<
<< ", num2 = " << num2 << endl; endl; 12.calulate Birth
swap(num1, num2); } #include <iostream>
cout << "After swapping: num1 = " << num1 return 0; #include <ctime>
<< ", num2 = " << num2 << endl; } using namespace std;
return 0; int main() {
} 9.exponetial time_t now = time(0);
#include <iostream> tm *ltm = localtime(&now);
5.Sum of odd Int #include <cmath>
#include <iostream> using namespace std; int year, month, day;
using namespace std; int exponential(int base, int exponent) { cout << "Enter your birth year: ";
int sumOfOddIntegers(int n) { return pow(base, exponent); cin >> year;
int sum = 0; } cout << "Enter your birth month: ";
for (int i = 1; i <= n; i += 2) { int main() { cin >> month;
sum += i; int base, exponent; cout << "Enter your birth day: ";
} cout << "Enter the base: "; cin >> day;
return sum; cin >> base; int age = ltm->tm_year + 1900 - year;
} cout << "Enter the exponent: "; if (ltm->tm_mon + 1 < month || (ltm-
cin >> exponent; >tm_mon + 1 == month && ltm->tm_mday <
int main() { cout << base << " raised to the power of " << day)) {
int n; exponent << " is " << exponential(base, age--;
cout << "Enter a positive integer: "; exponent) << endl; }
cin >> n; return 0; cout << "Your age is: " << age << endl;
cout << "Sum of all odd integers less than or } return 0;
equals to " << n << " is " << sumOfOddIntegers(n) << endl; }
return 0;
}