Assigment Programming
Assigment Programming
Registration number
Konzo, Eliya
NIT/BSCIT/2013/--
Cleophace, Patric
NIT/BSCIT/2013/--
Question 1;
Write a program to implement 3 classes which perform the following tasks.
(a) Addition & Subtraction
(b) Multiplication
(c) Division and remainder
Solution;
#include <iostream>
using namespace std;
//a class to perform Addition and subtraction
class AddSub {
private:
double a, b;
public:
double add() {
return a + b;
}
double sub() {
return a - b;
}
void setvalues(double,double);
};
//a class to perform multiplication
class Mult {
private:
double a, b;
public:
double operate() {
return a * b;
}
void setvalues(double,double);
};
//a class to perform division and provide a remainder
class Div {
private:
int a, b;
public:
int devide() {
return a / b;
}
int rem() {
return a%b;
}
void setvalues(int,int);
};
//body definitions for setvalues() method for each above classes
void AddSub::setvalues(double m,double n) {
a = m;
b = n;
}
void Mult::setvalues(double m, double n) {
a = m;
b = n;
}
void Div::setvalues(int m, int n) {
a = m;
b = n;
}
//The main class which defines the main entry of thee application
int main()
\n";
AddSub addsub;
Mult mult;
Div div;
double a, b;
int choice;
cout << "Enter a number of a task you wish to perform... \n";
cout<<" 1. Addition\n 2. Substraction\n 3. Multiplication\n 4. Division\n 5. Quit program.\n
cin >> choice;
if (choice<5&&choice>0) {
cout << "enter your first number.. \n";
cin >> a;
cout << "enter your second number.. \n";
cin >> b;
cout << endl;
}
switch (choice)
{
case 1:
addsub.setvalues(a, b);
cout<<a<< " plus "<<b<<" equals "<<addsub.add()<<endl;
break;
case 2:
addsub.setvalues(a, b);
cout << a << " minus " << b <<" equals "<< addsub.sub() << endl;
break;
case 3:
mult.setvalues(a, b);
cout << a << " multiplied by " << b << " equals "<< mult.operate() << endl;
break;
case 4:
div.setvalues(a, b);
cout << a << " divided by " << b << " equals " << div.devide();
if (div.rem()) {
cout << " remainig " << div.rem() << endl;
}
else {
cout << endl;
}
break;
case 5:
return 0;
default:
cout << "\n Invalid operation choice please try again\n";
}
cout << "\n\n\n\n Choose a different option or 5 to quit... \n ";
return main();
1. Choice screen, the user will be given a screen showing available tasks and
will choose a desired task by pressing a button on screen.
The output will be: 45 plus 60 equals 105 then the program will display the
choices again.
3. If a person chooses 2(Subtraction) he/she will be prompted to enter a first
then second number. In this sample the user have entered 60 and 45 as first
and second numbers respectively.
The output will be: 60 minus 45 equals 15 then the program will display the
choices again.
4. If a person chooses 3(Multiplication) he/she will be prompted to enter a first
then second number. In this sample the user have entered 60 and 45 as first
and second numbers respectively.
The output will be: 60 multiplied by 45 equals 2700 then the program will
display the choices again.
Question 2;
Write a program to implement 2 classes which will compute the following tasks
(a) Factorial of a number
(b) Quadratic equation
Solution;
#include <iostream>
using namespace std;
//Fist class fact to handle factorial (reclusively)
class Fact {
public:
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
};
//Second class for quadratic
class Quad {
private:
double a, b, c,root;
public:
double check_imaginary() {
return root >= 0;
}
double quad_pos()
{
return ((-b) + sqrt(root)) / (2 * a);
}
double quad_nag()
{
return ((-b) - sqrt(root)) / (2 * a);
}
void set_values();
};
//Body definition for set values in the Quad class
void Quad::set_values() {
cout << "Enter a \n";
cin >> a;
cout << "Enter b \n";
cin >> b;
cout << "Enter c \n";
cin >> c;
root = (pow(b, 2)) - (4 * a*c);
}
//main class defines the entry point of the application
int main()
{
Quad quad;
Fact fact;
int n;
int choice;
return main();
The output will be: The factorial is 24 then the program will display the
choices again.
3. If a person chooses 2(Quadratic) he/she will be prompted with how to format
his/her equation then enter values of a b and c. In this sample the user have
entered 1, 3 and -4 as first, second and third numbers respectively.
The output will be: the value of x is -4 or 1 then the program will display the
choices again.
4. If a person chooses any number less than 1 or greater than 3 the program
will display a message invalid operation choice please try again.
5. If the user chooses 3. The program will quit immediately.
Question 3;
Write a program to compute series of even & odd numbers from 0-30; using user
defined functions
Solution;
#include<iostream>
using namespace std;
//custom function even to provide even numbers taking one parameter max of type int
void even(int max) {
for (int i = 1; i <= max; i++) {
if (i % 2 == 0) {
cout << i << ", ";
}
}
}
//custom function odd to provide odd numbers taking one parameter max of type int
void odd(int max) {
for (int i = 0; i <= max; i++) {
if (i % 2 != 0) {
cout << i << ", ";
}
}
}
//application entry
int main()
{
cout << "This program will list even and odd numbers from 0 to 30\n\n\n";
cout << "Even numbers are.. ";
even(30);
cout << "\nAnd Odd numbers are... ";
odd(30);
cout << endl;
system("pause");
}
return 0;
10
Question 4;
Write a program to implementing function which will compute the temperature in
Celsius [Assuming the input temperature is farenheight]
Solution;
#include<iostream>
#include <iostream>
using namespace std;
//a function to accept Fahrenheit (double) and return Celsius (double)
double centigrade(double f) {
return (f - 32) * 5 / 9;
}
int main()
{
double f, c;
cout << "Enter Fahrenheit Temperature \n";
cin >> f;
c = centigrade(f);
cout << "The temprature is " << c <<" degree Celsius\n\nEnter 1 to convert another
temprature or anything to quit\n";
int choice;
cin >> choice;
if (choice == 1) {
cout << endl << endl;
return main();
}
}
return 0;
the program will display output The temperature is 100 degree Celsius;
2. The program will then ask the user to choose if he/she wants to convert
another temperature, pressing 1 will restart the application, any other key will
end the application.
11