Wednesday, 23 April 2014

C# program to perform a simple calculator function

Calculator is made to perform some predefined functions on it. We use it to speed up our calculation and provide exact result. Calculator performs varies functions some of the basic functions it does are addition, substation, multiplication and division. These are performed on integers either numbers. The input is provided with specifying operator and hence to get exact output.

The below motioned c# program will perform tasks of a calculator. The program first begins with menu and provides options to user. User will choose desired option to start calculation function. int.Parse(Console.ReadLine())  is used to get an input from the user and stored in a variables. The switch statement performs the its defined tasks or the code as with respect to user input. Case 1 is designed to perform addition, case 2 is to perform subtraction and so on,.  

The basic  calculator functions are :
ADDITION OF TWO NUMBER :
C= A + B
SUBTRACTION OF TWO NUMBER :
C= A – B
MULTIPLICATION OF TWO NUMBER :
C= A * B
DIVISION OF TWO NUMBER
C= A / B


                              SOURCE CODE:
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace calculator  
 {  
   class cal  
   {  
     public void calculator()  
     {  
       int a;  
       int b;  
       int c;  
       Console.WriteLine(" @@@ menu @@@");  
       Console.WriteLine("enter 1 for sum ");  
       Console.WriteLine("enter 2 for sub ");  
       Console.WriteLine("enter 3 for mult ");  
       Console.WriteLine("enter 4 for div ");  
       c = int.Parse(Console.ReadLine());  
       Console.Write("enter a integer A : ");  
       a = int.Parse(Console.ReadLine());  
       Console.Write("enter a integer B : ");  
       b = int.Parse(Console.ReadLine());  
       switch (c)  
       {  
         case 1:  
           c = a + b;  
           Console.WriteLine("sum=" + c);  
           break;  
         case 2:  
           c = a - b;  
           Console.WriteLine("sub=" + c);  
           break;  
         case 3:  
           c = a * b;  
           Console.WriteLine("mul=" + c);  
           break;  
         case 4:  
           if (b == 0)  
           {  
             Console.WriteLine("enter a valid number ");  
           }  
           else  
           {  
             c = a / b;  
             Console.WriteLine("mul=" + c);  
           }  
           break;  
         default:  
           Console.WriteLine("Invalid choice");  
           break;  
       }  
     }  
   }  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       cal c=new cal();  
       c.calculator();  
     }  
   }  
 }  



                   OUTPUT:
@@@ menu @@@
Enter 1 for sum
Enter 2 for sub
Enter 3 for mult
Enter 4 for div

3

Enter a integer A : 80
Enter a integer B : 90
Mult = 7200


No comments:
Write comments

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2016