Loading...
Wednesday 26 July 2017

Simple calculator program in C Language

This program is a simple calculator in C programming language, This calculator was created during teaching a lesson on conditions in C in some Institute. This program has tree modes 1. Standard Mode , Scientific Mode and Programmer Mode. 
In Standard of Calculator , users are allowed to perform add , subtract, divide , multiply and modulus operations. and In Scientific Mode contains operations like sin, cos, tan etc. This program uses stdio.h, and math.h header files, and some of the following functions:
1. main(): starting function of C Program, it is written in all c Programs, without it , c programs can not run. void keyword is optional which indicate main will not return any value
2. // single line comment 
3. /* multi line comments */
4. two integer type variables , two character type variable and one float type variable 
5. clrscr(): function to clear screen. 
6. printf() function to show output result on screen. 
7. Escape sequence characters in printf() like \n and \t
8. getch(): a function which gets singe invisible character from keyboard. 
9. exit(): function to exit from program 
10. if else conditions 
11. while loop 
12. do while loop

 

#include<math.h>
void main()
{ //Start block of main
int val1,val2;
float f;
char choice,op; /*choice for choosing modes
and op for choosing any calculation operation.
*/
clrscr();
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("\t\t<<<<<<<<<<<<<<--SCIENTIFIC CALCULATOR-->>>>>>>>>>>>\n\n");
printf("\n\t\t\t*****MADE BY SIR RASHID ALI*****\n");
printf("\n\t\t\tInstructor: Aptech Computer Education\n");
printf("\n\t\t\tShahra-e-Faisal Brach Karachi\n\n\n\n\n\n\n\n\n\n");
printf("\tPress any key to continue");
getch();
clrscr();
printf("\n\n\tSelect any Mode of Calculator\n\n");
printf("\t[1]\tStandard Mode\n");
printf("\t[2]\tScientific Mode\n");
printf("\t[3]\tProgrammer Mode\n\n");
printf("\tOr Press e to exit\n\n");
printf("\tEnter Your Choice:");
choice=getch();
if(choice=='e')
{
exit(0);
}
else if(choice=='1')
{   //Start Standard Mode
clrscr();
printf("\n\tWELCOME TO STANDARD MODE OF CALCULATOR\n\n");
printf("\tSelect Any Option:\n\n\n");
printf("\t[1]\tAddition\n");
printf("\t[2]\tSubstraction\n");
printf("\t[3]\tMultiplication\n");
printf("\t[4]\tDivision\n");
printf("\t[5]\tExit\n\n");
printf("\tYour Choice:");
op=getch();
clrscr();
if(op=='5')
{
printf("\n\n\n\n\n\n\n\n\n\n\n");
printf("\t######## THANK YOU FOR USING THIS CALCULATOR ########");
printf("\n\n\t\t\tPress any key to exit");
getch();
exit(0);
}
else if(op=='1')
{   // Start If Block (Addition Operation)
do
{    // Start do block
printf("\n\tYour Selection is Addition\n\n");
printf("\tEnter Value1:");
scanf("%d",&val1);
printf("\n\tEnter Value2:");
scanf("%d",&val2);
printf("\n\t%d+%d=%d",val1,val2,val1+val2);
printf("\n\n\tPress c to continue, any other key to exit:");
} // End do block
while(getch()=='c');
} // End If Block (Addition Operation)

else if(op=='2')
{   // Start If Block (Substraction Operation)
do
{    // Start do block
printf("\n\tYour Selection is Substraction\n\n");
printf("\tEnter Value1:");
scanf("%d",&val1);
printf("\n\tEnter Value2:");
scanf("%d",&val2);
printf("\n\t%d-%d=%d",val1,val2,val1-val2);
printf("\n\n\tPress c to continue, any other key to exit:");
} // End do block
while(getch()=='c');
} // End If Block (Substraction Operation)
else if(op=='3')
{   // Start If Block (Multiplication Operation)
do
{    // Start do block
printf("\n\tYour Selection is Multiplication\n\n");
printf("\tEnter Value1:");
scanf("%d",&val1);
printf("\n\tEnter Value2:");
scanf("%d",&val2);
printf("\n\t%d*%d=%d",val1,val2,val1*val2);
printf("\n\n\tPress c to continue, any other key to exit:");
} // End do block
while(getch()=='c');
} // End If Block (Multiplication Operation)

else if(op=='4')
{   // Start If Block (Division Operation)
do
{    // Start do block
printf("\n\tYour Selection is Division\n\n");
printf("\tEnter Value1:");
scanf("%d",&val1);
printf("\n\tEnter Value2:");
scanf("%d",&val2);
printf("\n\t%d/%d=%d",val1,val2,val1/val2);
printf("\n\n\tPress c to continue, any other key to exit:");
} // End do block
while(getch()=='c');
} // End If Block (Division Operation)
else {
printf("\n\n\n\n\n\n\t\tWrong Choice");
printf("\n\n\t\tRestart Calculator\n");
printf("\n\n\n\t=======Press any key to exit =======");
getch();
}





} // End Standard Mode

else if(choice=='2')
{ // Start Scientif Mode
clrscr();
printf("\n\tWELCOME TO SCIENTIFIC MODE OF CALCULATOR\n\n");
printf("\tSelect Any Option:\n\n\n");
printf("\t[1]\tSin\n");
printf("\t[2]\tCos\n");
printf("\t[3]\tTan\n");
printf("\t[4]\tabs\n");
printf("\t[5]\tsqrt\n");
printf("\t[6]\texp\n");
printf("\t[7]\tpow\n");
printf("\t[8]\tExit\n");
printf("\n\n\tYour Choice:");
op=getch();
clrscr();
if(op=='8')
{
printf("\n\n\n\n\n\n\n\n\n\n\n");
printf("\t######## THANK YOU FOR USING THIS CALCULATOR ########");
printf("\n\n\t\t\tPress any key to exit");
getch();
exit(0);
}
else if(op=='1')
{   // Start If Block (Sin)
do
{    // Start do block
printf("\n\tYour Selection is Sin\n\n");
printf("\tEnter any Value:");
scanf("%f",&f);
printf("\n\tSin of %2f is %f",f,sin(f));
printf("\n\n\tPress c to continue, any other key to exit:");
} // End do block
while(getch()=='c');
} // End If Block (Sin)

else if(op=='2')
{   // Start If Block (Cos)
do
{    // Start do block
printf("\n\tYour Selection is Cos\n\n");
printf("\tEnter any value:");
scanf("%f",&f);
printf("\n\tCos of %f is %f",f,cos(f));
printf("\n\n\tPress c to continue, any other key to exit:");
} // End do block
while(getch()=='c');
} // End If Block (Cos)
else if(op=='3')
{   // Start If Block (Tan)
do
{    // Start do block
printf("\n\tYour Selection is Tan\n\n");
printf("\tEnter any Value1:");
scanf("%f",&f);
printf("\n\tCos of %f is %f",f,tan(f));
printf("\n\n\tPress c to continue, any other key to exit:");
} // End do block
while(getch()=='c');
} // End If Block (tan)

else if(op=='4')
{   // Start If Block (abs)
do
{    // Start do block
printf("\n\tYour Selection is abs\n\n");
printf("\tEnter any value:");
scanf("%f",&f);
printf("\n\tAbsolute value of %f is %f",f,abs(f));
printf("\n\n\tPress c to continue, any other key to exit:");
} // End do block
while(getch()=='c');
} // End If Block (abs)

else if(op=='5')
{   // Start If Block (sqrt)
do
{    // Start do block
printf("\n\tYour Selection is sqrt\n\n");
printf("\tEnter any Value1:");
scanf("%f",&f);
printf("\n\tsqrt of %f is %f",f,sqrt(f));
printf("\n\n\tPress c to continue, any other key to exit:");
} // End do block
while(getch()=='c');
} // End If Block (sqrt)

else if(op=='6')
{   // Start If Block (exp)
do
{    // Start do block
printf("\n\tYour Selection is exp\n\n");
printf("\tEnter any value:");
scanf("%f",&f);
printf("\n\tResult is:\t %f",exp(f));
printf("\n\n\tPress c to continue, any other key to exit:");
} // End do block
while(getch()=='c');
} // End If Block (exp)

else if(op=='7')
{   // Start If Block (exp)
do
{    // Start do block
printf("\n\tYour Selection is pow\n\n");
printf("\n\tEnter any number for base");
scanf("%d",&val1);
printf("\n\tEnter any number for power");
scanf("%d",&val2);
printf("\n\tResult is:\t %d",pow(val1,val2));
printf("\n\n\tPress c to continue, any other key to exit:");
} // End do block
while(getch()=='c');
} // End If Block (exp)



else {
printf("\n\n\n\n\n\n\t\tWrong Choice");
printf("\n\n\t\tRestart Calculator\n");
printf("\n\n\n\t=======Press any key to exit =======");
getch();
}






}  // End Scientific Mode
else if(choice=='3')
{  // Start Programmer Mode
}  // End Programmer Mode
else
printf("\n\tWrong Choice");



}  // End Main


C Programming simple calculator
Calculator Program in C 

0 Comments:

Post a Comment

 
TOP