You are on page 1of 4

SCHOOL OF COMPUTING AND INFORMATICS

COURSE CODE: CCC1123


COURSE: PROBLEM SOLVING and PROGRAMING FUNDAMENTALS
INDIVIDUAL ASSIGNMENT

Student name Student Id


Esmael Aliyi Uta AIU22102300
Code report: Math Calculator
The written code is a simple program that acts as a math calculator. It asks the user to enter
his/her name and two numbers and then it performs different arithmetic operations such as
addition, subtraction, multiplication and division on the numbers. Additionally, it calculates the
Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of the numbers and
displays the results to the user.
Here are the details of the code:

Header Files: The required header file “stdio.h” is included, which provides input/output
functions like „printf‟ and „scanf‟.

GCD and LCM Functions:


gcd function: It calculates the Greatest Common Divisor (GCD) of two numbers using the
Euclidean algorithm.

lcm function: It calculates the Least Common Multiple (LCM) of two numbers using the
formula “(a * b) / gcd(a, b)”.

Main Function:
Num1 and Num2: These variables of type double are used to store the two numbers entered by
the user.
Name array: This character array with a size of 50 is used to store the name of the user.
User Input: The program asks the user to enter his/her name, and it reads the name using scanf.
Then it asks the user to enter the two numbers and reads them using scanf.
Arithmetic Operations:
The program performs several arithmetic operations on the entered numbers, such as addition,
subtraction, multiplication, and division. The results are stored in sum, sub, mult, and Div
variables, respectively.
The program prints the results of these arithmetic operations using „printf‟.

GCD and LCM Calculation:


The program calls the gcd and lcm functions, passing the integer conversion of Num1 and Num2
as arguments.
The calculated GCD and LCM values are stored in gcdValue and lcmValue variables,
respectively.
The program prints the calculated GCD and LCM values along with four arithmetic operations
using printf.
Finally, the program displays a closing message to thank the user for using the Math Calculator.
It returns 0 to indicate successful execution of the program.
Code overview
I used double data type the make the code suitable for accepting any number from the user
including large numbers and decimal numbers. I limit the decimal digit to one decimal point. The
code rounds ant decimal digits to one decimal point.
As a sample input, I entered my name, Esmael Aliyi Uta, as a user name. I also entered 30 and
20 as a first number and second number respectively. Then the program made arithmetic
operations and also calculated the value of greatest common divisor (GCD) and least common
multiple (LCM) of 30 and 20 and then displayed the result as follows.
Sample output

Summary:
The code is a basic math calculator that allows the user to enter their name and two numbers. It
performs arithmetic operations on the numbers, calculates the GCD and LCM, and displays the
results. It provides a simple and interactive way for users to perform calculations.

You might also like