Please note, this is a STATIC archive of website technosmarter.com from July 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

Addition ,multiplication ,subtraction ,divide in C# programming


In the previous tutorial, started a basic Hello World program in C# programming . In this tutorial, we will learn logics for addition, multiplication, subtraction, divide in the C# program. One of the best things is to create a login and execute a program using C#. We can use C programming concepts in C# programming. If you are learned C programming then It will be easy to understand.
First of all, create a program for Addition.


Adding two numbers using C# programming.


The heading is indicating the behavior of the method. Here, we are going to create an addition program for two number using mathematics and C# . It will help you understand the mutuality between C and C# programming. To add two numbers, you have to define three variables with the datatype. There is a similar example in PHP programming.

Two numbers addition in PHP programming

Let's create an example in C# programming.


using System;
namespace MathApplication{
   
   class Math{
      
      static void Main(string[] args) {
    
         int a=20; 
         int b=20; 
         int z=a+b; 
         
         Console.WriteLine(z);
         Console.ReadKey();
      }
   }
}

Execute this program on a windows console application. It gives the result as output -

40

In the above example, we defined three variable a,b,z as like C programming. We made logic for two number addition.
We added first two variables in the third variable and display the z value using WriteLine() method of Console class.

 

Multiplication of three numbers using C# -


In the above example, we added two numbers. Here, we are going to the multiplication of three number in C#. Multiply three numbers and check multiplied value after execute.
To multiply three values (numbers), you have to create a math logic using asterisk *.
Let's have a look -


using System;
namespace MathApplication{
   
   class Math{
      
      static void Main(string[] args) {
    
         int x=3; 
         int y=7; 
         int z=10;
         int mul=x*y*z; 
         
         Console.WriteLine(mul);
         Console.ReadKey();
      }
   }
}
Output-
210

In the example, we defined three variables with their values and data type. We multiplied three values in another variable mul. This is a simple mathematics logic with C sharp programming


Subtraction of two numbers in C sharp programming -


Let's create a program to subtract two numbers using C# programming with mathematics logic.

Here, we are going create a logic fo subtraction in C# programming.


using System;
namespace MathApplication{
   
   class Math{
      
      static void Main(string[] args) {
    
         int x=12; 
         int y=-5; 
         int z; 
         z=x-y;
         
         Console.WriteLine(z);
         Console.ReadKey();
      }
   }
}

Execute the program and will get the result as output.

17

 

Dividing two variable in C# programming -

 

We can divide two, three, four etc numbers using C# programming. You have to create a math logic inside the method.

Let's make logic to divide two numbers in C# programming.


using System;
namespace MathApplication{
   
   class Math{
      
      static void Main(string[] args) {
    
         int num1=36; 
         int num2=4; 
         int dev; 
         dev=num1/num2;
         
         Console.WriteLine(dev);
         Console.ReadKey();
      }
   }
}

Output -
9

In the above example, we created num1 and num2 variables and defines their values.

In this way, we can create mathematics logic for Addition, multiplication, subtraction, divide in C# program. These operatios are know of mathematics operations


Please Share

Recommended Posts:-