C# Programs
C# is an object-oriented programming language. It was developed by Microsoft. Launched in 2000, it has since become a cornerstone of the .NET framework, enabling the creation of robust, scalable applications for various platforms. Multiple programs can be written using C#. These c# programs demonstrate the language is easy to use and readable.
So, let us look into these sample c# programs.
Fibonacci Series
The Fibonacci sequence is a sequence of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. Mathematically, it is defined by the recurrence relation . The sequence begins as 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Widely found in nature and art, the Fibonacci sequence exhibits inherent mathematical beauty and properties. It has applications in various fields, including computer science and finance, and is a fundamental example of recursive mathematical patterns in natural and artificial systems. Let us see the c# prohram for fibonacci series.
Recursive Fibonacci Series
Code:
Output:
Explanation:
The above sample c# program is easy to write and in this, the FibonacciRecursion method calculates the Fibonacci series using a recursive approach. It returns the nth Fibonacci number by recursively summing the previous two numbers.
Iterative Fibonacci Series
We shall now discuss the c# program for the iterative Fibonacci series.
Code:
Output:
Explanation:
- The FibonacciIteration method generates the Fibonacci series using an iterative approach. It uses a loop to update variables a, b, and c in each iteration, printing the series as it progresses.
Prime Number
Primality involves determining if a number is prime, having no divisors other than 1 and itself. Key in cryptography and algorithms, methods like the Sieve of Eratosthenes or the Miller-Rabin test identify primes. Prime numbers, essential in mathematics and computer science, are integral to secure systems and various computational processes.
Iterative Method to Check Prime Numbers
Here we shall discuss the c# program for iterative methods to check prime numbers.
Code:
Output:
Explanation:
Let us now learn the workings of the above C# program.
- The IsPrime function iteratively checks whether a given number is prime by testing divisibility up to the square root of the number.
- In the Main method, the program checks if the number 19 is prime using the iterative method and prints the result.
Sieve of Eratosthenes for Prime Numbers
Let us understand this c# program.
Code:
Output:
Explanation:
- The SieveOfEratosthenes method employs the Sieve of Eratosthenes algorithm to find prime numbers up to the specified limit.
- In the Main method, the program prints the prime numbers up to 30 using the Sieve of Eratosthenes.
- Time Complexity: O(n log log n)
- Space Complexity: O(n)
Factorial
Factorial represents the product of all numbers up to the given number. In this program, we shall learn to do factorial of a particular number. Let us now understand this C# program.
Code:
Output:
Explanation:
- The CalculateFactorial method uses recursion to calculate the factorial of a given number.
- In the Main method, the program calculates and prints the factorial of 5.
- Time Complexity: O(n) - Due to the recursive calls, where n is the input number for which the factorial is calculated.
Armstrong Number
An Armstrong is a number that is the sum of its digits raised to the power number of digits. Let us now look into the code for the above sort of numbers.
Code:
Consider this C# code for implementing the above program.
Output:
Explanation:
The explanation for the above C# code is as follows:
- The IsArmstrongNumber function checks if a given number is an Armstrong number.
- In the Main method, the program checks and prints whether the number 153 is an Armstrong number.
- Time Complexity: O(log n) - Due to the logarithmic calculation of the number of digits.
- Space Complexity: O(1) - Constant space is used as only a few variables are allocated regardless of the input size.
Sum of Digits
When we are given any number, this program helps us to find the sum of the individual digits of the number.
Code:
Output:
Explanation:
The explanation for this C# program is as follows:
- The SumOfDigits function takes an integer num as input and calculates the sum of its digits.
- Using a while loop, the program iterates through each digit of the number by repeatedly extracting the last digit using the modulo operator (num % 10).
- The extracted digit is added to the sum, and the last digit is removed from the number (num /= 10).
- The process continues until there are no more digits.
- The final sum is returned and printed as the output.
- In this example, the sum of digits in the number 12345 is calculated, resulting in an output of 15.
Reverse Number
This program demonstrates how we should reverse a number given to us.
Code:
Output:
Explanation:
- The ReverseNumber function takes an integer num as input and reverses its digits.
- Using a while loop, the program iterates through each digit of the number by repeatedly extracting the last digit using the modulo operator (num % 10).
- The extracted digit is appended to the reversedNumber after multiplying it by 10 to shift its position to the left.
- The last digit is removed from the number (num /= 10), and the process continues until there are no more digits.
- The final reversed number is returned and printed as the output.
- In this example, the reverse of the number 12345 is calculated, resulting in an output of 54321.
Swap Two Numbers without Using a Third Variable
This program shows us the approach of swapping two numbers without using a third variable.
Code:
Output:
Explanation:
- The program swaps the values of num1 and num2 without using a third variable.
- This is achieved by adding the two numbers (num1 = num1 + num2), then subtracting the original num2 to update num1 (num2 = num1 - num2), and finally subtracting the updated num2 to update num2 (num1 = num1 - num2).
Decimal to Binary
Decimal numbers are base 10 numbers and binary numbers are base 2 numbers. In this program, we shall learn to represent base 10 into base 2.
Code:
Output:
Explanation:
- The program converts a decimal number (23 in this case) to its binary representation without using a third variable.
- It uses a while loop to perform the conversion by repeatedly dividing the decimal number by 2 and storing the remainder in a string (binary). The binary representation is obtained by concatenating the remainders in reverse order.
Alphabet Triangle
Representing patterns in a program is an important step in learning any programming language. It helps us to develop and implement critical basic logic. In this program, we shall learn to code the alphabet triangle in C#.
Code:
Output:
Explanation:
- The program uses nested loops to control the printing of spaces and characters in each row of the alphabet triangle.
- The outer loop (i) represents the row number, and the inner loops control the printing of spaces, characters in increasing order, and characters in decreasing order.
- The characters are printed based on the ASCII values of 'A' and the loop indices.
Number Triangle
As we saw in the previous program how to make an alphabet triangle, in this program we shall learn to make a number triangle.
Code:
Output:
Explanation:
- The program uses nested loops to control the printing of spaces and numbers in each row of the number triangle.
- The outer loop (i) represents the row number, and the inner loops control the printing of spaces, numbers in increasing order, and numbers in decreasing order.
- The numbers are printed based on the loop indices.
Fibonacci Triangle
The Fibonacci number is a series of numbers where each number is the sum of two preceding ones. In this set, we shall start the number with 1 and then print the series in the form of a triangle. Here's the code for it.
Code:
Output:
Explanation:
- The program takes user input for the limit of the Fibonacci triangle.
- It uses nested loops to control the printing of Fibonacci numbers in each row of the triangle.
- The Fibonacci numbers are calculated by adding the previous two Fibonacci numbers (a and b), and the values are updated accordingly.
Palindrome Number
A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backwards, disregarding spaces, punctuation, and capitalization. In other words, it remains unchanged when read from left to right or right to left.
Some examples of palindromic words are "radar", "level", "madam", and "refer". Palindromic phrases include "A man, a plan, a canal, Panama!" and "Madam, in Eden, I'm Adam". In terms of numbers, examples of palindromic numbers are 121, 1221, and 12321.
Code:
Explanation:
The program checks if a given number is a palindrome. It reverses the number and compares it with the original. If they match, the number is a palindrome; otherwise, it's not. The logic involves extracting digits from right to left using the modulo operation % and building the reversed number. This reversed number is then compared with the original to determine if it's a palindrome or not.
Number in Characters
We shall now see a program which demonstrates how to represent the character by a number which gives us its place in the alphabet series.
Code:
Output:
Explanation:
- The program prompts the user to enter a character.
- The entered character is converted to uppercase for case-insensitivity.
- The program then uses the ToNumber function to find the corresponding numerical representation.
- If the input is a valid uppercase letter, the program prints the corresponding number; otherwise, it displays an error message.
Conclusion
-
C# stands out as a versatile and powerful programming language developed by Microsoft, seamlessly integrated into the .NET framework.
-
Renowned for its simplicity and combining the strengths of C++ and Java, it has become a staple for developers.
-
The practical examples covered a spectrum of programming topics, from mathematical operations like the Fibonacci series and prime number checks to pattern-based tasks such as creating triangles and handling character-to-number conversions.
-
These examples showcase C#'s applicability in various domains, establishing its significance in handling fundamental algorithms and patterns essential for developers in diverse software development scenarios.