Laboratory Report of The Basic Arithmetic Operations Using C++
Laboratory Report of The Basic Arithmetic Operations Using C++
LBYEC2A
LABORATORY ACTIVITY #9
FINAL REPORT
CANO, ANTON S.
ELEDA, ROSCHELLE E.
EW1/F 11:00-14:00
Rating:____
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>
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:
VI. References
Pakzad, A. (2018). Functions. Retrieved from: edmodo.com