[go: up one dir, main page]

0% found this document useful (0 votes)
11 views16 pages

C# Operators

The document provides a comprehensive overview of various C# programming concepts including user input, arithmetic, assignment, comparison, logical operators, string manipulation, arrays, inheritance, and abstraction. Each section includes code examples and their outputs, demonstrating the functionality of different operators and programming structures. It serves as a practical guide for understanding fundamental programming principles in C#.

Uploaded by

sabariv688
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

C# Operators

The document provides a comprehensive overview of various C# programming concepts including user input, arithmetic, assignment, comparison, logical operators, string manipulation, arrays, inheritance, and abstraction. Each section includes code examples and their outputs, demonstrating the functionality of different operators and programming structures. It serves as a practical guide for understanding fundamental programming principles in C#.

Uploaded by

sabariv688
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Get value from user:-

using System;

class LogicalOperatorsWithInput
{
static void Main()
{
// Ask for the username
Console.Write("Enter username: ");
string userName = Console.ReadLine();

// Display the entered username


Console.WriteLine("Username is: " + userName);
}
}

Output:

Enter username: Username is: raja

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;

Console.WriteLine("x = " + x);


Console.WriteLine("y = " + y);

Console.WriteLine("x == y: " + (x == y)); // Equal to


Console.WriteLine("x != y: " + (x != y)); // Not equal to
Console.WriteLine("x > y: " + (x > y)); // Greater than
Console.WriteLine("x < y: " + (x < y)); // Less than
Console.WriteLine("x >= y: " + (x >= y)); // Greater than or equal to
Console.WriteLine("x <= y: " + (x <= y)); // Less than or equal to
}
}

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;

Console.WriteLine("a = " + a);


Console.WriteLine("b = " + b);

Console.WriteLine("a && b: " + (a && b)); // AND


Console.WriteLine("a || b: " + (a || b)); // OR
Console.WriteLine("!a: " + (!a)); // NOT
Console.WriteLine("!b: " + (!b)); // NOT

}
}

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;

Console.WriteLine("Number 1: " + num1);


Console.WriteLine("Number 2: " + num2);
Console.WriteLine();

Console.WriteLine("Square root of num1: " + Math.Sqrt(num1));


Console.WriteLine("num1 raised to num2: " + Math.Pow(num1, num2));
Console.WriteLine("Absolute of -num2: " + Math.Abs(-num2));
Console.WriteLine("Maximum: " + Math.Max(num1, num2));
Console.WriteLine("Minimum: " + Math.Min(num1, num2));
Console.WriteLine("Rounded num1: " + Math.Round(num1));
Console.WriteLine("Ceiling of num1: " + Math.Ceiling(num1));
Console.WriteLine("Floor of num1: " + Math.Floor(num1));
}
}

Output:

Number 1: 25
Number 2: 3

Square root of num1: 5


num1 raised to num2: 15625
Absolute of -num2: 3
Maximum: 25
Minimum: 3
Rounded num1: 25
Ceiling of num1: 25
Floor of num1: 25

String :-

using System;

class StringExample
{
static void Main()
{
Console.Write("Enter your first name: ");
string firstName = Console.ReadLine();

Console.Write("Enter your last name: ");


string lastName = Console.ReadLine();

// String concatenation
string fullName = firstName + " " + lastName;

Console.WriteLine("\nFull Name: " + fullName);

// Some common string operations


Console.WriteLine("Length of full name: " + fullName.Length);
Console.WriteLine("Uppercase: " + fullName.ToUpper());
Console.WriteLine("Lowercase: " + fullName.ToLower());
Console.WriteLine("Does it contain 'a'? " +
fullName.Contains("a"));
Console.WriteLine("First name starts with 'A'? " +
firstName.StartsWith("A"));
Console.WriteLine("Last name ends with 'n'? " +
lastName.EndsWith("n"));
Console.WriteLine("Substring (first 4 letters): " +
fullName.Substring(0, 4));
Console.WriteLine("Replaced spaces with dashes: " +
fullName.Replace(" ", "-"));
}
}

Output:

Enter your first name: Enter your last name:


