Calculator program in C

Discussion in 'C' started by coderzone, Apr 5, 2006.

  1. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    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:
    #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);
    }
     
    shabbir likes this.
  2. tjweb

    tjweb New Member

    Joined:
    Jul 19, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  3. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    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.
     
  4. gamehunter101

    gamehunter101 New Member

    Joined:
    Jun 19, 2006
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    very good stuff man
     
  5. 143rocker

    143rocker New Member

    Joined:
    Nov 14, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    philippines
    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>
    }
     
    Last edited by a moderator: Nov 14, 2006
  6. 143rocker

    143rocker New Member

    Joined:
    Nov 14, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    philippines
    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);
    }
     
    Last edited by a moderator: Nov 14, 2006
  7. shanku_4ch

    shanku_4ch New Member

    Joined:
    Jan 7, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    your program doesnt work if a space is given between operan & operator
    2+5=7

    but
    2 + 5

    doesnt work
     
  8. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    http://www.hanseninfotech.com
  9. happyz

    happyz New Member

    Joined:
    Nov 18, 2008
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Nicely done
     
  10. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    This program is very nicely done.
    I like this program. I have try it. it is very good.
     
  11. kadjam

    kadjam New Member

    Joined:
    Mar 29, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    what about a supermarket service to calculate prices of goods with fixed prices. with a cashier log in
     
  12. kadjam

    kadjam New Member

    Joined:
    Mar 29, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    what about a supermarket service to calculate prices of goods with fixed prices. with a cashier log in
     
  13. saji777

    saji777 New Member

    Joined:
    Jun 22, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    tnx but i think we needed a top source from calculator
    Specal thanks
     
  14. newtosee

    newtosee New Member

    Joined:
    Jun 27, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 :)

    Anyway, I would love to read your feedback ^^

    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!
     
  15. cute_dhanju

    cute_dhanju New Member

    Joined:
    Aug 5, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    computer feild
    Location:
    india
    gud,....................
     
  16. atena.sharifi

    atena.sharifi New Member

    Joined:
    Aug 11, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    :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.
     
  17. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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
     
  18. matuatua

    matuatua New Member

    Joined:
    Aug 31, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    i will try it later,anyway thank u for that code
     
  19. hemamca

    hemamca New Member

    Joined:
    Sep 13, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hai do i know which editor is best now in c / c++ languages. is better Microsoft or Turbo C
     
  20. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    TC is outdated and not used anymore.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice