Calculator program in C

coderzone's Avatar author of Calculator program in C
This is an article on Calculator program in C in C.
Rated 5.00 By 1 users
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);
}
shabbir like this
tjweb's Avatar
Newbie Member
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's Avatar, Join Date: Jul 2004
Team Leader
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's Avatar, Join Date: Jun 2006
Go4Expert Member
very good stuff man
143rocker's Avatar, Join Date: Nov 2006
Newbie Member
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 shabbir; 14Nov2006 at 14:11.. Reason: Code formating.
143rocker's Avatar, Join Date: Nov 2006
Newbie Member
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 shabbir; 14Nov2006 at 14:10.. Reason: Code formating.
shanku_4ch's Avatar, Join Date: Jan 2007
Light Poster
your program doesnt work if a space is given between operan & operator
2+5=7

but
2 + 5

doesnt work
hanleyhansen's Avatar, Join Date: Jan 2008
Pro contributor
Great stuff!
happyz's Avatar, Join Date: Nov 2008
Go4Expert Member
Nicely done
hkp819's Avatar
Banned
This program is very nicely done.
I like this program. I have try it. it is very good.