Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Calculator program in C (http://www.go4expert.com/articles/calculator-program-c-t689/)

 coderzone 5Apr2006 21:26

Calculator program in C

The below code is a calculator in plain C. It does not take into account the operability of bodmas but just one operation at a time.

The main thing in the source code below in the scanf which scans the input as number symbol number just as in case of normal calculator.
Code: C

`#include<stdio.h>float add(float,float);float sub(float,float);float product(float,float);float divide(float,float);void main(){    float n1,n2;    char sym,choice;    printf("This Program is a program for calculator\n\n");    scanf("%f%c%f",&n1,&sym,&n2);    if(sym=='+')        printf("\n%f",add(n1,n2));    if(sym=='-')        printf("\n%f",sub(n1,n2));    if(sym=='*')        printf("\n%f",product(n1,n2));    if(sym=='/')        printf("%f",divide(n1,n2));    printf("\nDo you wish to continue[y/n]");    scanf("%s",&choice);    if(choice=='y'||choice=='Y')        main();}float add(float m1,float m2){    return(m1+m2);}float sub(float m1,float m2){    return(m1-m2);}float product(float m1,float m2){    return(m1*m2);}float divide(float m1,float m2){    return(m1/m2);}`

 tjweb 19Jul2006 11:20

Re: Calculator program in C

Verry good.
Would you mind joining and writting a tutorial at my forums. SEE MY SIG

People hate it when i right a tutorial. I just aint good at telling people how to do something like that.

Thanks

 coderzone 19Jul2006 11:38

Re: Calculator program in C

I would love to but I see no topics on your forums and so how you know people hate what you write. I also had something similar thinking when I started but thanks to G4EF that made me think otherwise.

 gamehunter101 19Jul2006 11:41

Re: Calculator program in C

very good stuff man

 143rocker 14Nov2006 13:33

Re: Calculator program in C

i got another option in your program

Code:

```#include <stdio.h><br> #include <conio.h><br> main()<br> {<br>   float n1,n2,s,d,p,q;<br>   clrscr();<br>   printf("\n Enter two nos");<br>   scanf("%f%f"&n1,&n2);<br>   s=n1+n2;<br>   d=n1-n2;<br>   p=n1*n2;<br>   q=n1/n2;<br>   printf("\n Sum %f",s);<br>   printf("\n Difference %f",d);<br>   printf("\n Product %f",p);<br>   printf("\n Quotient %f",q);<br>   getch();<br>  return(0);<br> }```

 143rocker 14Nov2006 13:36

Re: Calculator program in C

sorry for that reply....this is the real one

Code:

```#include <stdio.h> #include <conio.h> main() {   float n1,n2,s,d,p,q;   clrscr();   printf("\n Enter two nos");   scanf("%f%f"&n1,&n2);   s=n1+n2   d=n1-n2;   p=n1*n2;   q=n1/n2;   printf("\n Sum %f",s);   printf("\n Difference %f",d);   printf("\n Product %f",p);   printf("\n Quotient %f",q);   getch();   return(0); }```

 shanku_4ch 6May2007 08:26

Re: Calculator program in C

your program doesnt work if a space is given between operan & operator
2+5=7

but
2 + 5

doesnt work

 hanleyhansen 23Sep2008 18:47

Re: Calculator program in C

Great stuff!

 happyz 22Nov2008 19:31

Re: Calculator program in C

Nicely done

 hkp819 4Dec2008 18:12

Re: Calculator program in C

This program is very nicely done.
I like this program. I have try it. it is very good.

Re: Calculator program in C

what about a supermarket service to calculate prices of goods with fixed prices. with a cashier log in

Re: Calculator program in C

what about a supermarket service to calculate prices of goods with fixed prices. with a cashier log in

 saji777 22Jun2010 12:34

Re: Calculator program in C

tnx but i think we needed a top source from calculator
Specal thanks

 newtosee 28Jun2010 03:55

Re: Calculator program in C

Hy, I begun studying c++ a few days ago and I came across this topic and decided to write a similar program in c++...

The code was a bit inspired from the original c calculator :)

