# C Program to do general mathematical operations

Some of the general mathematical operations are arithmetic, exponents, logarithms, trigonometry and hyperbolics. I wrote this program not only to do these operations but also to learn C in my initial days of engineering.

## Introduction

Most commonly used operations in higher mathematics are arithmetic, exponents, logarithms, trigonometry and hyperbolics. This is a C program which can do all these operations. This program doesn't give error outputs. Also, it mentions error inputs if any. A loop is used in this program so that there is no need to compile and run the program every time an operation is to be performed.

NOTE : This program is too big but it is too easy to understand (Only for beginners of C).

## Program

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void starlinedisplay(void)
{
int a;
for(a=0;a<77;a++)
printf("*");
printf("\n ");
}
void main(void)
{
int choice1,choice2;
double a,b,c;
clrscr();
for(choice1=0;choice1<=8;choice1++)
{
printf("\n");
printf(" 1.Arithmetic ");
printf(" 2.Exponents ");
printf(" 3.Logarithms ");
printf(" 4.Trigonometry ");
printf(" 5.Inverse Trigonometry ");
printf("\n");
printf(" 6.Hyperbolics ");
printf(" 7.Inverse Hyperbolics ");
printf(" 8.Off ");
scanf("%d",&choice1);
if(choice1==1)
{
printf("\n ARITHMETIC \n\n");
printf(" 1.(x)+(y) ");
printf(" 2.(x)-(y) ");
printf(" 3.(x)*(y) ");
printf(" 4.(x)/(y) ");
scanf("%d",&choice2);
if(choice2==1)
{
printf("\n Enter {x,y} values:");
scanf("%lf,%lf",&a,&b);
c=a+b;
printf("\n (%lf)+(%lf)={%lf} \n\n ",a,b,c);
}
else if(choice2==2)
{
printf("\n Enter {x,y} values:");
scanf("%lf,%lf",&a,&b);
c=a-b;
printf("\n (%lf)-(%lf)={%lf} \n\n ",a,b,c);
}
else if(choice2==3)
{
printf("\n Enter {x,y} values:");
scanf("%lf,%lf",&a,&b);
c=a*b;
printf("\n (%lf)*(%lf)={%lf} \n\n ",a,b,c);
}
else if(choice2==4)
{
printf("\n Enter {x,y} values:");
scanf("%Lf,%Lf",&a,&b);
if(b!=0)
{
c=a/b;
printf("\n (%lf)/(%lf)={%lf} \n\n ",a,b,c);
}
else
printf("\n (%lf)/(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
}
else
printf("\n {%d} IS NOT A VALID CHOICE:TRY AGAIN \n\n ",choice2);
}
else if(choice1==2)
{
printf("\n EXPONENTS \n\n");
printf(" 1.[e]^(x) ");
printf(" 2.[10]^(x) ");
printf(" 3.[a]^(x) ");
scanf("%d",&choice2);
if(choice2==1)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
b=exp(a);
printf("\n [e]^(%lf)={%lf} \n\n ",a,b);
}
else if(choice2==2)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
b=pow(10,a);
printf("\n [10]^(%lf)={%lf} \n\n ",a,b);
}
else if(choice2==3)
{
printf("\n Enter {a} value:");
scanf("%lf",&a);
printf("\n Enter {x} value:");
scanf("%lf",&b);
if(a!=0&&b!=0)
{
c=pow(a,b);
printf("\n [%lf]^(%lf)={%lf} \n\n ",a,b,c);
}
else
printf("\n [0]^(0) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
}
else
printf("\n {%d} IS NOT A VALID CHOICE:TRY AGAIN \n\n ",choice2);
}
else if(choice1==3)
{
printf("\n LOGARITHMS \n\n");
printf(" 1.log[e](x) ");
printf(" 2.log[10](x) ");
printf(" 3.log[a](x) ");
scanf("%d",&choice2);
if(choice2==1)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(a>0)
{
b=log(a);
printf("\n log[e](%lf)={%lf} \n\n ",a,b);
}
else
printf("\n log[e](%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==2)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(a>0)
{
b=log10(a);
printf("\n log[10](%lf)={%lf} \n\n ",a,b);
}
else
printf("\n log[10](%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==3)
{
printf("\n Enter {a} value:");
scanf("%lf",&a);
printf("\n Enter {x} value:");
scanf("%lf",&b);
if(b>0&&a>0&&a!=1)
{
c=log(b)/log(a);
printf("\n log[%lf](%lf)={%lf} \n\n ",a,b,c);
}
else
printf("\n log[%lf](%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
}
else
printf("\n {%d} IS NOT A VALID CHOICE:TRY AGAIN \n\n ",choice2);
}

else if(choice1==4)
{
printf("\n TRIGONOMETRY \n\n");
printf(" 1.sin(x) ");
printf(" 2.cos(x) ");
printf(" 3.tan(x) ");
printf("\n\n");
printf(" 4.cot(x) ");
printf(" 5.sec(x) ");
printf(" 6.csc(x) ");
scanf("%d",&choice2);
if(choice2==1)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
b=sin(a);
printf("\n sin(%lf)={%lf} \n\n ",a,b);
}
else if(choice2==2)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
b=cos(a);
printf("\n cos(%lf)={%lf} \n\n ",a,b);
}
else if(choice2==3)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(fabs(sin(a))!=1)
{
b=tan(a);
printf("\n tan(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n tan(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==4)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(fabs(cos(a))!=1)
{
b=1/tan(a);
printf("\n cot(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n cot(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==5)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(fabs(sin(a))!=1)
{
b=1/cos(a);
printf("\n sec(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n sec(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==6)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(fabs(cos(a))!=1)
{
b=1/sin(a);
printf("\n csc(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n csc(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else
printf("\n {%d} IS NOT A VALID CHOICE:TRY AGAIN \n\n ",choice2);
}
else if(choice1==5)
{
printf("\n INVERSE TRIGONOMETRY \n\n");
printf(" 1.arcsin(x) ");
printf(" 2.arccos(x) ");
printf(" 3.arctan(x) ");
printf("\n\n");
printf(" 4.arccot(x) ");
printf(" 5.arcsec(x) ");
printf(" 6.arccsc(x) ");
scanf("%d",&choice2);
if(choice2==1)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(a>=-1&&a<=1)
{
b=asin(a);
printf("\n arcsin(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n arcsin(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==2)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(a>=-1&&a<=1)
{
b=acos(a);
printf("\n arccos(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n arccos(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==3)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
b=atan(a);
printf("\n arctan(%lf)={%lf} \n\n ",a,b);
}
else if(choice2==4)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
b=atan(1/a);
printf("\n arccot(%lf)={%lf} \n\n ",a,b);
}
else if(choice2==5)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(a<-1&&a>1)
{
b=acos(1/a);
printf("\n arcsec(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n arcsec(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==6)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(a<-1&&a>1)
{
b=asin(1/a);
printf("\n arccsc(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n arccsc(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else
printf("\n {%d} IS NOT A VALID CHOICE:TRY AGAIN \n\n ",choice2);
}
else if(choice1==6)
{
printf("\n HYPERBOLICS \n\n");
printf(" 1.sinh(x) ");
printf(" 2.cosh(x) ");
printf(" 3.tanh(x) ");
printf("\n\n");
printf(" 4.coth(x) ");
printf(" 5.sech(x) ");
printf(" 6.csch(x) ");
scanf("%d",&choice2);
if(choice2==1)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
b=sinh(a);
printf("\n sinh(%lf)={%lf} \n\n ",a,b);
}
else if(choice2==2)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
b=cosh(a);
printf("\n cosh(%lf)={%lf} \n\n ",a,b);
}
else if(choice2==3)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
b=tanh(a);
printf("\n tanh(%lf)={%lf} \n\n ",a,b);
}
else if(choice2==4)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(tanh(a)!=0)
{
b=1/tanh(a);
printf("\n coth(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n coth(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==5)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
b=1/cosh(a);
printf("\n sech(%lf)={%lf} \n\n ",a,b);
}
else if(choice2==6)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(sinh(a)!=0)
{
b=1/sinh(a);
printf("\n csch(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n csch(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else
printf("\n {%d} IS NOT A VALID CHOICE:TRY AGAIN \n\n ",choice2);
}
else if(choice1==7)
{
printf("\n INVERSE HYPERBOLICS \n\n");
printf(" 1.arsinh(x) ");
printf(" 2.arcosh(x) ");
printf(" 3.artanh(x) ");
printf("\n\n");
printf(" 4.arcoth(x) ");
printf(" 5.arsech(x) ");
printf(" 6.arcsch(x) ");
scanf("%d",&choice2);
if(choice2==1)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
b=log(a+sqrt(a*a+1));
printf("\n arsinh(%lf)={%lf} \n\n ",a,b);
}
else if(choice2==2)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(a>=1)
{
b=log(a+sqrt(a*a-1));
printf("\n arcosh(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n arcosh(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==3)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(a*a<1)
{
b=0.5*log((1+a)/(1-a));
printf("\n artanh(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n artanh(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==4)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(a*a>1)
{
b=0.5*log((a+1)/(a-1));
printf("\n arcoth(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n arcoth(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==5)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(a>0&&a<=1)
{
b=log((1+sqrt(1-a*a))/a);
printf("\n arsech(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n arsech(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else if(choice2==6)
{
printf("\n Enter {x} value:");
scanf("%lf",&a);
if(a!=0)
{
b=log((1+sqrt(1+a*a))/fabs(a));
printf("\n arcsch(%lf)={%lf} \n\n ",a,b);
}
else
printf("\n arcsch(%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
}
else
printf("\n {%d} IS NOT A VALID CHOICE:TRY AGAIN \n\n ",choice2);
}
else if(choice1==8)
{
printf("\n ");
starlinedisplay();
exit(0);
}
else
{
printf("\n {%d} IS AN INVALID CHOICE:TRY AGAIN \n\n ",choice1);
choice1=0;
}
starlinedisplay();
}
getch();
}

## Explanation

There are 4 header files in this program. They are:
1) stdio.h (Standard Input Output) : For functions like printf(), scanf() etc,.
2) conio.h (Conditional Input Output) : For functions like clrscr(), getch() etc,.
3) math.h : For mathematical functions like pow(), exp(), log(), log10() etc,.
4) stdlib.h (Standard Library) : For standard library functions like exit() etc,.

starlinedisplay() function
It is a function which displays a line of 77 asterisk (*) symbols. It is used only for a good user interface.

main() function
It is the function at which the execution of a C program starts.
In it, there are two integer variables declared as choice1, choice2 and six double variables declared as a, b, c. The latter set is used for performing arithmetic, logarithmic, exponential, trigonometric or hyperbolic operations. But, the former set is used to select an operation as follows.
a) 'choice1' is used to select the topic.
b) 'choice2' is used to select the operation in a topic.
An else-if ladder is used for selecting the topic i.e. value of 'choice1'.
Another else-if ladder inside the above mentioned one is used for selecting the operation i.e. value of 'choice2'. If invalid values of 'choice1' or 'choice2' are selected, the program mentions it.

Arithmetic Operations done by the program are :
1) Addition : It is done using the 'c=a+b' statement.
2) Subtraction : It is done using the 'c=a-b' statement.
3) Multiplication : It is done using the 'c=a*b' statement.
4) Division : It is done using the 'c=a/b' statement.
Error message is provided when input leads to Division By Zero.

Exponential Operations done by the program are :
1) Power of 'e' : It is done using the exp() function of math.h
2) Power of 10 : It is done using the pow() function of math.h
3) Power of any 'a' : It is also done using the pow() function of math.h
Error message is provided when input leads to Zero to the Power of Zero.

Logarithmic Operations done by the program are :
1) Natural Logarithm : It is done using the log() function of math.h
2) Logarithm to base 10 : It is done using the log10() function of math.h
3) Logarithm to any base : It is done using the log() function of math.h and logarithm properties.
Error message is provided when input leads to domain errors like logarithm to base 1 etc,.

Trigonometric Operations done by the program are :
1) Sine : It is done using the sin() function of math.h
2) Cosine : It is done using the cos() function of math.h
3) Tangent : It is done using the tan() function of math.h
4) Cotangent : It is done by calculating Multiplicative Inverse of Tangent.
5) Secant : It is done by calculating Multiplicative Inverse of Cosine.
6) Cosecant : It is done by calculating Multiplicative Inverse of Sine.
Error message is provided when input leads to domain errors.

Inverse Trigonometric Operations done by the program are :
1) Inverse Sine : It is done using the asin() function of math.h
2) Inverse Cosine : It is done using the acos() function of math.h
3) Inverse Tangent : It is done using the atan() function of math.h
4) Inverse Cotangent : It is done by giving Multiplicative Inverse of input to atan() function of math.h
5) Inverse Secant : It is done by giving Multiplicative Inverse of input to acos() function of math.h
4) Inverse Cosecant : It is done by giving Multiplicative Inverse of input to asin() function of math.h
Error message is provided when input leads to domain errors.

