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

Lab_Exercises_Cpp

The document contains five C++ programs demonstrating basic programming concepts. These include combining two strings, sorting an array in ascending and descending order, transposing a matrix, calculating the sum of a matrix's diagonal elements, and converting digits of a number into words. Each program includes user input and output functionalities.
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)
3 views

Lab_Exercises_Cpp

The document contains five C++ programs demonstrating basic programming concepts. These include combining two strings, sorting an array in ascending and descending order, transposing a matrix, calculating the sum of a matrix's diagonal elements, and converting digits of a number into words. Each program includes user input and output functionalities.
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/ 3

1.

Combine Two Strings

#include <iostream>
#include <string>
using namespace std;

int main() {
string str1, str2, combined;
cout << "Enter first string: ";
cin >> str1;
cout << "Enter second string: ";
cin >> str2;
combined = str1 + str2;
cout << "Combined String: " << combined << endl;
return 0;
}

2. Ascending & Descending Order

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int arr[100], n;
cout << "Enter number of elements: ";
cin >> n;
cout << "Enter elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];

sort(arr, arr + n);


cout << "Ascending Order: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";

cout << "\nDescending Order: ";


for (int i = n - 1; i >= 0; i--)
cout << arr[i] << " ";

return 0;
}

3. Transpose of Matrix

#include <iostream>
using namespace std;

int main() {
int mat[10][10], trans[10][10], row, col;
cout << "Enter rows and columns: ";
cin >> row >> col;

cout << "Enter matrix elements:\n";


for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
cin >> mat[i][j];

for (int i = 0; i < row; i++)


for (int j = 0; j < col; j++)
trans[j][i] = mat[i][j];

cout << "Transpose of matrix:\n";


for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++)
cout << trans[i][j] << " ";
cout << endl;
}

return 0;
}

4. Sum of Diagonal of Matrix

#include <iostream>
using namespace std;

int main() {
int mat[10][10], n, sum = 0;
cout << "Enter size of square matrix: ";
cin >> n;

cout << "Enter matrix elements:\n";


for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> mat[i][j];

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


sum += mat[i][i]; // main diagonal

cout << "Sum of diagonal elements: " << sum << endl;
return 0;
}

5. Read Value and Split Digit by Digit

#include <iostream>
#include <string>
using namespace std;

string digitToWord(char digit) {


switch (digit) {
case '0': return "zero";
case '1': return "one";
case '2': return "two";
case '3': return "three";
case '4': return "four";
case '5': return "five";
case '6': return "six";
case '7': return "seven";
case '8': return "eight";
case '9': return "nine";
default: return "";
}
}

int main() {
string number;
cout << "Enter a number: ";
cin >> number;

for (char digit : number)


cout << digitToWord(digit) << " ";

cout << endl;


return 0;
}

You might also like