To make a simple calculator in C programming which performs basic four mathematical operations, use the switch case to identify the input operator to perform required calculation then display the result
C Programming Code to Make Simple Calculate
Following is a simple C program which is a menu-driven program based on simple calculation like addition, subtraction, multiplication and division according to user's choice:
EXAMPLE C PROGRAM FOR PRIME NUMBER:
#include <stdio.h>
intmain()
{
inti,num,p=0;
printf("Please enter a number: \n");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
{
p++;
}
}
if(p==2)
{
printf("Entered number is %d "\
"and it is a prime number.",num);
}
else
{
printf("Entered number is %d "\
"and it is not a prime number.",num);
}
}
OUTPUT OF C PROGRAM FOR PRIME NUMBER:
Please enter a number: 13
Entered number is 13 and it is a prime number.
This is a calculator that resembles a typical scientific calculator ,
albeit a simpler version. In our version, we have only included the
trigonometric functions and the logarithmic functions. The reason of
creating a simpler version of the calculator is to help users to learn
the programming concepts in a gradual manner and not to confuse them
especially those who are learning to program in Visual Basic.
To design the interface, we just to need to modify the interface of the basic calculator
that we have created earlier using Visual Basic 6. In this calculator,
we have added five more buttons, they are Sin, Cos, Tan, Log and Ln.
The common trigonometric functions in Visual Basic 6 are Sin, Cos, Tan
and Atn.
a) Sin is the function that computes the value of sine of an angle in radian.
b) Cos is the function that computes the value of cosine of an angle in radian.
c) Tan is the function that computes the value of tangent of an angle in radian.
d) Atn is the function that computes the value of arc tangent of an angle in radian.
Log computes the value of logarithm to base 10 whilst Ln computes the value of natural logarithm.
An angle in degree has to be converted to radian before it can be
calculated by the above trigonometric functions. From high school
mathematics, we know that π radian is equivalent to 180°; which means 1
radian is equivalent to π divided by 180. Therefore, in order to convert
an angle x from degree to radian, we have to multiply x by (π/180).
However, there is a small problem because it is rather difficult to
obtain the precise value of π, fortunately, there is a way to do it in
VB. First of all, we know that an arc tangent of 1 will return the value
of 45° which is π/4 radian. So, to obtain the value of π, just multiply
the arc tangent of 1 with 4. Now, we are ready to enter the code for
all the above additional buttons.
For the Sin button, enter the following code:
Private Sub CmdSin_Click()
sinx = Round(Sin(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = sinx
End Sub
* The Round function is to correct the output to certain number of decimal places.
For the Cos button, enter the following code:
Private Sub CmdCos_Click()
cosx = Round(Cos(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = cosx
End Sub
For the Tan button, enter the following code:
Private Sub CmdTan_Click()
tanx = Round(Tan(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = tanx
End Sub
For the Ln button, enter the following code:
Private Sub CmdLn_Click()
panel.Caption = Log(displayValue)
End Sub
and for the Log button, enter the following code:
Private Sub CmdLog_Click()
panel.Caption = Log(displayValue) / Log(10)
End Sub
The interface is shown below: Scientific Calculator created using Visual Basic 6
Input the date of birth of a person and find his age in terms of year, month and days. using class.
Age Calculator of a Person
Code:
//for more program please visit www.programtubenotes.blogspot.in
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#define WELCOME "WELCOME TO AGE CALCULATOR"
using namespace std;
class Age
{
private:
int day,month,year;
public:
void getdata(); //input date
friend void show(Age t, Age b, Age a);//calculate age
};
void Age::getdata()
{
cin>>day>>month>>year;
}
void show(Age t, Age b, Age a)
{
if(t.day<b.day)
{
t.month--;
t.day+=30;
}
a.day=t.day-b.day;
if(t.month<b.month)
{
t.year--;
t.month+=12;
}
a.month=t.month-b.month;
a.year=t.year-b.year;
cout<<"\nYour Age is "<<a.year<<" year "<<a.month<<" month "<<a.day<<" day ";
cout<<"\n\n\nCreated By:- Mr. Maheswar Sahoo\nFor more application please contact:-maheswarsahoo47@gmail.com";
cout<<"\nThank You...\n\n\n";
}
int main()
{
system("COLOR 4C"); //for background color
Age birth;
Age today,age;
cout<<"\t\t\t\t"<<WELCOME;
cout<<"\n\n\nEnter date of today (dd mm yyyy) : ";
today.getdata();
system("COLOR 1C"); //change background color
cout<<"\nEnter date of birth (dd mm yyyy) : ";
birth.getdata();
show(today,birth,age);
return 0;
}