Explanation
The program functions as a calculator, performing basic arithmetic operations (addition, subtraction, multiplication, and division) entered by the user. It uses separate functions for each operation.
Header
The code begins with including the standard input-output header file using #include <stdio.h>
.
Function Declarations
The program declares four functions to perform arithmetic operations:
double add(double d, double m);
: Declares the function to add two numbers.double sub(double d, double m);
: Declares the function to subtract two numbers.double mul(double d, double m);
: Declares the function to multiply two numbers.double divide(double d, double m);
: Declares the function to divide two numbers.
Variable Declaration
The main()
function is defined, and several variables are declared:
double d, m
: Variables to hold the input numbers.char j
: Variable to hold the operator.double result
: Variable to hold the result of the operation.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter an arithmetic expression in the form of two numbers separated by an operator:
printf("Enter an expression in the form number operand number or (2 + 2): ");
: Prompts the user to enter the expression.scanf("%lf %c %lf", &d, &j, &m);
: Reads the expression and stores the numbers ind
andm
, and the operator inj
.
Operation and Output
The program uses a switch
statement to perform the appropriate operation based on the operator entered by the user:
switch (j)
: Checks the value of the operator.case '+'
: Calls theadd
function if the operator is '+' and stores the result inresult
.case '-'
: Calls thesub
function if the operator is '-' and stores the result inresult
.case '*'
: Calls themul
function if the operator is '*' and stores the result inresult
.case '/'
: Calls thedivide
function if the operator is '/' and stores the result inresult
.default
: Prints an error message if the operator is invalid and exits the program.printf("Result: %.2lf\n", result);
: Prints the result of the operation formatted to two decimal places.
Function Definitions
The program defines the four arithmetic functions:
double add(double d, double m)
: Defines the function to add two numbers and return the result.double sub(double d, double m)
: Defines the function to subtract two numbers and return the result.double mul(double d, double m)
: Defines the function to multiply two numbers and return the result.double divide(double d, double m)
: Defines the function to divide two numbers and return the result.
Program End
The program ends with return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
double add(double d, double m);
double sub(double d, double m);
double mul(double d, double m);
double divide(double d, double m);
int main()
{
printf("\n\n Program to make function blocks as a calculator which calculates the operations entered by the user. \n\n");
double d, m;
char j;
printf("Enter an expression in the form number operand number or (2 + 2): ");
scanf("%lf %c %lf", &d, &j, &m);
double result;
switch (j)
{
case '+':
result = add(d, m);
break;
case '-':
result = sub(d, m);
break;
case '*':
result = mul(d, m);
break;
case '/':
result = divide(d, m);
break;
default:
printf("Invalid operator.\n");
return 1;
}
printf("Result: %.2lf\n", result);
return 0;
}
// Function definitions
double add(double d, double m)
{
return d + m;
}
double sub(double d, double m)
{
return d - m;
}
double mul(double d, double m)
{
return d * m;
}
double divide(double d, double m)
{
return d / m;
}
Output
Program to make function blocks as a calculator which calculates the operations entered by the user.
Enter an expression in the form number operand number or (2 + 2): 2 * 2
Result: 4.00