Code:

```#include <cstdlib> #include <cstdio> #include <iostream> using namespace std; double x; double y; double multi(double x, double y); double divi(double x, double y); double sub (double x, double y); double add(double x, double y); int main(int argc, char *argv[]) {     char a, z;     for(;;) {     cin>> x; cin>> z; cin>>y; if (z=='*'){cout<<"=" <<multi (x,y)<< endl;} if (z=='/'){cout<<"=" <<divi (x,y)<< endl;} if (z=='-'){cout<<"=" <<sub (x,y)<< endl;} if (z=='+'){cout<<"=" <<add (x,y)<< endl;} cout<<"Would you like to do another operation? [y/n]"; cin>>a; if (a!='y') {break;} }     system("PAUSE");     return EXIT_SUCCESS; } double multi (double x, double y) {return x*y;} double divi (double x, double y) {return x/y;} double sub (double x, double y) {return x-y;} double add (double x, double y) {return x+y;}```
Cheers!

 cute_dhanju 5Aug2010 11:27

Re: Calculator program in C

gud,....................

 atena.sharifi 12Aug2010 02:41

Re: Calculator program in C

:worried: hi ,thanks for program ,but this program in the line 25 give error . error : cannot call 'main' from within the program in function main().
sorry for level low language.

 shabbir 12Aug2010 08:32

Re: Calculator program in C

Quote:
 Originally Posted by atena.sharifi (Post 71438) :worried: hi ,thanks for program ,but this program in the line 25 give error . error : cannot call 'main' from within the program in function main(). please help me . sorry for level low language.
You are using a Cpp compiler and try a C compiler and it would work. For some old TC compiler just renaming the file to .c would compile it for you

 matuatua 7Sep2010 13:17

Re: Calculator program in C

i will try it later,anyway thank u for that code

 hemamca 13Sep2010 17:03

Re: Calculator program in C

hai do i know which editor is best now in c / c++ languages. is better Microsoft or Turbo C

 shabbir 13Sep2010 17:04

Re: Calculator program in C

TC is outdated and not used anymore.

 matuatua 13Sep2010 22:07

Re: Calculator program in C

more of this stuffs shabbir but i really do need codes in c programming to try my hands on it please i really need it badly

 hhyy6623 20Sep2010 15:18

Re: Calculator program in C

got a little problem here, it doesnt end when i input "n" where it asks me wanna countinues or not

 matuatua 21Sep2010 01:49

Re: Calculator program in C

Quote:
 Originally Posted by hhyy6623 (Post 72949) got a little problem here, it doesnt end when i input "n" where it asks me wanna countinues or not
it works perfectly cos i have tried it

 wdliming 2Oct2010 08:57

Re: Calculator program in C

but i copy the code into the wxDEV_C++,can not compile successfully
if(choice=='y'||choice=='Y')
//main();
something wrong in "main(); ",could you tell me how to modify it?thank you !!

 ratnesh 3Oct2010 10:36

Re: Calculator program in C

[quote=wdliming;73294]but i copy the code into the wxDEV_C++,can not compile successfully
if(choice=='y'||choice=='a')
//main()
then try it .

 wdliming 3Oct2010 15:18

Re: Calculator program in C

i forgot~~i wanted to say,i made a project in wxDEV_C++,and copyed the code in it,the wxDEV_C++ told me could not compile successfully;then i thought something was wrong in
"if(choice=='y'||choice=='a') main();",
as far as i concerned, the "main()" can not use again in one programme,

 wdliming 3Oct2010 15:47

Re: Calculator program in C

At last i find a new solution solve my problem which i proposed yesterday,using the "goto";code is as follows:
Code:

