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

GCD and Matrix Programs

The document contains C++ code for calculating the Greatest Common Divisor (GCD) using both recursive and iterative methods, allowing user input for two numbers. It also includes a Matrix class that supports reading, displaying, adding, multiplying, and transposing matrices, along with a menu-driven interface for user interaction. The code demonstrates fundamental programming concepts such as recursion, iteration, and object-oriented programming.

Uploaded by

shchimanocha2006
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)
10 views

GCD and Matrix Programs

The document contains C++ code for calculating the Greatest Common Divisor (GCD) using both recursive and iterative methods, allowing user input for two numbers. It also includes a Matrix class that supports reading, displaying, adding, multiplying, and transposing matrices, along with a menu-driven interface for user interaction. The code demonstrates fundamental programming concepts such as recursion, iteration, and object-oriented programming.

Uploaded by

shchimanocha2006
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

7a.

GCD with Recursion (User Input)

#include <iostream>
using namespace std;

int gcdRecursive(int a, int b) {


if (b == 0)
return a;
return gcdRecursive(b, a % b);
}

int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "GCD (recursive) of " << a << " and " << b << " is: " << gcdRecursive(a, b)
<< endl;
return 0;
}

7b. GCD without Recursion (User Input)

#include <iostream>
using namespace std;

int gcdIterative(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "GCD (iterative) of " << a << " and " << b << " is: " << gcdIterative(a, b)
<< endl;
return 0;
}

8. Matrix Class Menu Program

#include <iostream>
using namespace std;

class Matrix {
int a[10][10], r, c;
public:
void read() {
cout << "Enter rows and columns: ";
cin >> r >> c;
cout << "Enter elements:
";
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
cin >> a[i][j];
}
void display() {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
cout << a[i][j] << " ";
cout << endl;
}
}
Matrix add(Matrix m) {
Matrix temp;
if (r != m.r || c != m.c) {
cout << "Matrix dimensions do not match for addition.
";
temp.r = temp.c = 0;
} else {
temp.r = r;
temp.c = c;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
temp.a[i][j] = a[i][j] + m.a[i][j];
}
return temp;
}
Matrix multiply(Matrix m) {
Matrix temp;
if (c != m.r) {
cout << "Matrix dimensions incompatible for multiplication.
";
temp.r = temp.c = 0;
} else {
temp.r = r;
temp.c = m.c;
for (int i = 0; i < r; i++)
for (int j = 0; j < m.c; j++) {
temp.a[i][j] = 0;
for (int k = 0; k < c; k++)
temp.a[i][j] += a[i][k] * m.a[k][j];
}
}
return temp;
}
Matrix transpose() {
Matrix temp;
temp.r = c;
temp.c = r;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
temp.a[j][i] = a[i][j];
return temp;
}
};

int main() {
Matrix m1, m2, result;
int choice;
cout << "Enter first matrix:
";
m1.read();
cout << "Enter second matrix:
";
m2.read();

do {
cout << "\nMenu:\n1. Add\n2. Multiply\n3. Transpose First Matrix\n4. Exit\nEnter
choice: ";
cin >> choice;
switch (choice) {
case 1:
result = m1.add(m2);
cout << "Resultant Matrix:\n";
result.display();
break;
case 2:
result = m1.multiply(m2);
cout << "Resultant Matrix:\n";
result.display();
break;
case 3:
result = m1.transpose();
cout << "Transpose Matrix:\n";
result.display();
break;
}
} while (choice != 4);

return 0;
}

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