C# Operators
C# Operators
using System;
class LogicalOperatorsWithInput
{
static void Main()
{
// Ask for the username
Console.Write("Enter username: ");
string userName = Console.ReadLine();
Output:
1.Arthamatic:-
using System;
class ArithmeticOperatorsDemo
{
static void Main()
{
// Declare variables
int num1 = 20;
int num2 = 8;
// Arithmetic operations
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
int quotient = num1 / num2;
int remainder = num1 % num2;
// Display results
Console.WriteLine("First Number: " + num1);
Console.WriteLine("Second Number: " + num2);
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Difference: " + difference);
Console.WriteLine("Product: " + product);
Console.WriteLine("Quotient: " + quotient);
Console.WriteLine("Remainder: " + remainder);
}
}
Output:
First Number: 20
Second Number: 8
Sum: 28
Difference: 12
Product: 160
Quotient: 2
Remainder: 4
2. AssignmentOperatousing System:-
using System;
class AssignmentOperatorsDemo
{
static void Main()
{
int a = 10;
Console.WriteLine("Initial value of a: " + a);
a += 5; // a = a + 5
Console.WriteLine("After a += 5: " + a);
a -= 3; // a = a - 3
Console.WriteLine("After a -= 3: " + a);
a *= 2; // a = a * 2
Console.WriteLine("After a *= 2: " + a);
a /= 4; // a = a / 4
Console.WriteLine("After a /= 4: " + a);
a %= 3; // a = a % 3
Console.WriteLine("After a %= 3: " + a);
}
}
Output:
Initial value of a: 10
After a += 5: 15
After a -= 3: 12
After a *= 2: 24
After a /= 4: 6
After a %= 3: 0
3. ComparisonOperator:-
using System;
class ComparisonOperatorsDemo
{
static void Main()
{
int x = 10;
int y = 20;
Output:
x = 10
y = 20
x == y: False
x != y: True
x > y: False
x < y: True
x >= y: False
x <= y: True
4. LogicalOperators
using System;
class LogicalOperatorsDemo
{
static void Main()
{
bool a = true;
bool b = false;
}
}
Output:
a = True
b = False
a && b: False
a || b: True
!a: False
!b: True
Math:-
using System;
class MathFunctionsDemo
{
static void Main()
{
double num1 = 25;
double num2 = 3;
Output:
Number 1: 25
Number 2: 3
String :-
using System;
class StringExample
{
static void Main()
{
Console.Write("Enter your first name: ");
string firstName = Console.ReadLine();
// String concatenation
string fullName = firstName + " " + lastName;
Output:
String Interpolation:-
using System;
class StringExample
{
static void Main()
{
string firstName = "ajith";
string lastName = "kumar";
string name = $"My full name is: {firstName} {lastName}";
Console.WriteLine(name);
}
}
Output:
Access Strings:-
using System;
class AccessStringDemo
{
static void Main()
{
Console.Write("Enter a word: ");
string word = Console.ReadLine();
Console.WriteLine("\nYou entered: " + word);
Console.WriteLine("Length of the word: " + word.Length);
Output:
Enter a word:
You entered: raja
Length of the word: 4
First character: r
Last character: a
Console.WriteLine(message);
Console.WriteLine(tabbed);
Console.WriteLine(quoted);
Console.WriteLine("File path: " + path);
}
}
Output:
Hello,
Welcome to C# Programming!
Name Age City
She said, "Learning C# is fun!"
File path: C:\Users\Public\Documents
C# Booleans:-
Example:-1.
using System;
class SpecialCharactersDemo
{
static void Main()
{
bool isCSharpFun = true;
bool isBroccoliTasty = false;
Console.WriteLine(isCSharpFun);
}
}
Output:-
True
Example:-2.
using System;
class BooleanDemo
{
static void Main()
{
Console.Write("Enter your age: ");
int age = Convert.ToInt32(Console.ReadLine());
bool isAdult = age >= 18;
Console.WriteLine("Are you an adult? " + isAdult);
// Use Boolean in an if-statement
if (isAdult)
{
Console.WriteLine("You are eligible to vote.");
}
else
{
Console.WriteLine("You are not eligible to vote.");
}
Console.ReadLine(); // Pause the console
}
}
Output:
If Statement (1)’:-
using System;
class SpecialCharactersDemo
{
static void Main()
{
if (10 > 5)
{
Console.WriteLine("10 is greater than 5");
}
}
}
Output:
10 is greater than 5
Example:-2
using System;
class SpecialCharactersDemo
{
static void Main()
{
int a = 10;
int b = 5;
if (a > b)
{
Console.WriteLine("a is greater than b");
}
}
}
Output:
a is greater than b
Array:-
using System;
class FruitsArrayDemo
{
static void Main()
{
// Declare a string array with 2 elements
string[] fruits = new string[2];
class TestInheritance
{
public static void Main(string[] args)
{
Programmer p1 = new Programmer(); // Correct object declaration
Output:-
Salary:40000
bonus : 10000
Multilevel Inheritance:-
using System;
public class Employee
{
public float salary = 40000;
}
}
}
Output:-
Salary: 40000
Bonus: 10000
Hike: 20000
Hierarchical Inheritance:-
using System;
public class Employee
{
public float salary = 40000;
}
public class Programmer : Employee
{
public float bonus = 10000;
}
public class HR : Employee
{
public float hike = 5000;
}
class TestInheritance
{
public static void Main()
{
Programmer p1 = new Programmer();
HR p2 = new HR();
Console.WriteLine("Programmer Salary: " + p1.salary);
Console.WriteLine("Programmer Bonus: " + p1.bonus);
Console.WriteLine("HR Salary: " + p2.salary);
Console.WriteLine("HR Hike: " + p2.hike);
}
}
Output:-
Programmer Salary: 40000
Programmer Bonus: 10000
HR Salary: 40000
HR Hike: 5000
C# Abstraction Examples(1):-
using System;
abstract class Language
{
public abstract void SayHello();
}
class TestAbstract
{
static void Main()
{
// Language obj = new Language(); // ❌ This will cause a compile-time error
}
}
Output:- Hello!
C# Abstraction Examples (2):-
using System;
class Program
{
static void Main(string[] args)
{
Dog obj = new Dog(); // Create an object of Dog
obj.makeSound(); // Call the overridden method
}
}
Output:-
Bark Bark