0% found this document useful (1 vote)
310 views

Laboratory Report of The Basic Arithmetic Operations Using C++

This paper contains a post-laboratory report regarding a C++ program done to perform arithmetic operations namely, addition, subtraction, multiplication and division.

Uploaded by

Roschelle Eleda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
310 views

Laboratory Report of The Basic Arithmetic Operations Using C++

This paper contains a post-laboratory report regarding a C++ program done to perform arithmetic operations namely, addition, subtraction, multiplication and division.

Uploaded by

Roschelle Eleda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DE LA SALLE UNIVERSITY

LBYEC2A

LABORATORY ACTIVITY #9

FINAL REPORT

CANO, ANTON S.

ELEDA, ROSCHELLE E.

EW1/F 11:00-14:00

Rating:____

MRS. ARMIE PAKZAD

Instructor
I. Problem
Write a function oriented program that accepts two inputted numbers and outputs
their sum, difference, product, and quotient depending on the choice of the user.
II. Solution
a. Algorithm
1. Create 4 functions that return the sum, difference, product and quotient
respectively and declare them as float.
- a = num1 + num 2
- a = num1 – num2
- a = num1 * num2
- a = num1 / num2
2. Declare the variables one and two as float; and letter as character.
3. Input the two numbers.
4. Input the letter which corresponds to the operation to be used.
5. Call upon the corresponding function.
6. Print the answer.
b. Flowchart
c. Source code
# include<stdio.h>

float add(float num1, float num2);


float subtract(float num1, float num2);
float mult(float num1, float num2);
float divide(float num1, float num2);

main()
{
float one;
float two;
char letter;
char ans;
printf("This is a simple calculator program\nIt will perform simple
arithmetic operations");
do
{
printf("\nInput first number: ");
scanf("%f", &one);
printf("\nInput second number: ");
scanf("%f", &two);
printf("a. add\nb. subtract\nc. multiply\nd. divide\n");
fflush(stdin);
printf("Select the letter of the operation to be performed: ");
scanf("%c", &letter);
fflush(stdin);
if ((letter=='a')|| (letter=='A'))
{
printf("\nThe sum of %.2f and %.2f is %.2f", one, two, add(one,
two));
}
else if ((letter=='b')|| (letter=='B'))
{
printf("\nThe difference of %.2f and %.2f is %.2f", one, two,
subtract(one, two));
}
else if ((letter=='c')|| (letter=='C'))
{
printf("\nThe product of %.2f and %.2f is %.2f", one, two,
mult(one, two));
}
else if ((letter=='d')|| (letter=='D'))
{
printf("\nThe quotient of %.2f and %.2f is %.2f", one, two,
divide(one, two));
}
fflush(stdin);
printf("\nDo you want to perform another operation?(Y/N) ");
scanf("%c", &ans);
}
while ((ans == 'y')||(ans=='Y'));
}
float add(float num1, float num2)
{
float a;
a = num1 + num2;
return a;
}
float subtract(float num1, float num2)
{
float a;
a = num1 - num2;
return a;
}
float mult(float num1, float num2)
{
float a;
a = num1 * num2;
return a;
}
float divide(float num1, float num2)
{
float a;
a = num1 / num2;
return a;
}
III. Output
IV. Discussion
The program uses a do while loop syntax which allows the program to run repeatedly for
as long as the user answers yes when asked if he/she wants to continue.
The first thing the program does is introduce itself to the user as a simple calculator
printf("This is a simple calculator program\nIt will perform simple arithmetic
operations");
Next the program will ask the user to input two numbers that will be used in the arithmetic
operation and select which arithmetic operation to perform
printf("\nInput first number: "); prompt
scanf("%f", &one);
printf("\nInput second number: "); prompt
scanf("%f", &two);
printf("a. add\nb. subtract\nc. multiply\nd. divide\n"); choices for arithmetic operations
fflush(stdin);
printf("Select the letter of the operation to be performed: ");
scanf("%c", &letter);
Depending on the selected operation, the program will then call upon a specific user-defined
function
if ((letter=='a')|| (letter=='A'))
{
printf("\nThe sum of %.2f and %.2f is %.2f", one, two, add(one, two));
addition function
}
else if ((letter=='b')|| (letter=='B'))
{
printf("\nThe difference of %.2f and %.2f is %.2f", one, two, subtract(one, two));
subtraction
function
}
else if ((letter=='c')|| (letter=='C'))
{
printf("\nThe product of %.2f and %.2f is %.2f", one, two, mult(one, two));
multiplication
function
}
else if ((letter=='d')|| (letter=='D'))
{
printf("\nThe quotient of %.2f and %.2f is %.2f", one, two, divide(one, two));
division function
}
else
{
Printf(“\nInvalid Input”);
}
The user-defined functions are located outside the main program. They accept two inputted
floats and perform arithmetic operations on them, returning the result. The following are the
user-defined functions performing specific arithmetic operations:

float add(float num1, float num2)


{
float a; adds two numbers
a = num1 + num2;
return a;
}
float subtract(float num1, float num2)
{
float a;
a = num1 - num2;
return a; subtracts two numbers
}
float mult(float num1, float num2)
{
float a;
a = num1 * num2; multiplies two numbers
return a;
}
float divide(float num1, float num2)
{
float a; divides two numbers
a = num1 / num2;
return a;
}
The program will then ask the user if s/he wants to do another operation and begin again if the
user says will ask the user to input another 2 numbers
printf("\nDo you want to perform another operation?(Y/N) ");
scanf("%c", &ans);
}
while ((ans == 'y')||(ans=='Y')); looping condition
V. Conclusion
We have concluded that the use of functions can greatly decrease the difficulty in creating
more complex programs. Although it may seem trivial to use functions in a simple calculator
app like this one, it will be very useful for programs that require a lot of repetition. It saves
space in the main program and makes “copy-pasting” a series of commands easier. This allows
us to make the program more organized and user friendly for future editing.
We didn’t really encounter any problems while constructing this program since it was a
relatively simple one; however, we did learn about and understand the value of user-defined
functions in making a neat and easy to understand program.

VI. References
Pakzad, A. (2018). Functions. Retrieved from: edmodo.com

You might also like