Scientific Calculator in C

(For the previous post in this series (Part 6b), pls see here: Discussion on how to design scientific calculator in C.)

Answer:

Some features of our program:

  • include header file math.h for pow(), sqrt() ,etc
  • include stdio.h for printf(), scanf().
  • our scientific calculator performs the following:
    • arithmetic operations
    • square root, power
    • sin, cos
  • we use double values for input and result
  • use switch-case for processing user input and performing appropriate operation.
// Program to build a scientific calculator using built-in functions
#include <stdio.h> //for printf(, scanf())
#include <math.h> //for mathematical functions like pow(), etc
int main()
{
double m, n; //double data type allows decimal points with high precision
int choice; //choice entered by user
printf("SCIENTIFIC CALCULATOR\n");
//arithmetic operations
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
//power and square root
printf("5. Square root\n");
printf("6. Power\n");
//Trigonometic functions
printf("7. sin\n");
printf("8. cos\n");
//ask user to make a choice
printf("Which operation do you wish to perform?\n");
printf("Enter a number from 1 to 8: ");
//get input from user
scanf("%d", &choice);
//use switch-case to process user input, and perform appropriate operation
switch (choice)
{
case 1: //addition
printf("Enter 2 numbers: ");
scanf("%lf %lf", &m, &n); // format specifier %lf is for double values
printf("Sum is %.2lf \n", m+n); //round off result to 2 decimal places
break; //exit the switch block
case 2: //subtraction
printf("Enter 2 numbers: ");
scanf("%lf %lf", &m, &n);
printf("Difference is %.2lf \n", m-n);
break;
case 3: //multiplication
printf("Enter 2 numbers: ");
scanf("%lf %lf", &m, &n);
printf("Product is %.2lf \n", m*n);
break;
case 4: //division
printf("Enter 2 numbers: ");
scanf("%lf %lf", &m, &n);
if (n == 0) //check for division by zero
{
printf("Division by zero is not allowed\n");
}
else
{
printf("Quotient is %.2lf \n", m/n);
}
break;
case 5: //square root
printf("Enter a non-negative number: ");
scanf("%lf", &m);
if (m < 0) //check if number is negative
{
printf("Square root of negative number is not allowed\n");
}
else
{
printf("Square root is %.2lf \n", sqrt(m)); //use the sqrt() function from math.h
}
break;
case 6: //power or exponentiation
printf("Enter base and exponent: ");
scanf("%lf %lf", &m, &n);
printf("Power is %.2lf \n", pow(m,n)); //use the pow() function from math.h
break;
case 7: //sin
printf("Enter value in radians: ");
scanf("%lf", &m);
printf("sin value is %.2lf \n", sin(m));
break;
case 8: //cos
printf("Enter value in radians: ");
scanf("%lf", &m);
printf("cos value is %.2lf \n", cos(m));
break;
default:
printf("Invalid choice!\n");
}
printf("Thanks, have a nice day!");
return 0;
}
Sample output 1:
SCIENTIFIC CALCULATOR
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Square root
6. Power
7. sin
8. cos
Which operation do you wish to perform?
Enter a number from 1 to 8: 3
Enter 2 numbers: 2.0 3
Product is 6.00
Thanks, have a nice day!
Sample output 2:
Which operation do you wish to perform?
Enter a number from 1 to 8: 6
Enter base and exponent: 2 3
Product is 8.00
Thanks, have a nice day!
Sample output 3:
Which operation do you wish to perform?
Enter a number from 1 to 8: 7
Enter value in radians: 1.57
sin value is 1.00
Thanks, have a nice day!
Posted in

One response to “Scientific Calculator in C (C Prog Q&A – Part 6(c), CS 3251, Anna Univ Previous Years Exams)”

  1. […] Scientific Calculator in C (C Prog Q&A – Part 6(c), CS 3251, Anna Univ Previous Years&nbsp… […]

    Like

Leave a comment

Is this your new site? Log in to activate admin features and dismiss this message
Log In