Professional Documents
Culture Documents
BCSL 032
BCSL 032
#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
cout<< "Enter n: ";
cin>> n;
for (int i = 1; i < n; i += 2)
{
sum += i;
}
#include <iostream>
using namespace std;
int main()
{
int f = 0, s = 1, t, n;
Write a C++ program to create class named Account. Derive Current_Account and Saving_Account
classes from it. Define method display-balance() in both the classes. Make necessary assumptions
wherever required.
Question 2(a):
Write a C++ program to demonstrate exception handling by using example of multiplication of two
matrices.
Question 2(b):
Write C++ program for adding two matrices by overloading ‘+’ operator. Make necessary assumptions
wherever required.
#include <iostream>
using namespace std;
classMatrixType
{
int matrix[30][30];
introw,col;
public:
MatrixType(int, int);
void display();
voiddisplaySum(MatrixType);
~MatrixType()
{}
};
MatrixType::MatrixType(int r, int c)
{
row = r;
col = c;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
cout<< "Enter data in ["<<i<<"]["<<j<<"] :";
cin>> matrix[i][j];
}
}
}
voidMatrixType::display()
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
cout<< matrix[i][j] <<"\t";
}
cout<<endl;
}
}
voidMatrixType::displaySum(MatrixType t)
{
if(row == t.row&& col == t.col)
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
cout<< matrix[i][j] + t.matrix[i][j] <<"\t";
}
cout<<endl;
}
}
else
{
cout<< "Unequal matrices. Addition can't be done!!";
}
}
int main()
{
cout<<"Matrix A "<<endl;
MatrixTypeA(2,4);
A.display();
cout<<"\nMatrix B "<<endl;
MatrixTypeB(2,4);
B.display();
cout<<"sum of matrices \n";
A.displaySum(B);
return 0;
}