Job Name-1
Job Name-1
Arithmetic Operations in C#
Comparison Operations in C#
Comparison operators are used to compare two values and return a boolean result (true or false).
== Equal to a == b false
• Console.WriteLine() – Prints output to the console and moves the cursor to the next
line.
• Console.Write() – Prints output to the console without moving to the next line.
• Console.ReadLine() – Reads an entire line of input from the user as a string.
• Console.Read() – Reads a single character from the input as an integer (ASCII value).
• Convert.ToInt32() – Converts a value (e.g., string, double) to a 32-bit integer.
1. Write a C# Console program to input two numbers and perform all arithmetic operations.
Input:
First number:
10
Second number:
5
Output:
Sum = 15
Difference = 5
Product = 50
Quotient = 2
Modulus = 0
Code:
int a, b, sum, diff, product, qoutient, mod;
Console.WriteLine("Input: ");
Console.WriteLine("First Number: ");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Second Number: ");
b = Convert.ToInt32(Console.ReadLine());
sum = a + b;
diff = a - b;
product = a * b;
qoutient = a / b;
mod = a % b;
Console.WriteLine("Output: ");
Console.WriteLine("Sum = " + sum);
Console.WriteLine($"Difference = {diff}");
Console.WriteLine($"Product = { product}");
Console.WriteLine($"Quotient = {qoutient}");
Console.WriteLine($"Modulus = {mod}");
Output:
2. Write a C# Console program to input radius of a circle from user and find diameter,
circumference and area of the circle.
Input:
Enter radius: 10
Output:
Diameter = 20 units
Circumference = 62.79 units
Area = 314 sq. units
Code:
const double PI = 3.1416;
int radius;
double diameter, circumference, area ;
Console.WriteLine("Input: ");
Console.Write("Enter Radius: ");
radius = Convert.ToInt32(Console.ReadLine());
diameter = 2 * radius;
circumference = 2 * radius * PI;
area = PI * radius * radius;
Console.WriteLine("Output: ");
Console.WriteLine($"Diameter = {diameter} units");
Console.WriteLine($"Circumference = {circumference} units");
Console.WriteLine($"Area = {area} units");
Output:
3. Write a C# Console program to find maximum between three numbers.
Input:
Input num1: 10
Input num2: 20
Input num3: 15
Output:
Maximum is: 20
Code:
int a, b, c;
Console.WriteLine("Input: ");
Console.Write("Input num1: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Input num2: ");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Input num3: ");
c = Convert.ToInt32(Console.ReadLine());
Output:
Conclusion:
This program demonstrates how to use arithmetic and comparison operators in C#. Arithmetic
operations help perform calculations, while comparison operators are essential for decision-
making in programs. These concepts are fundamental for writing effective C# applications, such
as calculators and automated decision-making systems.