Monday, January 31, 2011

Functions in C++

Hi guys!the program below is of a standard calculator by using switch statement.It asks for the values from the user and pass it by values.
If this program gives errors please let me know,i am ready to help any time..
May this program helps you alot!!

#include<iostream>
#include<conio.h>
using namespace std;
double calculator(float a,float b)
{
      char op;
       float c;
       cout<<"enter operator you want to perform(+,-,*,/):";
cin>>op;
      if(op=='+')
      c=a+b;
      else if(op=='-')
      c=a-b;
       else if(op=='*')
      c=a*b;
       else if(op=='/')
      c=a/b;
      return c;
      }
     
     
main()
{

      float n1,n2,x;
     
cout<<"enter the first number:";
cin>>n1;
cout<<"enter second number:";
cin>>n2;

x=calculator(n1,n2);
cout<<x;

getch();

}

No comments:

Post a Comment