Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Question 1(a):

Write a C++ program for followings:

Find sum of 1+3+5+…. up to n terms

#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;
}

cout<< "Sum of series is " << sum;


return 0;
}

Generate Fibonacci series

#include <iostream>
using namespace std;

int main()
{
int f = 0, s = 1, t, n;

cout<< "Enter Number of terms of Series : ";


cin>> n;

cout<< f << " " << s << " ";

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


{
t = f + s;
cout<< t << " ";
f = s;
s = t;
}
return 0;
}
Question 1(b):

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;
}

You might also like