You are on page 1of 4

National University of Sciences and Technology (NUST)

SMME

LAB REPORT

COURSE : Fundamentals of Programming

SUBMITTED BY : Altaf Ahmad (250282)

SUBMITTED TO: Ahmad Subhani

SECTION : ME-10 A

DATE : December 13,2018


Program#1:
Create a calculator for addition, subtraction, multiplication and division for two numbers
using functions:
Code:
Description:
The program calculates sum, difference, product and quotient of two numbers which
are entered by the user according to the operator selected by the user.
Explanation:
After loading the preprocessor directives and standard libraries. At the start of the
program, 5 function prototypes (for addition, subtraction, multiplication, division and operator)
are defined so the program can know that these functions are actually defined somewhere in
the program. The function for sum is add, for difference is sub, for multiplication is mult and for
division the function is division. All these functions take 2 integer variables. The fifth function
select takes 3 variables, a character and two integer variables. The character variable is for
operator and the integer variables are for the user to enter their desired values.
In the main function, two integer variables a and b are defined, and a character variable
named x. the program asks the user to enter the two numbers and are stored respectively in a
and b. Then the program asks the user to enter the operator. This operator is stored in x. The
next statement is an if statement which states that if the operator is ‘/’ (division operation) and
the second number is 0, then the program should output that division by zero is impossible.
And the program prints “your answer is: “, which is a result of the function select. The function
first takes the operator, then the first number and then the second number as this is the format
for the function select.
Then the functions are defined one by one. The first function is an integer function add
which returns the sum of two numbers a and b. Similarly, the 2nd function sub returns the
difference of a and b. Third function mult returns the product of a and b. Fourth function
division returns the quotient of a and b. The fifth function select is the major function of the
program which basically does the operations. In select function, a switch function is used to
check which operator is entered by the user and according to that, the respective function is
used to perform the respective operation. And so the program works in this way.
Output:
Explanation:
In the output, the program at first asks the user to enter two numbers and then the
operator, the program then performs the given function according to the operator entered, and
shows the answer. If a user tries to divide a number by 0, then the program tells the user that
division by 0 is not possible.

You might also like