C#: 100 Simple Codes
C#: 100 Simple Codes
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, World!");
}
}
C#:
using System;
class Program {
static void Main() {
int a = 5;
int b = 3;
Console.WriteLine("Sum: " + (a + b));
}
}
using System;
class Program {
static void Main() {
int x = 10, y = 20;
int temp = x;
x = y;
y = temp;
Console.WriteLine("x: " + x + ", y: " + y);
}
}
4. Check Even or Odd
C#:
using System;
class Program {
static void Main() {
int num = 7;
if (num % 2 == 0)
Console.WriteLine("Even");
else
Console.WriteLine("Odd");
}
}
using System;
class Program {
static void Main() {
int n = 5;
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
Console.WriteLine("Factorial: " + result);
}
}
6. Fibonacci Sequence
C#:
using System;
class Program {
static void Main() {
int a = 0, b = 1, n = 10;
for (int i = 0; i < n; i++) {
Console.Write(a + " ");
int temp = a + b;
a = b;
b = temp;
}
}
}
using System;
class Program {
static void Main() {
int num = 7;
bool isPrime = true;
for (int i = 2; i < num; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
Console.WriteLine(isPrime ? "Prime" : "Not Prime");
}
}
8. Find Max of 3 Numbers
C#:
using System;
class Program {
static void Main() {
int a = 5, b = 8, c = 3;
int max = Math.Max(a, Math.Max(b, c));
Console.WriteLine("Max: " + max);
}
}
9. Simple Calculator
C#:
using System;
class Program {
static void Main() {
int a = 6, b = 2;
Console.WriteLine("Add: " + (a + b));
Console.WriteLine("Subtract: " + (a - b));
Console.WriteLine("Multiply: " + (a * b));
Console.WriteLine("Divide: " + (a / b));
}
}
using System;
class Program {
static void Main() {
int num = -4;
if (num > 0)
Console.WriteLine("Positive");
else if (num < 0)
Console.WriteLine("Negative");
else
Console.WriteLine("Zero");
}
}
11. Sum of Numbers in an Array
C#:
using System;
class Program {
static void Main() {
int[] numbers = { 1, 2, 3, 4, 5 };
int sum = 0;
foreach (int n in numbers) {
sum += n;
}
Console.WriteLine("Sum: " + sum);
}
}
using System;
class Program {
static void Main() {
string text = "hello";
char[] chars = text.ToCharArray();
Array.Reverse(chars);
Console.WriteLine(new string(chars));
}
}
13. Count Vowels in String
C#:
using System;
class Program {
static void Main() {
string input = "education";
int count = 0;
foreach (char c in input.ToLower()) {
if ("aeiou".Contains(c)) {
count++;
}
}
Console.WriteLine("Vowel count: " + count);
}
}
using System;
class Program {
static void Main() {
string word = "madam";
string reversed = new string(word.Reverse().ToArray());
Console.WriteLine(word == reversed ? "Palindrome" : "Not
Palindrome");
}
}
15. Find Minimum in Array
C#:
using System;
class Program {
static void Main() {
int[] numbers = { 9, 3, 6, 2, 8 };
int min = numbers[0];
foreach (int n in numbers) {
if (n < min) min = n;
}
Console.WriteLine("Minimum: " + min);
}
}
using System;
class Program {
static void Main() {
int year = 2024;
bool isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 ==
0);
Console.WriteLine(isLeap ? "Leap Year" : "Not Leap Year");
}
}
using System;
class Program {
static void Main() {
int num = 5;
for (int i = 1; i <= 10; i++) {
Console.WriteLine($"{num} x {i} = {num * i}");
}
}
}
18. Simple For Loop
C#:
using System;
class Program {
static void Main() {
for (int i = 1; i <= 5; i++) {
Console.WriteLine("Iteration: " + i);
}
}
}
using System;
class Program {
static void Main() {
int i = 1;
while (i <= 5) {
Console.WriteLine("Count: " + i);
i++;
}
}
}
using System;
class Program {
static void Main() {
int i = 1;
do {
Console.WriteLine("Do count: " + i);
i++;
} while (i <= 5);
}
}
21. Check if Number is Prime (Function)
C#:
using System;
class Program {
static bool IsPrime(int num) {
if (num < 2) return false;
for (int i = 2; i <= Math.Sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
using System;
class Program {
static void Main() {
string text = "Hello, world!";
Console.WriteLine("Length: " + text.Length);
}
}
using System;
class Program {
static void Main() {
string text = "hello";
Console.WriteLine(text.ToUpper());
}
}
24. Convert String to Lowercase
C#:
using System;
class Program {
static void Main() {
string text = "HELLO";
Console.WriteLine(text.ToLower());
}
}
using System;
class Program {
static void Main() {
double result = Math.Pow(2, 3);
Console.WriteLine("2^3 = " + result);
}
}
using System;
class Program {
static void Main() {
double num = 25;
Console.WriteLine("Square root: " + Math.Sqrt(num));
}
}
27. Check if Character is Digit
C#:
using System;
class Program {
static void Main() {
char ch = '7';
Console.WriteLine(Char.IsDigit(ch) ? "Digit" : "Not a digit");
}
}
using System;
class Program {
static void Main() {
char ch = 'A';
Console.WriteLine(Char.IsLetter(ch) ? "Letter" : "Not a letter");
}
}
using System;
class Program {
static void Main() {
int num = 1234;
int reversed = 0;
while (num > 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
Console.WriteLine("Reversed: " + reversed);
}
}
30. Check Armstrong Number (3-digit)
C#:
using System;
class Program {
static void Main() {
int num = 153, sum = 0, temp = num;
while (temp > 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
Console.WriteLine(sum == num ? "Armstrong" : "Not Armstrong");
}
}
using System;
class Program {
static bool IsEven(int num) {
return num % 2 == 0;
}
using System;
using System.Linq;
class Program {
static void Main() {
int[] numbers = { 10, 20, 30, 40 };
double avg = numbers.Average();
Console.WriteLine("Average: " + avg);
}
}
using System;
class Program {
static void Main() {
Console.WriteLine("1. Add\n2. Subtract");
int choice = int.Parse(Console.ReadLine());
int a = 10, b = 5;
switch (choice) {
case 1:
Console.WriteLine("Sum: " + (a + b));
break;
case 2:
Console.WriteLine("Difference: " + (a - b));
break;
default:
Console.WriteLine("Invalid choice");
break;
}
}
}
34. Find the Largest Element in Array
C#:
using System;
class Program {
static void Main() {
int[] arr = { 2, 9, 5, 1, 7 };
int max = arr[0];
foreach (int n in arr) {
if (n > max) max = n;
}
Console.WriteLine("Max: " + max);
}
}
using System;
class Program {
static void Main() {
string first = "Hello";
string second = "World";
string result = first + " " + second;
Console.WriteLine(result);
}
}
using System;
class Program {
static void Main() {
int[] arr = { 1, 3, 5, 7 };
int target = 5;
bool found = Array.Exists(arr, element => element == target);
Console.WriteLine(found ? "Found" : "Not Found");
}
}
37. Sort Array Elements
C#:
using System;
class Program {
static void Main() {
int[] arr = { 4, 2, 8, 1 };
Array.Sort(arr);
Console.WriteLine("Sorted: " + string.Join(", ", arr));
}
}
using System;
class Program {
static void Main() {
string str = "banana";
char target = 'a';
int count = 0;
using System;
class Program {
static void Main() {
int age = 18;
string result = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine(result);
}
}
40. Simple Try-Catch for Error Handling
C#:
using System;
class Program {
static void Main() {
try {
int a = 10, b = 0;
int result = a / b;
Console.WriteLine(result);
} catch (DivideByZeroException) {
Console.WriteLine("Cannot divide by zero");
}
}
}
using System;
class Program {
static void Main() {
int number = 123;
string str = number.ToString();
Console.WriteLine("String: " + str);
}
}
using System;
class Program {
static void Main() {
string str = "456";
int num = int.Parse(str);
Console.WriteLine("Integer: " + num);
}
}
43. Check if String is Null or Empty
C#:
using System;
class Program {
static void Main() {
string text = "";
Console.WriteLine(string.IsNullOrEmpty(text) ? "Empty" : "Not
Empty");
}
}
using System;
class Program {
static void Main() {
string text = " ";
Console.WriteLine(string.IsNullOrWhiteSpace(text) ? "Blank" : "Not
Blank");
}
}
using System;
class Program {
static void Main() {
double num = 4.67;
Console.WriteLine("Rounded: " + Math.Round(num));
}
}
46. Use Math.Floor()
C#:
using System;
class Program {
static void Main() {
double num = 4.99;
Console.WriteLine("Floor: " + Math.Floor(num));
}
}
using System;
class Program {
static void Main() {
double num = 4.01;
Console.WriteLine("Ceiling: " + Math.Ceiling(num));
}
}
using System;
class Program {
static void Main() {
Random rnd = new Random();
int num = rnd.Next(1, 101); // 1 to 100
Console.WriteLine("Random number: " + num);
}
}
49. Use foreach with Array
C#:
using System;
class Program {
static void Main() {
string[] fruits = { "Apple", "Banana", "Cherry" };
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
}
}
using System;
class Program {
static void Main() {
double celsius = 25;
double fahrenheit = (celsius * 9 / 5) + 32;
Console.WriteLine("Fahrenheit: " + fahrenheit);
}
}
using System;
class Program {
static void Main() {
double fahrenheit = 77;
double celsius = (fahrenheit - 32) * 5 / 9;
Console.WriteLine("Celsius: " + celsius);
}
}
52. Check if Number is Positive
C#:
using System;
class Program {
static void Main() {
int num = 10;
Console.WriteLine(num > 0 ? "Positive" : "Not Positive");
}
}
using System;
class Program {
static void Main() {
int num = -5;
Console.WriteLine(num < 0 ? "Negative" : "Not Negative");
}
}
using System;
class Program {
static void Main() {
char ch = 'A';
int ascii = (int)ch;
Console.WriteLine("ASCII: " + ascii);
}
}
55. Convert ASCII to Char
C#:
using System;
class Program {
static void Main() {
int ascii = 66;
char ch = (char)ascii;
Console.WriteLine("Char: " + ch);
}
}
using System;
class Program {
static void Main() {
for (int i = 1; i <= 20; i++) {
if (i % 2 == 0)
Console.Write(i + " ");
}
}
}
using System;
class Program {
static void Main() {
for (int i = 1; i <= 20; i++) {
if (i % 2 != 0)
Console.Write(i + " ");
}
}
}
58. Check if Character is Uppercase
C#:
using System;
class Program {
static void Main() {
char ch = 'G';
Console.WriteLine(char.IsUpper(ch) ? "Uppercase" : "Not Uppercase");
}
}
using System;
class Program {
static void Main() {
char ch = 'g';
Console.WriteLine(char.IsLower(ch) ? "Lowercase" : "Not Lowercase");
}
}
using System;
class Program {
static void Main() {
int num = 1234;
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
Console.WriteLine("Sum of digits: " + sum);
}
}
61. Factorial Using Recursion
C#:
using System;
class Program {
static int Factorial(int n) {
if (n == 0) return 1;
return n * Factorial(n - 1);
}
using System;
class Program {
static void Main() {
string word = "hello";
string reversed = "";
using System;
class Program {
static int GCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
using System;
class Program {
static int GCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
using System;
class Program {
static void Main() {
int n = 10;
int a = 0, b = 1;
using System;
class Program {
static void Main() {
string text = "hello world";
string replaced = text.Replace('o', 'x');
Console.WriteLine(replaced);
}
}
67. Find Maximum of Two Numbers Using Function
C#:
using System;
class Program {
static int Max(int a, int b) {
return (a > b) ? a : b;
}
using System;
class Program {
static void Main() {
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
Console.WriteLine($"a = {a}, b = {b}");
}
}
69. Swap Two Numbers Without Temp
C#:
using System;
class Program {
static void Main() {
int a = 3, b = 7;
a = a + b;
b = a - b;
a = a - b;
Console.WriteLine($"a = {a}, b = {b}");
}
}
using System;
class Program {
static void Main() {
int a = 10, b = 5;
Console.WriteLine("Add: " + (a + b));
Console.WriteLine("Subtract: " + (a - b));
Console.WriteLine("Multiply: " + (a * b));
Console.WriteLine("Divide: " + (a / b));
}
}
71. Count Words in a Sentence
C#:
using System;
class Program {
static void Main() {
string sentence = "C# is a great language";
string[] words = sentence.Split(' ');
Console.WriteLine("Word count: " + words.Length);
}
}
using System;
class Program {
static void Main() {
string word = "madam";
char[] arr = word.ToCharArray();
Array.Reverse(arr);
string reversed = new string(arr);
using System;
class Program {
static void Main() {
int year = 2024;
bool isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 ==
0);
Console.WriteLine(isLeap ? "Leap Year" : "Not Leap Year");
}
}
using System;
class Program {
static void Main() {
Console.WriteLine("Now: " + DateTime.Now);
}
}
using System;
class Program {
static void Main() {
DateTime future = DateTime.Now.AddDays(5);
Console.WriteLine("Future date: " + future.ToShortDateString());
}
}
76. Check if Number is a Perfect Number
C#:
using System;
class Program {
static void Main() {
int num = 28, sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0) sum += i;
}
Console.WriteLine(sum == num ? "Perfect Number" : "Not Perfect");
}
}
using System;
class Program {
static void Main() {
int num = 121, original = num, reversed = 0;
using System;
class Program {
static void Main() {
string text = "education";
int count = 0;
foreach (char c in text.ToLower()) {
if ("aeiou".Contains(c)) count++;
}
Console.WriteLine("Vowels: " + count);
}
}
using System;
class Program {
static void Main() {
int[] arr = { 1, 2, 3, 4, 5 };
Array.Reverse(arr);
Console.WriteLine("Reversed: " + string.Join(", ", arr));
}
}
80. Print Multiplication Table
C#:
using System;
class Program {
static void Main() {
int num = 7;
for (int i = 1; i <= 10; i++) {
Console.WriteLine($"{num} x {i} = {num * i}");
}
}
}
using System;
class Program {
static void Main() {
int num = 153, sum = 0, temp = num;
while (temp > 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
Console.WriteLine(sum == num ? "Armstrong" : "Not Armstrong");
}
}
82. Find Power of a Number
C#:
using System;
class Program {
static void Main() {
double baseNum = 2;
double exponent = 4;
double result = Math.Pow(baseNum, exponent);
Console.WriteLine("Power: " + result);
}
}
using System;
class Program {
static void Main() {
int i = 1;
while (i <= 5) {
Console.WriteLine(i);
i++;
}
}
}
84. Print Numbers Using Do-While Loop
C#:
using System;
class Program {
static void Main() {
int i = 1;
do {
Console.WriteLine(i);
i++;
} while (i <= 5);
}
}
using System;
class Program {
static void Main() {
int day = 2;
switch (day) {
case 1: Console.WriteLine("Sunday"); break;
case 2: Console.WriteLine("Monday"); break;
case 3: Console.WriteLine("Tuesday"); break;
default: Console.WriteLine("Other day"); break;
}
}
}
86. Find Max in Array
C#:
using System;
class Program {
static void Main() {
int[] numbers = { 3, 7, 2, 9, 5 };
int max = numbers[0];
using System;
class Program {
static void Main() {
int[] numbers = { 3, 7, 2, 9, 5 };
int min = numbers[0];
using System;
class Program {
static void Main() {
int[] numbers = { 1, 2, 3, 4, 5 };
int even = 0, odd = 0;
using System;
class Program {
static void Main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
Console.Write("*");
}
Console.WriteLine();
}
}
}
90. Print Inverted Star Triangle
C#:
using System;
class Program {
static void Main() {
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
Console.Write("*");
}
Console.WriteLine();
}
}
}
using System;
class Program {
static void Main() {
char ch = '5';
Console.WriteLine(char.IsDigit(ch) ? "Digit" : "Not a Digit");
}
}
92. Check if Character is a Letter
C#:
using System;
class Program {
static void Main() {
char ch = 'A';
Console.WriteLine(char.IsLetter(ch) ? "Letter" : "Not a Letter");
}
}
using System;
class Program {
static void Main() {
string sentence = "Learning C# is fun!";
Console.WriteLine(sentence.Contains("C#") ? "Contains C#" : "Does not
contain C#");
}
}
94. Join Array of Strings
C#:
using System;
class Program {
static void Main() {
string[] words = { "Hello", "World", "CSharp" };
string result = string.Join(" ", words);
Console.WriteLine(result);
}
}
using System;
class Program {
static void Main() {
string sentence = "C# is awesome";
string[] words = sentence.Split(' ');
foreach (string word in words) {
Console.WriteLine(word);
}
}
}
96. Check if Number is Prime
C#:
using System;
class Program {
static void Main() {
int num = 7;
bool isPrime = true;
using System;
class Program {
static void Main() {
string word = "hello";
foreach (char c in word) {
Console.WriteLine(c);
}
}
}
using System;
class Program {
static void Main() {
string text = "C# is fun";
string noSpaces = text.Replace(" ", "");
Console.WriteLine("Without spaces: " + noSpaces);
}
}
99. Count Occurrence of a Character
C#:
using System;
class Program {
static void Main() {
string text = "banana";
char target = 'a';
int count = 0;
using System;
class Program {
static void Main() {
string username = "admin";
string password = "1234";
Email:
souhaillaghchim.dev@proton.me
Blogger:
https://souhaillaghchimdev.blogspot.com/