C language |
In this project, we are going to create a simple scientific calculator using the C programming language.
Table of Contents
Prerequisite!
If you are new to C programming language and you want to design more program to practice your new acquired skills, then this project is for you.
Also if you are not learning C programming language you can also try to build your own scientific calculator for fun sake.
What to Expect!
The scientific calculator program will be able to perform the following computation: Addition, Subtraction, Multiplication, Division, Square root, Powers, Cosine (cos), Sine (sin), Tangent (tan), arc-cosine (acos), arc-sine (asin), arc-tangent (atan), Exponential (e), Natural Log, and Base 10 log.
The scientific calculator program will use the math functions of C programming language.
This program will not use any user graphics interface, hence the codes are simple and easy to follow
Lets Begin!!!
Build A Simple Scientific Calculator prototype Using C Programming Language |
Step 1:
The scientific calculator program was coded using the code::blocks IDE. Hence visit the Code::Blocks website to download and install the software on your device.
Step 2:
Now that you have the Code::Blocks IDE, open an "Empty file" and save the file with any name should like, (avoid spaces) after the name add the .c extension.
Step 3:
Now type the following C header functions as shown below in your new file. Without these functions, the rest of the program will not work.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
Step 4:
Type in the main function and open its curly brackets, as shown below. Note that all codes from now on will be typed inside these curly brackets; if not the code will note work
main()
{
}
Now define all the variables to be use in this program and initialize some of them.
float a, b;
float ans = 0;
float ans2;
float ans3 = 1;
int i, choice1, choice2;
Step 5:
Create a simple user interface: This will require a printf and few escape sequence (You don't really need to know what this mean, if you are not into C programming). The code is below
printf("To Perform any calculations pick the number ");
printf("holding the function\n");
printf("\n1. Addition\t\t2. Subtraction\t\t3. Division");
printf("\n4. multiplication\t5.Square root\t\t6. Powers");
printf("\n7. Cosine(cos)\t\t8. Sine(sin)\t\t9. Tangent(tan)");
printf("\n10. arc-cosine(acos)\t11. arc-sine(asin)\t12. arc-tangent(atan)");
printf("\n13. Exponential\t\t14. Natural log\t\t15. Base 10 log");
printf("\n16. Quit");
Step 6:
Create a loop that will hold the calculator computation. The loop will control the calculator program to ensure it functions well. Note That from now on, all codes will be written in-between the do-while curly brackets as shown below:
do
{
} while ((choice1 < 1) || (choice1 > 15));
return (0);
Step 7:
All the calculations will be done using the switch case function. Now first write the switch code for the calculator: Note that all the codes hence fort will be written in-between the switch curly bracket as shown below.
printf("\n\nTake a number: ");
scanf(" %d", &choice1);
switch(choice1)
{
}
Please don't get confuse, the curly brackets so far is like this:
- All codes except for the C function headers are written inside the main() function curly brackets.
- the do-while loop is inside the main function but also has its own curly brackets
- the switch is inside the do-while loop but also contains its own curly brackets to hold the case
- The summary of the above is shown below: Please do not type the code below
main()
{
do
{
switch()
{
}
} while
return (0);
}
Step 8:
Type the following block of case code to perform the 15 different calculations that is available to the user; and the case (16): will exit the program when selected without performing any calculation. all these code should be inside the switch curly brackets.
case(1): //Case to perform the addition of numbers
{
printf("\n\nHow many numbers do you want to add: ");
scanf(" %d", &choice2);
for (i = 0; i < choice2; i++)
{
printf("\n\tPut your number: ");
scanf(" %f", &a);
ans += a;
printf("total addition: %.1f\n", ans);
}
if (i == choice2) //This code ensures the (for) completes the number of computations (choice2) required before ending
{
break;
}
}
case(2): // Case to perform Subtraction
{
printf("\n\nHow many numbers do you want to subtract: ");
scanf(" %d", &choice2);
printf("What is the first number: ");
scanf(" %f", &ans2);
for (i = 0; i < choice2; i++)
{
printf("\n\tPut your other number: ");
scanf(" %f", &a);
ans2 -= a;
printf("total subtraction: %.1f\n", ans2);
}
if (i == choice2)
{
break;
}
}
case (3): // Case to perform division
{
printf("\n\nNOTE: This program can only divide two numbers!");
printf("\n\nInput first number: ");
scanf(" %f", &a);
printf("Input second number: ");
scanf(" %f", &b);
ans = a / b;
printf("\n\n%.2f / %.2f = %.2f\n", a, b, ans);
break;
}
case (4): // Case to perform multiplication
{
printf("\n\nHow many numbers do you want to multiply: ");
scanf(" %d", &choice2);
for(i = 0; i < choice2; i++)
{
printf("\n\tPut a number: ");
scanf(" %f", &a);
ans3 *= a;
printf("Total multiplication is: %.1f\n", ans3);
}
if (i == choice2)
{
break;
}
}
case (5): // Case to perform square root
{
printf("\n\nFind the square root of which number?: ");
scanf(" %f", &a);
printf("\nThe square root of %.1f is %.1f\n", a, sqrt(a));
break;
}
case (6): // Case to perform Power
{
printf("\n\nInput the base first before the superscript(power)");
printf("\n\tEnter the base: ");
scanf(" %f", &a);
printf("\tBase raised to the power of what: ");
scanf(" %f", &b);
printf("%.1f raised to the power of %.1f is %.1f\n", a, b, pow(a, b));
break;
}
case (7): // Case to perform cosine
{
printf("\n\nNOTE: You are to input the angle number without adding degree");
printf("\n\tEnter number: ");
scanf(" %f", &a);
printf("\nThe cosine for a %.1f-degree angle is %.1f\n", a, cos((a*(3.14159/180))));
break;
}
case (8): // Case to perform Sine
{
printf("\n\nNOTE: You are to input the angle number without adding degree");
printf("\n\tEnter number: ");
scanf(" %f", &a);
printf("\nThe sine for a %.1f-degree angle is %.1f\n", a, sin((a*(3.14159/180))));
break;
}
case (9): // Case to perform tangent
{
printf("\n\nNOTE: You are to input the angle number without adding degree");
printf("\n\tEnter number: ");
scanf(" %f", &a);
printf("\nThe tangent for a %.1f-degree angle is %.1f\n", a, tan((a*(3.14159/180))));
break;
}
case (10): // Case to perform acos
{
printf("\n\nNOTE: You are to input the angle number without the degree");
printf("\n\tEnter number: ");
scanf(" %f", &a);
printf("\nThe arc-cosine for a %.1f-degree angle is %.1f\n", a, acos((a*(3.14159/180))));
break;
}
case (11): // Case to perform asin
{
printf("\n\nNOTE: You are to input the angle number without the degree");
printf("\n\tEnter number: ");
scanf(" %f", &a);
printf("\nThe arc-sine for a %.1f-degree angle is %.1f\n", a, asin((a*(3.14159/180))));
break;
}
case (12): // Case to perform atan
{
printf("\n\nNOTE: You are to input the angle number without the degree");
printf("\n\tEnter number: ");
scanf(" %f", &a);
printf("\nThe arc-tangent for a %.1f-degree angle is %.1f\n", a, atan((a*(3.14159/180))));
break;
}
case (13): // case to perform expontenial
{
printf("\n\nNOTE: You are to input the value you want the expontenial");
printf("\n\tEnter number: ");
scanf(" %f", &a);
printf("\ne raised to %.1f is %.3f\n", a, exp(a));
break;
}
case (14): // Case to perform natural log
{
printf("\n\nNOTE: You are to input the value needed for natural log");
printf("\n\tEnter number: ");
scanf(" %f", &a);
printf("\nThe natural log of %.1f is %.3f\n", a, log(a));
break;
}
case (15): // Case to perform Base 10 log
{
printf("\n\nNOTE: You are to input the value needed for base-10 log");
printf("\n\tEnter number: ");
scanf(" %f", &a);
printf("\nThe base-10 log of %.1f is %.3f\n", a, log10(a));
break;
}
case (16): // Case to quit without performing any calculation
exit(1);
Step 9:
Type the default code that will be displayed if the user dose not select any of the available options from 1 to 16. These chuck of code is still found inside the switch curly brackets.
default: printf("\n%d is not a valid choice. \n", choice1);
printf("Try again.\n");
break;
Now we are done with writing Codes. You can test your program by clicking on the yellow wrench icon with a green play button as shown in the image below:
Image 2 |
The video below will show you the full code and how the program works.
Don't forget to check our C programming page for more programming projects.
Follow us on our social media handles to stay updated.