---
title: "Creating a web service for performing arithmetic operations"  
description: "I am creating a web service for performing arithmetic operations or say a calculator. Then, I will use this service in a windows based application to"  
author: "mohan kumar"  
published: 2012-05-08  
updated: 2020-01-03  
canonical: https://www.mindstick.com/articles/943/creating-a-web-service-for-performing-arithmetic-operations  
category: "web services"  
tags: ["web services"]  
reading_time: 7 minutes  

---

# Creating a web service for performing arithmetic operations

##### Introduction:

I am creating a web service for performing arithmetic operations or say a calculator. Then, I will use this service in a windows based application to perform arithmetic operations like a calculator. So, let's create a web service. Following is the Code Snippet.

Now replace the code on **.asmx.cs** page with the following code.

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace calc
{
    ///<summary>
    /// Summary description for CalcWebService
    ///</summary>
    [WebService(Namespace = "mycalculatorexample.org")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class CalcWebService : System.Web.Services.WebService
    {
       [WebMethod]
        public string calculate(string first, string second, char sign)
        {
            string result;
            switch (sign)
            {
                case '+':
                    {
                        result = (Convert.ToInt32(first) + Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '-':
                    {
                        result = (Convert.ToInt32(first) - Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '*':
                    {
                        result = (Convert.ToInt32(first) * Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '/':
                    {
                        result = (Convert.ToInt32(first) / Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '%':
                    {
                        result = (Convert.ToInt32(first) % Convert.ToInt32(second)).ToString();
                        break;
                    }
                default:
                    result = "Invalid";
                    break;
            }
            return result;
        }
    }
}
```

Run the application.

##### Output:

![Creating a web service for performing arithmetic operations](https://www.mindstick.com/mindstickarticle/896efaad-4f5e-417c-be36-bdc8cc21def0/images/0d96f734-2ce2-4413-add7-2873c6380380.png)

Go to test page by clicking at "calculate" -> do the entry and click the "invoke"

button.\
\
![Creating a web service for performing arithmetic operations](https://www.mindstick.com/mindstickarticle/896efaad-4f5e-417c-be36-bdc8cc21def0/images/8309f66f-5096-4115-a5ac-41d526f5d2cc.png)

It will show the result like the below figure. (After clicking the "invoke" button)\
\
![Creating a web service for performing arithmetic operations](https://www.mindstick.com/mindstickarticle/896efaad-4f5e-417c-be36-bdc8cc21def0/images/ffebbb4b-bdd1-48d8-b843-797ec8bd2b75.png)\
\
Similarly, you can perform different operations like addition, subtraction, multiplication, division and modulo division. Now we consume this web service into window form application.

Create a windows application and add a service reference (You can get help here ). After adding the reference, go to the design page and arrange the UI controls like in the below figure.\
\
![Creating a web service for performing arithmetic operations](https://www.mindstick.com/mindstickarticle/896efaad-4f5e-417c-be36-bdc8cc21def0/images/7e8128a6-3ab4-4604-a464-918dbf9e2af7.png)\
\
Now, write the following code in the .cs file.

```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string i, j;
        static char c;
        private void btnthree_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(3);
        }
        private void btnone_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(1);
        }
        private void btntwo_Click(object sender, EventArgs e)
       {
            textBox1.Text += Convert.ToString(2);
        }
        private void btnfour_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(4);
        }
        private void btnfive_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(5);
        }
        private void btnsix_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(6);
        }
        private void btnseven_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(7);
        }
        private void btneight_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(8);
        }
        private void btnnine_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(9);
        }
        private void btnzero_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(0);
        }
        private void btnplus_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '+';
        }
        private void btnminuse_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '-';
        }
        private void btnmultiply_Click(object sender, EventArgs e)
        {
           i = textBox1.Text;
            textBox1.Text = "";
            c = '*';
        }
        private void btndivide_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '/';
        }
        private void btnmodulo_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '%';
        }
        private void btnequal_Click(object sender, EventArgs e)
        {
            j = textBox1.Text;
            calclocalhost.CalcWebService obj = new calclocalhost.CalcWebService();
            textBox1.Text = obj.calculate(i, j, c);
        }
    }
}
```

Run the application.\

\

##### Output:

**![Creating a web service for performing arithmetic operations](https://www.mindstick.com/mindstickarticle/896efaad-4f5e-417c-be36-bdc8cc21def0/images/e5f538c6-da29-4054-8a5a-3654f8b79af0.png)**

**\** You can work as calculator.

---

Original Source: https://www.mindstick.com/articles/943/creating-a-web-service-for-performing-arithmetic-operations

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
