Go4Expert

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.


All times are GMT +5.5. The time now is 18:42.