Muzamil Niaz 03

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

ASSINGMENT OF PROGRAMING

FUNDAMENTAL
Name : MUAZAMIL NIAZBAIG
Roll no: 2303
Second Semester BS( IT)
Q#1 :WRITE A CALCULATOR C++ CODE USING
FUNCTION METHOD:

SOLUTION:
#include <iostream>

using namespace std;

double calc(double m, double n, char op) {

switch(op) {

case '+':

return m + n;

case '-':

return m - n;

case '*':

return m * n;

case '/':

if (n != 0) {
return m / n;

} else {

cout << "Error" << endl;

return 0;

default:

cout << "Error" << endl;

return 0;

int main() {

char op;

double value1,value2;

char again;

do {

cout << "Enter operator (+, -, *, /) ";

cin >> op;

cout << "Enter two values: ";

cin >> value1 >> value2;

double res = calc(value1, value2, op);

cout << value1 << " " << op << " " << value2 << " = " << res << endl;
cout << "Do you want to calculate again? (yes/no): ";

cin >> again;

} while (again == 'y'|| again == 'Y');

cout << ". Goodbye!" << endl;

return 0;

You might also like