Data Structures

Develop a program to solve simple computational problems using arithmetic expressions and use of each operator leading to simulation of a commercial calculator

Develop a program to compute the roots of a quadratic equation by accepting the coefficients

ALGORITHM:

PURPOSE : To simulate a calculator using arithmetic expressions
INPUT: Enter num1 and num2
OUTPUT: Print the result of addition or subtraction or multiplication or division or modulus.
START
Step 1: [Enter first number]
read num1
Step 2: [Enter Second number]
read num2
Step 3:[Enter Choice]
read choice
Step 4:[To perform addition]
if choice is equal to plus
add num1 and num2
print result
Step 5: [To perform subtraction]
if choice is equal to minus
subtract num2 from num1
print result
Step 6: [To perform multiplication]
if choice is equal to multiplication
multiply num1 and num2
print result
Step 7: [To perform division]
if choice is equal to division
divide num1 by num2
print result
Step 8: [To perform Modulus]
if choice is equal to modulus
divide num1 by num2
print result (remainder)
STOP




FLOWCHART




PROGRAM
#include<stdio.h>
int main()
{
int num1,num2;
float result;
char choice; //to store operator choice
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Choose operation to perform (+,-,*,/,%): ");
scanf(" %c",&choice);
result=0;
switch(choice)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
case '%':
result=num1%num2;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result); 
return 0;
}




***************************************************************************
OUTPUT:
First run:
Enter first number: 10
Enter second number: 20
Choose operation to perform (+,-,*,/,%): +
Result: 10 + 20 = 30.000000
Second run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): /
Result: 10 / 3 = 3.333333
Third run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): >
Invalid operation.
Result: 10 > 3 = 0.000000

Leave a comment

Your email address will not be published. Required fields are marked *