Professional Documents
Culture Documents
PROGRAMING]
[20F-0114_BCS-2C]
Question no.1
#include<iostream>
using namespace std;
int divide(int x, int y) {
int result = 0;
try
{
if (y==0)
{
throw y;
}
else
{
result = x / y;
cout << "The result of division is : " ;
return result;
}
}
catch (int y)
{
cout << "Can't divide by zero" << endl;
}
cout << endl;
}
float divide(float x, float y) {
int result = 0;
try
{
if (y == 0)
{
throw y;
}
else
{
result = x / y;
cout << "The result of division is : ";
return result;
}
}
catch (float y)
{
cout << "Can't divide by zero" << endl;
}
cout << endl;
}
string divide(string x, string y) {
int result = 0;
try
{
if (y == "0")
{
throw "abc";
}
else
{
return "Denominrator should string\n";
}
}
catch (string s1)
{
cout << "Can't divide by null" << endl;
}
cout << endl;
}
char divide(char x, char y) {
char result = 'char';
try
{
if (y == '0')
{
throw "abc";
}
else
{
return result;
}
}
catch (char c)
{
cout << "Can't divide by null" << endl;
}
cout << endl;
}
int main() {
int a, b;
float c, d;
string s,s1;
char v,v2;
cout << "Enter the numinator in integer : ";
cin >> a;
cout << "Enter denomirator in integer : ";
cin >> b;
cout << divide(a, b) << endl;
cout << "Enter the numinator in float : ";
cin >> c;
cout << "Enter denomirator in float : ";
cin >> d;
cout << divide(c, d) << endl;
cout << "Enter the string :";
cin >> s;
cout << "Enter the string :";
cin >> s1;
}
int subtract(int a, int b, int c, int d) {
try {
if (b == 0 && d == 0)
throw b, d;
return((a / b) - (c / d));
}
catch (int ) {
cout << "Denomiater must be a non zero element\n";
}
}
int multiply(int a, int b, int c, int d) {
try {
if (b == 0 && d == 0)
throw b, d;
return((a / b) * (c / d));
}
catch (int ) {
cout << "Denomiater must be a non zero element\n";
}
}
int divide(int a, int b, int c, int d) {
try {
if (b == 0 && d == 0)
throw b, d;
return((a / b) / (c / d));
}
catch (int ) {
cout << "Denomiater must be a non zero element\n";
}
}
void main() {
int a, b, c, d;
cout << "Enter first numinator : ";
cin >> a;
cout << "Enter first denominator : ";
cin >> b;
cout << "Enter second numinator : ";
cin >> c;
cout << "Enter second denominator : ";
cin >> d;
char choice;
cout << "Which operator you want to operat :" << endl;
cout << "Press + for addition" << endl;
cout << "Press - for subtraction" << endl;
cout << "Press * for multiplication" << endl;
cout << "Press / for division" << endl;
cin >> choice;
switch (choice) {
case '+':
cout<<"Addition of two rational numbers is :"<< add(a, b, c, d);
break;
case '-':
cout<<"subtraction of two rational numbers is : "<<subtract(a, b, c, d);
break;
case '/':
cout<<"division of two rational numbers is : "<<divide(a, b, c, d);
break;
case'*' :
cout<<"multiplication of two rational numbers is : "<<multiply(a, b, c, d);
break;
}
cout << endl;
system("pause");
Question no.3
(A)
#include<iostream>
using namespace std;
int main() {
int lowerLimit=50;
try
{
cout << "Entering the try block." << endl;
if (lowerLimit < 100)
throw exception("Lower limit violation.");
cout << "Exiting the try block." << endl;
}
catch (exception eObj)
{
cout << "Exception: " << eObj.what() << endl;
}
cout << "After the catch block" << endl;
}
(B)
#include<iostream>
using namespace std;
int main() {
int lowerLimit=150;
try
{
cout << "Entering the try block." << endl;
if (lowerLimit < 100)
throw exception("Lower limit violation.");
cout << "Exiting the try block." << endl;
}
catch (exception eObj)
{
cout << "Exception: " << eObj.what() << endl;
}
cout << "After the catch block" << endl;
}
Question no.4
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
int len, len1;
float ft, ft1;
cout << "Enter the lengths in feets : ";
cin >> len;
cout << "Enter the length in inches :";
cin >> len1;
try {
}
ft = len * 30.48;
ft1 = len1 * 2.54;
float total = 0;
cout << "length of feet in centimeters : " << ft << endl;
cout << "length of inches in centimeters : " << ft1 << endl;
total = ft1 + ft;
cout << "Total length in centimeters " << total << endl;
system("pause");
}
Question no.5
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class invalidHr : public exception
public:
};
class invalidMin : public exception
{
public:
};
class invalidSec : public exception
public:
};
int getHours();
int getMinutes();
int getSeconds();
//Declare variables
int hours;
int minutes;
int seconds;
string str;
hours = getHours();
minutes = getMinutes();
seconds = getSeconds();
return 0;
int getHours()
//declare hours
int hr = 0;
do
try
throw invalidHr();
done = true;
}
} while (!done);
//return hours
return hr;
int getMinutes()
int min = 0;
do
try
throw invalidMin();
done = true;
} while (!done);
return min;
int getSeconds()
int sec = 0;
do
try
{
throw invalidSec();
done = true;
} while (!done);
return sec;
if (str == "AM")
{
if (hr == 12)
cout << 0;
else
cout << ":" << min << ":" << sec << endl;
if (hr == 12)
else
cout << ":" << min << ":" << sec << endl;
}
}
Question no.6
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class invalidDay {
string msg;
public:
invalidDay() {
void showException()
};
class invalidMonth
string msg;
public:
invalidMonth() //constructor.
};
public:
};
{
cout << "Enter day:";
cin >> d;
if (d <= 0 || d > 31)
throw invalidDay(); //Exception.
cout << "Enter month:";
cin >> m;
if (m <= 0 || m >= 13)
throw invalidMonth();
cout << "Enter year: ";
cin >> y;
if (y % 400 == 0 || (y != 100 && y % 4 == 0))
if (d >= 30)
throw leapYear();
}
int main()
{
string months[12] = {
"January","February","March","April","May","June","July","August","September",
"October","November","December" };
try {
cout << "Date of Birth " << months[month - 1] << " " << day << "," << year;
catch (invalidDay d)
{
d.showException();
catch (invalidMonth m) {
m.showException();
}
catch (leapYear l) {
l.showException();
}
cout << endl;
system("pause");
return 0;