Hyperbolic Operations done by the program are :
1) Hyperbolic Sine : It is done using the sinh() function of math.h
2) Hyperbolic Cosine : It is also done using the cosh() function of math.h
3) Hyperbolic Tangent : It is also done using the tanh() function of math.h
4) Hyperbolic Cotangent : It is done by calculating Multiplicative Inverse of Hyperbolic Tangent.
5) Hyperbolic Secant : It is done by calculating Multiplicative Inverse of Hyperbolic Cosine.
6) Hyperbolic Cosecant : It is done by calculating Multiplicative Inverse of Hyperbolic Sine.
Error message is provided when input leads to domain errors.

Inverse Hyperbolic Operations done by the program are :
1) Inverse Hyperbolic Sine : It is done using the log() function of math.h and formula for arsinh().
2) Inverse Hyperbolic Cosine : It is also done using the log() function of math.h and formula for arcosh().
3) Inverse Hyperbolic Tangent : It is also done using the log() function of math.h and formula for artanh().
4) Inverse Hyperbolic Cotangent : It is done using the log() function of math.h and formula for arcoth().
5) Inverse Hyperbolic Secant : It is also done using the log() function of math.h and formula for arsech().
6) Inverse Hyperbolic Cosecant : It is also done using the log() function of math.h and formula for arcsch().
Error message is provided when input leads to domain errors.

This program is designed such that if you need code for any operation, you can just copy it by going through values of 'choice1' and 'choice2'. That makes the program lengthy but easily understandable to beginners. For example, if you want to write a C program to calculate sin(x):
1) Write header files required. (mostly the same ones in this program)
2) Declare double variables a,b,c,..
3) Choose 'choice1' and 'choice2' corresponding to sin(x) in this program and copy the code.

This program is adaptable i.e. new operations can be added very easily. To add a new operation, just add an else if block with the code of the operation in it and update the name of the operation in the display list present in the parent if block (used to select 'choice1').

## Output

An example output of this program is as follows.

It shows the case where 'power' operation is performed.

Hence, the C program to perform general mathematical operations. I hope this C program helps you to gain some grip on C programming as it helped me to.