## Monday, March 17, 2014

### Simple + Scientific Calculator

So here comes a simple plus scientific calculator. Code is a little long but interesting and easy to understand. Calculations done using only loops.

```#include <iostream.h>

#include <math.h>

void simple_cal();

void scientific_cal();

void subtraction();

void multiplication();

void division();

void average();

void percentage();

void power();                                      //Prototypes of functions used

void root();

void sine();

void cosine();

void tangent();

void cosecant();

void secant();

void cotangent();

void log_nat();

void log_com();

void main()                                      //main function

{

}

{

int n;

cout<< "\n\n-Enter 1 if you want to use a Simple Calculator.\n";

cout<< "-Enter 2 if you want to use a Scientific Calculator.\n\n";

cin>>n;

if (n==1)

simple_cal();                              //calling simple calculator function

else if (n==2)

scientific_cal();                          //calling scientific calculator function

}

void simple_cal()                           //function for simple calculator

{

int a;

clrscr();

cout<< "\n\t\t\t**** SIMPLE CALCULATOR ****\n";

cout<< "-Enter 2 for subtraction.\n";

cout<< "-Enter 3 for multiplication.\n";

cout<< "-Enter 4 for division.\n";

cout<< "-Enter 5 for average.\n";                                                   //menu of simple calcuator

cout<< "-Enter 6 for Percentage.\n";

cout<< "-Enter 7 for the power.\n";

cout<< "-Enter 8 for root.\n\n";

cin>>a;

if (a==1)

else if (a==2)

subtraction();                  //calling subtraction function

else if (a==3)

multiplication();               //calling multiplication function

else if (a==4)

division();                     //calling division function

else if (a==5)

average();                      //calling average function

else if (a==6)

percentage();                   //calling percentage function

else if (a==7)

power();                        //calling power function

else if (a==8)

root();                         //calling root function

}

void scientific_cal()                              //funtion for scientific calculator

{

int a;

clrscr();

cout<< "\n\t\t\t**** SCIENTIFIC CALCULATOR ****\n";

cout<< "\n\n-Enter 1 if you want to find value of Sine Function of an angle.\n";

cout<< "-Enter 2 if you want to find value of Cosine Function of an angle.\n";

cout<< "-Enter 3 if you want to find value of Tangent Function of an angle.\n";

cout<< "-Enter 4 if you want to find value of Cosecant Function of an angle.\n";              //menu of scientific calculator

cout<< "-Enter 5 if you want to find value of Secant Function of an angle.\n";

cout<< "-Enter 6 if you want to find value of Cotangent Function of an angle.\n";

cout<< "-Enter 7 if you want to find value of Natural Logarithm of a number.\n";

cout<< "-Enter 8 if you want to find value of Common Logarithm of a number.\n\n";

cin>>a;

if (a==1)

sine();                              //calling sine function

else if (a==2)

cosine();                            //calling cosine function

else if (a==3)

tangent();                           //calling tangent function

else if (a==4)

cosecant();                          //calling cosecant function

else if (a==5)

secant();                            //calling secant function

else if (a==6)

cotangent();                         //calling cotangent function

else if (a==7)

log_nat();                           //calling natural log function

else if (a==8)

log_com();                           //calling common log function

}

{

float a,sum=0.0;

clrscr();

cout<< "\nEnter the numbers to be added.\n\n\t==>Enter 0 to complete the adddition.\n\n";

cin>>a;

while(a!=0)

{

sum=sum+a;

cin>>a;

}

cout<< "The sum of given numbers is:\t"<<sum<<endl<<endl;

getch();

}

void subtraction()                                        //function for subtraction

{

float a,difference=0.0;

clrscr();

cout<< "\n\t\t\t\t*** SUBTRACTION ***\n";

cout<< "\nEnter the numbers.\n\n\t==>Enter 0 if you want to complete the subtraction.\n\n";

cin>>a;

while(a!=0)

{

difference=difference-a;

cin>>a;

a=-1*a;

}

difference=-1*difference;

cout<< "The difference of given numbers is:\t"<<difference<<endl<<endl;

getch();

}

void multiplication()                              //function for multiplication

{

float a,product=1.0;

clrscr();

cout<< "\n\t\t\t\t*** MULTIPLICATION ***\n";

cout<< "\nEnter the numbers to be multiplied.\n\n\t==>Enter 1 if you want to complete the multiplication.\n\n";

cin>>a;

while(a!=1)

{

product=product*a;

cin>>a;

}

cout<< "The product of given numbers is:\t"<<product<<endl<<endl;

getch();

}

void division()                              //function for division

{

float a,b,division;

clrscr();

cout<< "\n\t\t\t\t*** DIVISION ***\n";

cout<< "\nEnter the DIVIDENT.\n\n";

cin>>a;

cout<< "Enter the DIVISOR.\n\n";

cin>>b;

division=a/b;

cout<< "The result is:\t"<<division<<endl<<endl;

getch();

}

void average()                              //function for average

{

float a,sum=0.0,average;

int i=0;

clrscr();

cout<< "\n\t\t\t\t*** AVERAGE ***\n";

cout<< "\nEnter the numbers.\n\n\t==>Enter 0 if you want to complete the calculation.\n\n";

cin>>a;

while(a!=0)

{

i++;

sum=sum+a;

cin>>a;

}

average=sum/i;

cout<< "The average of given numbers is:\t"<<average<<endl<<endl;

getch();

}

void percentage()                      //function for percentage

{

float a,b,percentage;

clrscr();

cout<< "\n\t\t\t\t*** PERCENTAGE ***\n";

cout<< "\nEnter the total.\n\n";

cin>>a;

cout<< "Enter the number whose percentage value is to be found.\n\n";

cin>>b;

percentage=(b/a)*100;

cout<< "The percentage is:\t"<<percentage<<"%"<<endl<<endl;

getch();

}

void power()                          //function for power

{

float a,b,power;

clrscr();

cout<< "\n\t\t\t\t*** POWER ***\n";

cout<< "\nEnter the base.\n\n";

cin>>a;

cout<< "Enter the power.\n\n";

cin>>b;

power=pow (a,b);

cout<< "The entered power of the given number is:\t"<<power<<endl<<endl;

getch();

}

void root()                        //function for root

{

float a,b,root;

clrscr();

cout<< "\n\t\t\t\t*** ROOT ***\n";

cout<< "\nEnter the number whose root is to be calculated.\n\n";

cin>>a;

cout<< "Enter the root.\n\n";

cin>>b;

b=1/b;

root=pow (a,b);

cout<< "The entered root of the given number is:\t"<<root<<endl<<endl;

getch();

}

void sine()                              //function for sine

{

int n;

float a,sine_d,sine_r,PI=3.141592654;

clrscr();

cout<< "\n\t\t\t\t*** SINE VALUE ***\n";

cout<< "\nEnter 1 if angle is in degrees.\n";

cout<< "Enter 2 if angle is in radians.\n\n";

cin>>n;

if (n==1)

{

clrscr();

cout<<"\n\t\t*** SINE VALUE WHEN ANGLE IS IN DEGREES ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

sine_d=sin(a*PI/180);

cout<<"The value of SINE Function of given angle is:\t"<<sine_d<<endl<<endl;

}

else if (n==2)

{

clrscr();

cout<<"\n\t\t*** SINE VALUE WHEN ANGLE IS IN RADIANS ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

sine_r=sin(a);

cout<<"The value of SINE Function of given angle is:\t"<<sine_r<<endl<<endl;

}

getch();

}

void cosine()                     //function foe cosine

{

int n;

float a,cosine_d,cosine_r,PI=3.141592654;

clrscr();

cout<<"\n\t\t\t\t*** COSINE VALUE ***\n";

cout<<"\nEnter 1 if angle is in degrees.\n";

cout<<"Enter 2 if angle is in radians.\n\n";

cin>>n;

if (n==1)

{

clrscr();

cout<<"\n\t\t*** COSINE VALUE WHEN ANGLE IS IN DEGREES ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

cosine_d=cos(a*PI/180);

cout<<"The Value of COSINE Function of given angle is:\t"<<cosine_d<<endl<<endl;

}

else if (n==2)

{

clrscr();

cout<<"\n\t\t*** COSINE VALUE WHEN ANGLE IS IN RADIANS ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

cosine_r=cos(a);

cout<<"The value of COSINE Function of given angle is:\t"<<cosine_r<<endl<<endl;

}

getch();

}

void tangent()                    //function for tangent

{

int n;

float a,tangent_d,tangent_r,PI=3.141592654;

clrscr();

cout<<"\n\t\t\t\t*** TANGENT VALUE ***\n";

cout<<"\nEnter 1 if angle is in degrees.\n";

cout<<"Enter 2 if angle is in radians.\n\n";

cin>>n;

if (n==1)

{

clrscr();

cout<<"\n\t\t*** TANGENT VALUE WHEN ANGLE IS IN DEGREES ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

tangent_d=tan(a*PI/180);

cout<<"The Value of TANGENT Function of given angle is:\t"<<tangent_d<<endl<<endl;

}

else if (n==2)

{

clrscr();

cout<<"\n\t\t*** TANGENT VALUE WHEN ANGLE IS IN RADIANS ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

tangent_r=tan(a);

cout<<"The value of TANGENT Function of given angle is:\t"<<tangent_r<<endl<<endl;

}

getch();

}

void cosecant()                    //function for cosecant

{

int n;

float a,cosecant_d,cosecant_r,PI=3.141592654;

clrscr();

cout<<"\n\t\t\t\t*** COSECANT VALUE ***\n";

cout<<"\nEnter 1 if angle is in degrees.\n";

cout<<"Enter 2 if angle is in radians.\n\n";

cin>>n;

if (n==1)

{

clrscr();

cout<<"\n\t\t*** COSECANT VALUE WHEN ANGLE IS IN DEGREES ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

cosecant_d=1/sin(a*PI/180);

cout<<"The value of the COSECANT Function of given angle is:\t"<<cosecant_d<<endl<<endl;

}

else if (n==2)

{

clrscr();

cout<<"\n\t\t*** COSECANT VALUE WHEN ANGLE IS IN RADIANS ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

cosecant_r=1/sin(a);

cout<<"The value of the COSECANT Function of the given angle is:\t"<<cosecant_r<<endl<<endl;

}

getch();

}

void secant()              //function for secant

{

int n;

float a,secant_d,secant_r,PI=3.141592654;

clrscr();

cout<<"\n\t\t\t\t*** SECANT VALUE ***\n";

cout<<"\nEnter 1 if angle is in degrees.\n";

cout<<"Enter 2 if angle is in radians.\n\n";

cin>>n;

if (n==1)

{

clrscr();

cout<<"\n\t\t*** SECANT VALUE WHEN ANGLE IS IN DEGREES ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

secant_d=1/cos(a*PI/180);

cout<<"The value of the SECANT Function of given angle is:\t"<<secant_d<<endl<<endl;

}

else if (n==2)

{

clrscr();

cout<<"\n\t\t*** SECANT VALUE WHEN ANGLE IS IN RADIANS ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

secant_r=1/cos(a);

cout<<"The value of the SECANT Function of the given angle is:\t"<<secant_r<<endl<<endl;

}

getch();

}

void cotangent()                              //function for cotangent

{

int n;

float a,cotangent_d,cotangent_r,PI=3.141592654;

clrscr();

cout<<"\n\t\t\t\t*** COTANGENT VALUE ***\n";

cout<<"\nEnter 1 if angle is in degrees.\n";

cout<<"Enter 2 if angle is in radians.\n\n";

cin>>n;

if (n==1)

{

clrscr();

cout<<"\n\t\t*** COTANGENT VALUE WHEN ANGLE IS IN DEGREES ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

cotangent_d=1/tan(a*PI/180);

cout<<"The value of the COTANGENT Function of given angle is:\t"<<cotangent_d<<endl<<endl;

}

else if (n==2)

{

clrscr();

cout<<"\n\t\t*** COTANGENT VALUE WHEN ANGLE IS IN RADIANS ***\n";

cout<<"\nEnter the Angle.\n\n";

cin>>a;

cotangent_r=1/tan(a);

cout<<"The value of the COTANGENT Function of the given angle is:\t"<<cotangent_r<<endl<<endl;

}

getch();

}

void log_nat()                       //function for natural logarithm

{

float a,ln;

clrscr();

cout<<"\n\t\t\t\t*** NATURAL LOGARITHM ***\n";

cout<<"\nEnter the number.\n\n";

cin>>a;

ln=log(a);

cout<<"The value of Natural Logarithm of the given number is:\t"<<ln<<endl<<endl;

getch();

}

void log_com()                   //function for common logarithm

{

float a,log;

clrscr();

cout<<"\n\t\t\t\t*** COMMON LOGARITHM ***\n";

cout<<"\nEnter the number.\n\n";

cin>>a;

log=log10(a);

cout<<"The value of Common Logarithm of the given number is:\t"<<log<<endl<<endl;

getch();

}

a```