```#include<stdio.h> float add(float,float); float sub(float,float); float product(float,float); float divide(float,float); int main(int argc, char *argv[]) {   float n1,n2;     char sym,choice;              printf("This Program is a program for calculator\n\n");     start_again:            scanf("%f%c%f",&n1,&sym,&n2);     if(sym=='+')          printf("\n%f",add(n1,n2));   if(sym=='-')          printf("\n%f",sub(n1,n2));  if(sym=='*')        printf("\n%f",product(n1,n2));   if(sym=='/')          printf("%f",divide(n1,n2));   printf("\nDo you wish to continue[y/n]");   scanf("%s",&choice);     if(choice=='y'||choice=='Y')            goto start_again;             system("PAUSE");     return 0; } float add(float m1,float m2) {    return(m1+m2);} float sub(float m1,float m2) {    return(m1-m2);} float product(float m1,float m2) {    return(m1*m2);} float divide(float m1,float m2) {    return(m1/m2);}```

 wdliming 3Oct2010 16:33

Re: Calculator program in C

i add some functioins and program a new code that based on codezone as follows:
Code:

```#include<stdio.h> float add(float,float); float sub(float,float); float product(float,float); float divide(float,float); int main(int argc, char *argv[]) {   float n1,n2;     char sym,choice;              printf("This Program is a program for calculator\n");  printf("Please input two numbers like this:4+5 or 4*5\n");     start_again:            scanf("%f%c%f",&n1,&sym,&n2);     if(sym=='+')          printf("\n%f",add(n1,n2));   if(sym=='-')          printf("\n%f",sub(n1,n2));  if(sym=='*')        printf("\n%f",product(n1,n2));   if(sym=='/')          printf("%f",divide(n1,n2));   printf("\nDo you wish to continue[y/n]\n");  input:     scanf("%s",&choice);       if(choice=='n'||choice=='N')     {            printf("Good Luck!!ByeBye!~\n");         goto end;      }     else if(choice=='y'||choice=='Y')     {         printf("Please Contiue:\n");         goto start_again;     }  else if(choice!='n'||choice!='N'||choice!='y'||choice!='Y')     {         printf("Wrong code!\n");         printf("Input again:\n");         goto input;     }        end:     system("PAUSE");     return 0; } float add(float m1,float m2) {    return(m1+m2);} float sub(float m1,float m2) {    return(m1-m2);} float product(float m1,float m2) {    return(m1*m2);} float divide(float m1,float m2) {    return(m1/m2);}```

 Harish723 27Oct2010 09:30

Re: Calculator program in C

Ok its alright

 zaster 1Nov2010 20:25

Re: Calculator program in C

Okay, I understand its meant to be a bare bones sort of program, but still you should include a check for division by 0.

 Deucel 14Feb2011 12:26

Re: Calculator program in C

Quote:
 Originally Posted by 143rocker (Post 5548) sorry for that reply....this is the real one Code: ```#include #include main() {   float n1,n2,s,d,p,q;   clrscr();   printf("\n Enter two nos");   scanf("%f%f"&n1,&n2);   s=n1+n2   d=n1-n2;   p=n1*n2;   q=n1/n2;   printf("\n Sum %f",s);   printf("\n Difference %f",d);   printf("\n Product %f",p);   printf("\n Quotient %f",q);   getch();   return(0); }```
Thanks, and it is good, but...
To give the program some readability:

Code:

```#include <stdio.h> #include <conio.h> main() {   float n1,n2,s,d,p,q;   printf("\n Enter two numbers:  ");   scanf("%f %f", &n1, &n2);   s=n1+n2;   d=n1-n2;   p=n1*n2;   q=n1/n2;   printf("\n Sum        %9.2f",s);   printf("\n Difference %9.2f",d);   printf("\n Product    %9.2f",p);   printf("\n Quotient  %9.2f",q);   getch();   return(0); }```

 Naveed Marwat 1Mar2011 11:39

Re: Calculator program in C

Nice Sharing

 alexsmth114 11Mar2011 10:18

Re: Calculator program in C

Excellent post, couldn't have asked for more!!..

 sura 27Oct2011 04:38

Re: Calculator program in C

very good
i also like this ..
its good...

 rioeduardo 18Feb2013 08:13

Re: Calculator program in C

Thanks for sharing.

 All times are GMT +5.5. The time now is 02:23.