Arithmetic Operations in C# using Switch Cases | Function
Arithmetic operations in C# using switch cases and functions.
In this article, you will learn how to make arithmetic operations in C# using switch cases and functions.
Example
Enter any two positive integer numbers:
9
6
Addition of 9 + 6 = 15
Subtraction of 9 - 6 = 3
Multiplication of 9 * 6 = 54
Division of 9 / 6 = 1.5
Modulus of 9 % 6 = 3
You should have knowledge of the following topics in C# programming to understand this program:
- C# Oops Concepts
- C# System Library
- C# Switch Case Statement
- C# Functions
Source Code
// Arithmetic Operators in C#
using System;
public class W3CW {
// It's the driver function
static public void Main (string[] args) {
Console.WriteLine("Enter any two positive integer numbers:\n");
int p = Convert.ToInt32(Console.ReadLine());
int q = Convert.ToInt32(Console.ReadLine());
int sum, sub, mul, mod;
float div;
// It will perform all arithmetic operations
sum = p + q;
sub = p - q;
mul = p * q;
div = (float)p / q;
mod = p % q;
// It will print the final output
Console.WriteLine("Addition of "+p+" + "+q+" = "+sum);
Console.WriteLine("Subtraction of "+p+" - "+q+" = "+sub);
Console.WriteLine("Multiplication of "+p+" * "+q+" = "+mul);
Console.WriteLine("Division of "+p+" / "+q+" = "+div);
Console.WriteLine("Modulus of "+p+" % "+q+" = "+mod);
}
}
Output
Enter any two positive integer numbers:
9
6
Addition of 9 + 6 = 15
Subtraction of 9 - 6 = 3
Multiplication of 9 * 6 = 54
Division of 9 / 6 = 1.5
Modulus of 9 % 6 = 3
Explanation
In the above program, we have taken input 9
and 6
from the user via the system console. Then we applied these arithmetic operations following addition, subtraction, multiplication, division, and modulus.
After that It will return these output values following: 15
, 3
, 54
, 1.5
, 3
.
Arithmetic Operators in C# using Switch Case
// Arithmetic Operators in C# using Switch Case
using System;
public class W3CW {
// It's the driver function
static public void Main (string[] args) {
Console.WriteLine("Enter any two positive integer numbers:\n");
int p = Convert.ToInt32(Console.ReadLine());
int q = Convert.ToInt32(Console.ReadLine());
// It will suggest choosing an option to make the operation
Console.WriteLine("Input your choice to make an operation\n");
Console.WriteLine("1 :: for Addition");
Console.WriteLine("2 :: for Subtraction");
Console.WriteLine("3 :: for Multiplication");
Console.WriteLine("4 :: for Division");
Console.WriteLine("5 :: for Modulus");
Console.WriteLine("\nEnter your choice:\n");
int choice = Convert.ToInt32(Console.ReadLine());
// It will perform all arithmetic operations
// According to user's choice & print the final output
switch (choice) {
case 1:
Console.WriteLine("Addition of "+p+" + "+q+" = "+(p + q));
break;
case 2:
Console.WriteLine("Subtraction of "+p+" - "+q+" = "+(p - q));
break;
case 3:
Console.WriteLine("Multiplication of "+p+" * "+q+" = "+(p * q));
break;
case 4:
Console.WriteLine("Division of "+p+" / "+q+" = "+(float)(p + q));
break;
case 5:
Console.WriteLine("Modulus of "+p+" % "+q+" = "+(p % q));
break;
default:
Console.WriteLine("Kindly input correct choice!");
break;
}
}
}
Arithmetic Operators in C# using Functions
// Arithmetic Operators in C# using Functions
using System;
public class W3CW {
// Function to make an Addition
public static int Add(int a, int b) {
return a + b;
}
// Function to make Subtraction
public static int Sub(int a, int b) {
return a - b;
}
// Function to make Multiplication
public static int Mul(int a, int b) {
return a * b;
}
// Function to make Division
public static float Div(int a, int b) {
return (float)(a / b);
}
// Function to make Modulus
public static int Mod(int a, int b) {
return a % b;
}
// It's the driver function
static public void Main (string[] args) {
Console.WriteLine("Enter any two positive integer numbers:\n");
int p = Convert.ToInt32(Console.ReadLine());
int q = Convert.ToInt32(Console.ReadLine());
// It will print the final output
Console.WriteLine("Addition of "+p+" + "+q+" = "+Add(p,q));
Console.WriteLine("Subtraction of "+p+" - "+q+" = "+Sub(p,q));
Console.WriteLine("Multiplication of "+p+" * "+q+" = "+Mul(p,q));
Console.WriteLine("Division of "+p+" / "+q+" = "+Div(p,q));
Console.WriteLine("Modulus of "+p+" % "+q+" = "+Mod(p,q));
}
}