# C# Basic Programming Examples

Here is the listing of C# programming examples on Basics.

#### 1. C# Examples on Fundamental Mathematical Operations

The following section deals with fundamental mathematical operations. Numbers ending with 0,2,4,6,8 are called even numbers and numbers ending with 1,3,5,7,9 are called odd numbers. A Binary Triangle is the triangle formed by a series of 0’s and 1’s. So, the C# Programs in the following section checks for even or odd properties of a number, interchanges the values of 2 numbers which is done using the process of swapping, displays the sum of digits in a number, displays the reverse of a number, prints a binary triangle and counts the number of 1’s in a given number which is input to the program.

#### 2. C# Examples on Date Formats and Years

If the given year is divisible by 4, then it is said to be a Leap Year. A Leap Year has 366 days. The C# Programs in the given section check if the entered year is a leap year or not, they also display various date formats. The different date formats are dd/mm/yyyy, mm/dd/yyyy, dd/mmm/yyyy and so on and compares the two entered dates. It also displays day-to-day ATM transactions and generates random numbers. A random number is generated by selecting a specified distribution that tries to return values in the range of values generated by that distribution.

#### 3. C# Examples on Bitwise and Swapping Operations

The bitwise exclusive OR operator (^) compares each bit of its first operand to the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. otherwise, the corresponding result bit is set to 0. The following C# Programs swap the contents of two numbers using bitwise XOR Operation, it finds the number of times the word ‘the’ (frequency) appears in a given sentence and accept the height of a person & categorizes it as Taller, Dwarf & Average.

#### 4. C# Examples on Access Specifiers

Access Specifiers determine whether a field or method in a class, can be used or invoked by another method in another class or sub-class. Public, Private, Protected and Default are the four access specifiers. Loop within another loop is called nested loop. The left-shift operator (<<), which moves the bits of shift_expression to the left. The following C# programs illustrate the use of access specifiers, left shift operators, prints a diamond using nested loop, reads a grade & displays the equivalent description and finds the greatest among 2 numbers.

#### 5. C# Examples on Unboxing Operation and Sealed classes.

The automatic conversion of primitive data types into its equivalent wrapper type is known as boxing and opposite operation is known as unboxing. A method, indexer, property, or event on a derived class that is overriding a virtual member of the base class can declare that member as sealed. The following C# Programs create a sealed class, performs unboxing operation, prints squarefeet of a house, lists all the prime numbers between 1 to 100 and accepts a number from the user and indicates it if it is positive.

#### 6. C# Examples on Numerical Operations

The average of a set of numbers is the sum of the numbers divided by the number of values in the set. The following C# Programs calculates the magnitude of integer, evaluates average for the set of values and displays numbers in the form of triangle.

C# Program to Find Magnitude of Integer C# Program to Compute Average for the Set of Values C# Program to Display Numbers in the form of Triangle |

#### 7. C# Examples on Interface and Looping Operations

An interface is a collection of abstract methods. The C# Program in the section implements for-each loop in the interface. The other programs implement phonebook, calculation of acceleration, finding a number using pythagoras theorem and performs division of same base exponents.