Full Name: ajith kumar
Length of full name: 11
Uppercase: AJITH KUMAR
Lowercase: ajith kumar
Does it contain 'a'? True
First name starts with 'A'? False
Last name ends with 'n'? False
Substring (first 4 letters): ajit
Replaced spaces with dashes: ajith-kumar

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:

My full name is: ajith kumar

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);

// Accessing first and last characters


Console.WriteLine("First character: " + word[0]);
Console.WriteLine("Last character: " + word[word.Length - 1]);

// Loop through all characters


Console.WriteLine("\nCharacters in the word:");
for (int i = 0; i < word.Length; i++)
{
Console.WriteLine($"Character at index {i}: {word[i]}");
}
}
}

Output:

Enter a word:
You entered: raja
Length of the word: 4
First character: r
Last character: a

Characters in the word:


Character at index 0: r
Character at index 1: a
Character at index 2: j
Character at index 3: a

Strings - Special Characters:-


using System;
class SpecialCharactersDemo
{
static void Main()
{
string message = "Hello,\nWelcome to C# Programming!";
string tabbed = "Name\tAge\tCity";
string quoted = "She said, \"Learning C# is fun!\"";
string path = "C:\\Users\\Public\\Documents";

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:

Enter your age: Are you an adult? False


You are not eligible to vote.

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];

// Assign values to the array


fruits[0] = "Mango";
fruits[1] = "Banana";

// Print the first element


Console.WriteLine("First fruit: " + fruits[0]);

// Optional: print all fruits


Console.WriteLine("All fruits in the array:");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
}
}
Output:-

First fruit: Mango


All fruits in the array:
Mango
Banana
Single Inheritance:-
using System;

public class Employee


{
public float salary = 40000;
}

public class Programmer : Employee


{
public float bonus = 10000;
}

class TestInheritance
{
public static void Main(string[] args)
{
Programmer p1 = new Programmer(); // Correct object declaration

Console.WriteLine("Salary: " + p1.salary);


Console.WriteLine("Bonus: " + p1.bonus);
}
}

Output:-
Salary:40000
bonus : 10000

Multilevel Inheritance:-
using System;
public class Employee
{
public float salary = 40000;
}

public class Programmer : Employee


{
public float bonus = 10000;
}

public class FEDeveloper : Programmer


{
public float hike = 20000;
}
class TestInheritance
{
public static void Main()
{
FEDeveloper dev = new FEDeveloper();

Console.WriteLine("Salary: " + dev.salary);


Console.WriteLine("Bonus: " + dev.bonus);
Console.WriteLine("Hike: " + dev.hike);

}
}
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

Multiple Inheritance Using Interfaces Example:-


using System;
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}
class Demo : Car, Bus
{
// Explicit implementation of Car.Drive
void Car.Drive()
{
Console.WriteLine("Drive Car");
}
// Explicit implementation of Bus.Drive
void Bus.Drive()
{
Console.WriteLine("Drive Bus");
}

static void Main()


{
Demo DemoObject = new Demo();

// Call Drive() using interface references


((Car)DemoObject).Drive(); // Outputs: Drive Car
((Bus)DemoObject).Drive(); // Outputs: Drive Bus
}
}
Output:-
Drive Car
Drive Bus

C# Abstraction Examples(1):-
using System;
abstract class Language
{
public abstract void SayHello();
}

// A derived class must implement the abstract method


class English : Language
{
public override void SayHello()
{
Console.WriteLine("Hello!");
}
}

class TestAbstract
{
static void Main()
{
// Language obj = new Language(); // ❌ This will cause a compile-time error

// ✅ Correct: Create object of a derived class


Language obj = new English();
obj.SayHello();

}
}
Output:- Hello!
C# Abstraction Examples (2):-
using System;

abstract class Language


{
// Non-abstract method
public void display()
{
Console.WriteLine("Non abstract method");
}
}

// Program class inherits from the abstract class Language


class Program : Language
{
static void Main(string[] args)
{
Program obj = new Program(); // Object of derived class
obj.display(); // Calls the method from the abstract class
}
}
Output:- Non abstract method
C# Abstraction Examples (3):-
using System;

abstract class Animal


{
// Abstract method (no body)
public abstract void makeSound();
}

// Dog inherits from Animal and implements makeSound


class Dog : Animal
{
public override void makeSound()
{
Console.WriteLine("Bark Bark");
}
}

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

You might also like