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
Newbie Member
19Jul2006,11:20   #2
tjweb's Avatar
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
Team Leader
19Jul2006,11:38   #3
coderzone's Avatar
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.
Go4Expert Member
19Jul2006,11:41   #4
gamehunter101's Avatar
very good stuff man
Newbie Member
14Nov2006,13:33   #5
143rocker's Avatar
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.
Newbie Member
14Nov2006,13:36   #6
143rocker's Avatar
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.
Light Poster
6May2007,08:26   #7
shanku_4ch's Avatar
your program doesnt work if a space is given between operan & operator
2+5=7

but
2 + 5

doesnt work
Pro contributor
23Sep2008,18:47   #8
hanleyhansen's Avatar
Great stuff!
Go4Expert Member
22Nov2008,19:31   #9
happyz's Avatar
Nicely done
Banned
4Dec2008,18:12   #10
hkp819's Avatar
This program is very nicely done.
I like this program. I have try it. it is very good.