Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program using C language Finally, we put it all together with the main()

ID: 650612 • Letter: W

Question

Write a program using C language

Finally, we put it all together with the main() in the file calc.c implementing the Driver Module (the algorithm is given there) to create the calculator called calc when compiled with

To get an idea of how the final program will work and what kinds of functions we will need, we start with a high level algorithm for the calculator. This is only an algorithm, we will not even begin to code this algorithm until much later.

The idea of the algorithm comes directly from the spec. The program gets the first character of input; if it is not 'q' (for quit) it must be the beginning of an operand so it gets the operand, and then gets an operator. If the operator is not '=', then it should get another operand, compute the result of applying the operator to the two operands, saving the result as a new first operand, and get another operator. If that is not '=', it gets the next operand, computes and saves. It continues like this until the operator is '=', when it displays the result. After the '=' operator, any characters read are skipped until the newline, at which time it checks for quit and repeats the whole process. So it sounds like we have two nested loops here; the outer one processing an entire expression, and the inner one processing an operand/operator pair, accumulating the result. Here is the algorithm:

get the first character

while not quitting

get the first operand (this is the result so far)

get the first operator

while the operator is not '='

get the next operand

evaluate the operator with the 2 operands

get the next operator

display the result

wait for the newline

get the first character of the next line

Explanation / Answer

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

#define KEY "Enter the calculator Operation you want to do:"

// Function prototype declaration
void addition();
void subtraction();
void multiplication();
void division();
void modulus();
void power();
int factorial();
void calculator_operations();

// Start of Main Program
int main()
{
int X=1;
char Calc_oprn;

// Function call
calculator_operations();

while(X)
{
printf(" ");
printf("%s : ", KEY);

Calc_oprn=getche();

switch(Calc_oprn)
{
case '+': addition();
break;

case '-': subtraction();
break;

case '*': multiplication();
break;

case '/': division();
break;

case '?': modulus();
break;

case '!': factorial();
break;

case '^': power();
break;

case 'H':
case 'h': calculator_operations();
break;

case 'Q':
case 'q': exit(0);
break;
case 'c':
case 'C': system("cls");
calculator_operations();
break;

default : system("cls");

printf(" **********You have entered unavailable option");
printf("*********** ");
printf(" *****Please Enter any one of below available ");
printf("options**** ");
calculator_operations();
}
}
}

//Function Definitions

void calculator_operations()
{
//system("cls"); use system function to clear
//screen instead of clrscr();
printf(" Welcome to C calculator ");

printf("******* Press 'Q' or 'q' to quit ");
printf("the program ******** ");
printf("***** Press 'H' or 'h' to display ");
printf("below options ***** ");
printf("Enter 'C' or 'c' to clear the screen and");
printf(" display available option ");

printf("Enter + symbol for Addition ");
printf("Enter - symbol for Subtraction ");
printf("Enter * symbol for Multiplication ");
printf("Enter / symbol for Division ");
printf("Enter ? symbol for Modulus ");
printf("Enter ^ symbol for Power ");
printf("Enter ! symbol for Factorial ");
}

void addition()
{
int n, total=0, k=0, number;
printf(" Enter the number of elements you want to add:");
scanf("%d",&n);
printf("Please enter %d numbers one by one: ",n);
while(k<n)
{
scanf("%d",&number);
total=total+number;
k=k+1;
}
printf("Sum of %d numbers = %d ",n,total);
}

void subtraction()
{
int a, b, c = 0;
printf(" Please enter first number : ");
scanf("%d", &a);
printf("Please enter second number : ");
scanf("%d", &b);
c = a - b;
printf(" %d - %d = %d ", a, b, c);
}

void multiplication()
{
int a, b, mul=0;
printf(" Please enter first numb : ");
scanf("%d", &a);
printf("Please enter second number: ");
scanf("%d", &b);
mul=a*b;
printf(" Multiplication of entered numbers = %d ",mul);
}

void division()
{
int a, b, d=0;
printf(" Please enter first number : ");
scanf("%d", &a);
printf("Please enter second number : ");
scanf("%d", &b);
d=a/b;
printf(" Division of entered numbers=%d ",d);
}

void modulus()
{
int a, b, d=0;
printf(" Please enter first number : ");
scanf("%d", &a);
printf("Please enter second number : ");
scanf("%d", &b);
d=a%b;
printf(" Modulus of entered numbers = %d ",d);
}

void power()
{
double a,num, p;
printf(" Enter two numbers to find the power ");
printf("number: ");
scanf("%lf",&a);

printf("power : ");
scanf("%lf",&num);

p=pow(a,num);

printf(" %lf to the power %lf = %lf ",a,num,p);
}

int factorial()
{
int i,fact=1,num;

printf(" Enter a number to find factorial : ");
scanf("%d",&num);

if (num<0)
{
printf(" Please enter a positive number to");
printf(" find factorial and try again. ");
printf(" Factorial can't be found for negative");
printf(" values. It can be only positive or 0 ");
return 1;
}   

for(i=1;i<=num;i++)
fact=fact*i;
printf(" ");
printf("Factorial of entered number %d is:%d ",num,fact);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote