0% found this document useful (0 votes)
14 views17 pages

EX - 6

The document contains multiple C++ programs that demonstrate various functionalities such as generating multiplication tables, counting even and odd numbers in an array, reversing arrays, swapping elements, summing matrix elements, and performing matrix multiplication. Each program prompts the user for input and displays the results based on the operations performed. The examples illustrate basic programming concepts including loops, arrays, and conditional statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views17 pages

EX - 6

The document contains multiple C++ programs that demonstrate various functionalities such as generating multiplication tables, counting even and odd numbers in an array, reversing arrays, swapping elements, summing matrix elements, and performing matrix multiplication. Each program prompts the user for input and displays the results based on the operations performed. The examples illustrate basic programming concepts including loops, arrays, and conditional statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

#include <iostream>

using namespace std;

int main()

int n;

cout<<"enter the table : ";

cin>>n;

int x[10]={1,2,3,4,5,6,7,8,9,10};

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

cout<<" "<<x[i]*n<<endl;

return 0;

enter the table : 5

10

15

20

25

30

35

40

45

50
#include<iostream>

using namespace std;

int main() {

int n;

cout << "Enter the number of elements in the array: ";

cin >> n;

int arr[n];

int evenCount = 0, oddCount = 0;

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

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

cin >> arr[i];

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

if(arr[i] % 2 == 0) {

evenCount++;

} else {

oddCount++;
}

cout << "Number of even elements: " << evenCount << endl;

cout << "Number of odd elements: " << oddCount << endl;

return 0;

Enter the number of elements in the array: 2

Enter 2 elements: 2

Number of even elements: 1

Number of odd elements: 1


#include <iostream>

using namespace std;

int main()

int i,j,n,m;

cout<<"enter n :";

cin>>n;

cout<<"enter m :";

cin>>m;

int x[n];

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

cin>>x[i];

int y[n];

for(int j=0; j<m; j++)

cin>>y[j];

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

for(int j=0; j<m; j++)

{
if (x[i]==y[j])

cout<<" "<<x[i];

return 0;

enter n :2

enter m :3

4
#include<iostream>

using namespace std;

int main() {

int i,j,n;

cout<<"enter the size of array = ";

cin>>n;

int x[n];

int y[n];

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

cin>>x[i];

cout<<"The original array is ";

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

cout<<x[i]<<" ";

cout<<endl;

for(i=0,j=(n-1);i<n;i++,j--)

y[j]=x[i];

cout<<"the reversed array is ";


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

cout<<y[i]<<" ";

cout<<endl;

return 0;

enter the size of array = 3

The original array is 1 2 3

the reversed array is 3 2 1


#include <iostream>

using namespace std;

int main() {

int N;

cout << "Enter the size of the array: ";

cin >> N;

int arr[N];

cout << "Enter the elements of the array: " << endl;

for(int i = 0; i < N; i++) {

cin >> arr[i];

if(N > 1) {

int temp = arr[0];

arr[0] = arr[N - 1];

arr[N - 1] = temp;
}

cout << "Array after swapping first and last elements: " << endl;

for(int i = 0; i < N; i++) {

cout << arr[i] << " ";

return 0;

Enter the size of the array: 6

Enter the elements of the array:

Array after swapping first and last elements:

623451
#include <iostream>

using namespace std;

int main() {

int M, N;

cout << "Enter the number of rows (M): ";

cin >> M;

cout << "Enter the number of columns (N): ";

cin >> N;

int matrix[M][N];

cout << "Enter elements of the matrix: " << endl;

for(int i = 0; i < M; i++) {

for(int j = 0; j < N; j++) {

cin >> matrix[i][j];

}
int sum = 0;

for(int i = 0; i < M; i++) {

for(int j = 0; j < N; j++) {

sum += matrix[i][j];

cout << "The sum of the elements of the matrix is: " << sum << endl;

return 0;

Enter the number of rows (M): 2

Enter the number of columns (N): 2

Enter elements of the matrix:

The sum of the elements of the matrix is: 10


#include <iostream>

using namespace std;

int main()

int r,c;

cout<<"enter number of rows=";

cin>>r;

cout<<"enter number of coloums=";

cin>>c;

int x[r][c];

cout<<"enter the elements of 2D array of size="<<r<<"x"<<c<<endl;

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

for(int j=0;j<c;j++)

cin>>x[i][j];

cout<<" The elements of 2D array "<<endl;

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

for(int j=0;j<c;j++)

cout<<x[i][j]<<" "; }

cout<<endl;
} int sum=0;

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

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

sum=sum+x[i][j];

cout<<"sum of the lower triangle elements = "<<sum;

return 0;

enter number of rows=3

enter number of coloums=3

enter the elements of 2D array of size=3x3

The elements of 2D array

123

456

789

sum of the lower triangle elements = 34


#include <iostream>

using namespace std;

int main() {

const int M = 3, N = 3;

int matrix[M][N];

cout << "Enter elements of the 3x3 matrix: " << endl;

for (int i = 0; i < M; i++) {

for (int j = 0; j < N; j++) {

cin >> matrix[i][j];

int maxElement = matrix[0][0];

for (int i = 0; i < M; i++) {

for (int j = 0; j < N; j++) {

if (matrix[i][j] > maxElement) {

maxElement = matrix[i][j];

}
cout << "The largest element in the matrix is: " << maxElement << endl;

return 0;

Enter elements of the 3x3 matrix:

The largest element in the matrix is: 9


#include <iostream>

using namespace std;

int main() { int M, N, P, Q;

cout << "Enter the number of rows and columns of the first matrix (MxN): ";

cin >> M >> N;

cout << "Enter the number of rows and columns of the second matrix (PxQ): ";

cin >> P >> Q;

if (N != P) { cout << "Matrix multiplication not possible. Number of columns of the first matrix must
equal the number of rows of the second matrix." << endl;

return 0;

} int matrix1[M][N], matrix2[P][Q], result[M][Q];

cout << "Enter elements of the first matrix: " << endl;

for (int i = 0; i < M; i++) {

for (int j = 0; j < N; j++) cin >> matrix1[i][j];

} cout << "Enter elements of the second matrix: " << endl;

for (int i = 0; i < P; i++) {

for (int j = 0; j < Q; j++) {

cin >> matrix2[i][j]; }

for (int i = 0; i < M; i++) { for (int j = 0; j < Q; j++) {

result[i][j] = 0; }

} for (int i = 0; i < M; i++) {

for (int j = 0; j < Q; j++) {

for (int k = 0; k < N; k++) {

result[i][j] += matrix1[i][k] * matrix2[k][j];


}

} cout << "The resultant matrix after multiplication is: " << endl;

for (int i = 0; i < M; i++) {

for (int j = 0; j < Q; j++) {

cout << result[i][j] << " ";

cout << endl;

return 0;

Enter the number of rows and columns of the first matrix (MxN): 2

Enter the number of rows and columns of the second matrix (PxQ): 2

Enter elements of the first matrix:

Enter elements of the second matrix:

The resultant matrix after multiplication is:

85

20 13

You might also like