csharp Arithmetic operations Via Classes

In c sharp console application we can use all arithmetic operations via classes. first of you have to create a separate class by going to solution explorer right click on project > Add > Class and Name your class i.e Operations. (following is Source code of our  operations.cs class)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class operations
    {
        public int add(int x ,int y )
           {
            int sum = x + y;
            Console.Write("Your Sum is = " + sum);
            return 0;
           }

        public int substract(int x, int y)
           {
            int sub=x-y;
            Console.Write("Substraction of Your Numbers = " + sub);
            return 0; 
           }

        public int multiply(int x, int y)
         
        {
             int muliplication = x * y;
             Console.Write("Multiplication of your Numbers = " + muliplication);
             return 0;
        }

        public int divide(int x, int y)
        
        {
            int division = x / y;
            Console.Write ("Division of your Numbers i s= ");
            return 0;
        }
    }
}

First  You to create a  Instance of your Class in main Program.cs in your Project
operations operation =new operations();

the we call the Required Method in our operation class according to user input. see the following full source code of our  main program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        
        {
            operations operation =new operations();

            Console.Write("Enter Your First Number =");
            int x = int.Parse(Console.ReadLine());
            Console.Write("Enter Your 2nd Number ");
            int y = int.Parse(Console.ReadLine());

            Console.Write("Select Any Operation ");
            Console.WriteLine("1.Add 2.Substract 3.Multiply 4.divide");

            string choice = Console.ReadLine();

            if (choice == "1")
                operation.add(x, y);

            else if (choice == "2")
                operation.substract(x, y);

            else if (choice == "3")
                operation.multiply(x, y);

            else if (choice == "4")
                operation.divide(x, y);

                Console.ReadLine();


        }
    }
}


No comments:

Post a Comment

Sending SMS Via Windows Form Application Using any Android Smart Phone  Source Code  using System ; using System . C...