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

Programming in C++ 062 Laboratory Sheet 1

Modular Programming Revisited I:


1. Peruse the code given below: #include <cstdlib> #include <iostream> using namespace std; int main() { int N1, N2, Sum, Diff; cout << "Please Enter The First Number: "; cin >> N1; cout << "Please Enter The Second Number: "; cin >> N2; Sum = N1 + N2; Diff = N1 - N2; cout << "The Sum Of The Two Numbers Is: "; cout << Sum << endl; cout << "The Difference Of The Two Numbers Is: "; cout << Diff << endl; system("PAUSE"); return 0; } The output of the above code is given below: Please Enter The First Number: 4 Please Enter The Second Number: 5 The Sum Of The Two Numbers Is: 9 The Difference Of The Two Numbers Is: -1 Press any key to continue Rewrite the above program by including 2 user-defined functions Add() and Difference() without affecting the output of the program. Add() and Difference() functions should contain their respective calculations and displays. Use global variables to solve the problem. foad, Curtin University, Sarawak 1

2. Study the code given below: #include <cstdlib> #include <iostream> using namespace std; int main() { double N1, N2, Product, Divide; cout << "Please Enter The First Number: "; cin >> N1; cout << "Please Enter The Second Number: "; cin >> N2; Product = N1 * N2; Divide = N1 / N2; cout << "The Product Of The Two Numbers Is: "; cout << Product << endl; cout << "The Division Result For The Two Numbers Is: "; cout << Divide << endl; system("pause"); return 0; } A sample output of the above code is given below: Please Enter The First Number: 4 Please Enter The Second Number: 5 The Product Of The Two Numbers Is: 20 The Division Result For The Two Numbers Is: 0.8 Press any key to continue Rewrite the above program by including 2 user-defined functions Multiply() and Div() without affecting the output of the program. Multiply() and Div() functions should contain their respective calculations and displays. Use Functions with arguments to solve the problem. Ensure that you do not use global variables.

foad, Curtin University, Sarawak

You might also like