C# Programming Example and Solutions
C# Programming Example and Solutions
TOPIC 1
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter first number: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
int num2 = Convert.ToInt32(Console.ReadLine());
int total = num2 + num1;
Console.WriteLine($"Total is {total}");
Console.ReadKey();
}
}
}
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Type value of number 1: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Type value of number 2: ");
int num2 = Convert.ToInt32(Console.ReadLine());
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter first number: ");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter second number: ");
double num2 = Convert.ToDouble(Console.ReadLine());
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter first number: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
int num2 = Convert.ToInt32(Console.ReadLine());
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter feet: ");
double feet = Convert.ToInt32(Console.ReadLine());
double meter = feet / 3.2808399;
Console.WriteLine($"\nFeet in meter: {meter}");
Console.ReadKey();
}
}
}
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Temperature in Celsius: ");
double celsius = Convert.ToDouble(Console.ReadLine());
double fahrenheit = (1.8 * celsius) + 32;
Console.WriteLine($"Temperature in Fahrenheit: {fahrenheit}");
Console.ReadKey();
}
}
}
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Temperature in Fahrenheit: ");
double fahrenheit = Convert.ToDouble(Console.ReadLine());
double celsius = (fahrenheit - 32) * 5 / 9;
Console.WriteLine($"Temperature in Celsius: {celsius}");
Console.ReadKey();
}
}
}
3
C# programming example and solutions
C# Program to find the Size of data types
using System;
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Size of char: {sizeof(char)}");
Console.WriteLine($"Size of Short: {sizeof(short)}");
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
char c;
Console.Write("Enter a character: ");
c = Convert.ToChar(Console.ReadLine());
Console.WriteLine($"\nASCII value of {c} is {Convert.ToInt32(c)}");
Console.ReadKey();
}
}
}
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Radius: ");
double radius = Convert.ToDouble(Console.ReadLine());
double area = Math.PI * radius * radius;
Console.WriteLine($"\nArea of circle: {area}");
Console.ReadKey();
}
}
}
4
C# programming example and solutions
C# Program to Calculate Area of Square
using System;
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the Length of Side: ");
double side = Convert.ToDouble(Console.ReadLine());
double area = side * side;
Console.WriteLine($"\nArea of Square: {area}");
Console.ReadKey();
}
}
}
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter length of rectangle: ");
double length = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter breadth of rectangle: ");
double breadth = Convert.ToDouble(Console.ReadLine());
namespace StudyCSharp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter days: ");
int days = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"Years: {years}");
Console.WriteLine($"weeks: {weeks}");
Console.WriteLine($"Days: {days}");
5
C# programming example and solutions
Console.ReadKey();
}
}
}
TOPIC 2
List of all conditional programs in c# language
namespace csharpprograms
{
class Program
{
static void Main(string[] args)
{
int number;
Console.Write("Enter a number: ");
number = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
}
}
}
namespace csharpprograms
{
class Program
{
static void Main(string[] args)
{
Console.Write("Input the 1st number: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the 2nd number: ");
int num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the 3rd number: ");
int num3 = Convert.ToInt32(Console.ReadLine());
6
C# programming example and solutions
if (num1 > num2)
{
if (num1 > num3)
{
Console.Write("The 1st number is the greatest among three. \n\n");
}
else
{
Console.Write("The 3rd number is the greatest among three. \n\n");
}
}
else if (num2 > num3)
Console.Write("The 2nd number is the greatest among three \n\n");
else
Console.Write("The 3rd number is the greatest among three \n\n");
Console.ReadLine();
}
}
}
namespace csharpprograms
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter three numbers:");
int num1 = Convert.ToInt32(Console.ReadLine());
int num2 = Convert.ToInt32(Console.ReadLine());
int num3 = Convert.ToInt32(Console.ReadLine());
int largest = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3 ?
num2 : num3);
Console.ReadLine();
}
}
}
C# Program to find the Largest among Three Variables using Nested if.
using System;
namespace csharpprograms
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter three numbers: \n");
int num1 = Convert.ToInt32(Console.ReadLine());
int num2 = Convert.ToInt32(Console.ReadLine());
int num3 = Convert.ToInt32(Console.ReadLine());
7
C# programming example and solutions
if (num1 >= num2)
{
if (num1 >= num3)
Console.WriteLine($"{num1} is the largest number");
else
Console.WriteLine($"{num3} is the largest number");
}
else
Console.WriteLine($"{num3} is the largest number");
Console.ReadLine();
}
}
}
namespace csharpprograms
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Year: ");
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine((year % 400 == 0) || ((year % 4 == 0) && (year % 100 !=
0)) ? "Entered year is a leap" : "Not leap year");
Console.ReadLine();
}
}
}
int i = ch;
if (i >= 48 && i <= 57)
Console.Write("You entered a number, please enter an alpahbet.");
else
{
switch (ch)
{
case 'a':
Console.WriteLine("The Alphabet is vowel");
break;
8
C# programming example and solutions
case 'i':
Console.WriteLine("The Alphabet is vowel");
break;
case 'o':
Console.WriteLine("The Alphabet is vowel");
break;
case 'u':
Console.WriteLine("The Alphabet is vowel");
break;
case 'e':
Console.WriteLine("The Alphabet is vowel");
break;
default:
Console.WriteLine("The Alphabet is not a vowel");
break;
}
}
Console.ReadLine();
}
}
if (num > 0)
{
Console.WriteLine("Enter number is positive ");
}
else if (num < 0)
{
Console.WriteLine("Enter number is nagative ");
}
else
{
Console.WriteLine("Enter number is zero ");
}
Console.ReadLine();
}
}
9
C# programming example and solutions
C# program to check uppercase or lowercase alphabets.
using System;
Console.ReadLine();
}
}
else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
Console.WriteLine(ch + " is Consonant.");
Console.ReadLine();
}
}
10
C# programming example and solutions
C# program to check whether a character is alphabet, digit or special
character.
using System;
Console.ReadLine();
}
}
int week;
if (week == 1)
Console.WriteLine("Monday");
else if (week == 2)
Console.WriteLine("Tuesday");
else if (week == 3)
Console.WriteLine("Wednesday");
else if (week == 4)
Console.WriteLine("Thursday");
else if (week == 5)
Console.WriteLine("Friday");
else if (week == 6)
Console.WriteLine("Saturday");
else if (week == 7)
Console.WriteLine("Sunday");
else
Console.WriteLine("Invalid Input! Please enter week in between 1-7.");
Console.ReadLine();
}
}
11
C# programming example and solutions
C# program to accept two integers and check whether they are equal or
not.
using System;
if (num1 == num2)
Console.WriteLine("Number1 and Number2 are equal\n");
else
Console.WriteLine("Number1 and Number2 are not equal\n");
Console.ReadLine();
}
}
int Candiateage;
Console.ReadLine();
}
}
12
C# programming example and solutions
C# program to find the eligibility of admission for an engineering course
based on the criteria.
using System;
Console.Write("\n\n");
Console.Write("Find eligibility for admission :\n");
Console.Write("----------------------------------");
Console.Write("\n\n");
if (m >= 65)
if (p >= 55)
if (c >= 50)
if ((m + p + c) >= 180 || (m + p) >= 140)
Console.Write("The candidate is eligible for admission.\n");
else
Console.Write("The candidate is not eligible.\n\n");
else
Console.Write("The candidate is not eligible.\n\n");
else
Console.Write("The candidate is not eligible.\n\n");
else
Console.Write("The candidate is not eligible.\n\n");
Console.ReadLine();
}
}
13
C# programming example and solutions
C# program to calculate the total marks, percentage and division of
student.
using System;
Console.Write("\n\n");
Console.Write("Calculate the total, percentage and division to take marks of
three subjects:\n");
Console.Write("---------------------------------------------------------------
----------------");
Console.Write("\n\n");
Console.ReadLine();
}
}
14
C# programming example and solutions
C# program to enter month number and print number of days in month.
using System;
if (month == 1)
Console.WriteLine("Enter month : January \nNo. of days : 31 days");
else if (month == 2)
Console.WriteLine("Enter month : February \nNo. of days : 28 or 29 days");
else if (month == 3)
Console.WriteLine("Enter month : March \nNo. of days : 31 days");
else if (month == 4)
Console.WriteLine("Enter month : April \nNo. of days : 30 days");
else if (month == 5)
Console.WriteLine("Enter month : May \nNo. of days : 31 days");
else if (month == 6)
Console.WriteLine("Enter month : June \nNo. of days : 30 days");
else if (month == 7)
Console.WriteLine("Enter month : July \nNo. of days : 31 days");
else if (month == 8)
Console.WriteLine("Enter month : August \nNo. of days : 31 days");
else if (month == 9)
Console.WriteLine("Enter month : September \nNo. of days : 30 days");
else if (month == 10)
Console.WriteLine("Enter month : October \nNo. of days : 31 days");
else if (month == 11)
Console.WriteLine("Enter month : November \nNo. of days : 30 days");
else if (month == 12)
Console.WriteLine("Enter month : December \nNo. of days : 31 days"); ;
else
Console.WriteLine("Invalid input! Please enter month number between (1-
12).");
Console.ReadLine();
}
}
int amount;
Console.ReadLine();
}
}
16
C# programming example and solutions
C# program to check whether a triangle can be formed by the given value
for the angles.
using System;
// Check whether sum=180 then its a valid triangle otherwise invalid triangle
if (sum == 180)
Console.WriteLine("It is a valid triangle.\n");
else
Console.WriteLine("It is a invalid triangle.\n");
Console.ReadLine();
}
}
TOPIC 3
List of C# Language Loop Programs with Examples
char ch;
Console.ReadLine();
}
}
17
C# programming example and solutions
Write C# program to print ASCII values of all characters
using System;
Console.ReadLine();
}
}
//Reading number
Console.WriteLine("Enter number to print table: ");
num = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
}
}
/*Running loop from the number entered by user, and Decrementing by 1*/
for (i = num; i >= 1; i--)
Console.WriteLine("\n" + i);
Console.ReadLine();
}
}
18
C# programming example and solutions
Write C# program to print sum of digits enter by user
using System;
//Reading number
Console.WriteLine("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
}
}
// Reading number
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
}
}
// Reading number
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
19
C# programming example and solutions
Console.ReadLine();
}
}
// Reading number
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
temp = num;
last = temp % 10;
count = (int)Math.Log10(temp);
Console.ReadLine();
}
}
Write C# program to find the sum of first and last digit of any number
using System;
// Reading number
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
firstDigit = num;
Console.ReadLine();
}
}
// Reading number
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
}
}
// Reading number
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
}
}
// Reading number
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
}
}
// Reading number
Console.Write("Enter any number: ");
basenumber = Convert.ToInt32(Console.ReadLine());
power = 1;
i = 1;
//caculatinh power of given number
while (i <= exponent)
{
power = power * basenumber;
i++;
}
22
C# programming example and solutions
Console.Write("Power : " + power);
Console.ReadLine();
}
}
// Reading number
Console.Write("Enter any number to calculate factorial: ");
num = Convert.ToInt32(Console.ReadLine());
fact = 1;
i = 1;
if (num == sum)
Console.WriteLine(num + " is an armstrong number.");
else
Console.WriteLine(num + " is not an armstrong number.");
Console.ReadLine();
}
}
23
C# programming example and solutions
Write C# program to find Armstrong numbers between 1 to n
using System;
// Reading number
Console.Write("Please Enter two numbers: ");
lower = Convert.ToInt32(Console.ReadLine());
higher = Convert.ToInt32(Console.ReadLine());
Console.Write("Armstrong numbers between " + lower + " and " + higher + " are:
");
Console.ReadLine();
}
}
24
C# programming example and solutions
Write C# program to calculate compound Interest
using System;
// ci=pow(intrest,time);
ci = 1;
for (a = 1; a <= time; a++)
ci = ci * intrest;
ci = amount * ci - amount;
Console.ReadLine();
}
}
//Reading number
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
f = 0;
i = 2;
while (i <= num / 2)
{
if (num % i == 0)
{
f = 1;
break;
25
C# programming example and solutions
}
i++;
}
if (f == 0)
Console.WriteLine(num + " is a Prime Number");
else
Console.WriteLine(num + " is not a Prime Number");
Console.ReadLine();
}
}
rev = num;
for (i = 0; num > 0; num = num / 10)
{
i = i * 10;
i = i + (num % 10);
}
Console.ReadLine();
}
}
while (n != 0)
{
num = (num * 10) + (n % 10);
n /= 10;
}
26
C# programming example and solutions
Console.ReadLine();
}
}
27
C# programming example and solutions
for (i = 1; i <= min; i++)
{
if (num1 % i == 0 && num2 % i == 0)
{
HCF = i;
}
}
Console.WriteLine("HCF of " + num1 + " and " + num2 + " is: " + HCF);
Console.ReadLine();
}
}
Console.ReadLine();
}
}
28
C# programming example and solutions
TOPIC 4
List of Switch case C# programs with an examples
switch (weeknumber)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
default:
Console.WriteLine("Invalid input! Please enter week no. between 1-
7.");
break;
}
Console.ReadLine();
}
}
30
C# programming example and solutions
Write C# program to create calculator using switch Statement
using System;
class Program
{
static void Main(string[] args)
{
int num1;
int num2;
string operand;
float answer;
switch (operand)
{
case "-":
answer = num1 - num2;
break;
case "+":
answer = num1 + num2;
break;
case "/":
answer = num1 / num2;
break;
case "*":
answer = num1 * num2;
break;
default:
answer = 0;
break;
}
Console.WriteLine($"{num1} {operand} {num2} = {answer}");
Console.ReadLine();
}
}
class Program
{
31
C# programming example and solutions
switch (num % 2)
{
//If n%2 == 0
case 0:
Console.WriteLine(num + " is even number");
break;
//Else if n%2 == 1
case 1:
Console.WriteLine(num + " is odd number");
break;
}
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
char ch;
32
C# programming example and solutions
default:
Console.WriteLine("consonant");
break;
}
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
char gender;
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
int num1 = 0, num2 = 0;
33
C# programming example and solutions
// Condition to check maximum number
switch (num1 > num2)
{
case true:
Console.WriteLine(num1 + " is Maximum number");
break;
case false:
Console.WriteLine(num2 + " is Maximum number");
break;
}
Console.ReadLine();
}
}
TOPIC 5
List of C# language array programs with an examples
class Program
{
static void Main()
{
int[] arrayA = new int[10];
int lengthA = arrayA.Length;
Console.WriteLine("Length of ArrayA : {0}", +lengthA);
long longLength = arrayA.LongLength;
Console.WriteLine("Length of the Long Length Array : {0}", longLength);
int[,] twoD = new int[20, 50];
Console.WriteLine("The Size of 2D Array is : {0}", twoD.Length);
Console.ReadLine();
}
}
namespace Program
{
class twodmatrix
{
int p, r;
int[,] a;
int[] b;
twodmatrix(int x, int y)
{
p = x;
r = y;
a = new int[p, r];
b = new int[p * r];
}
34
C# programming example and solutions
public void readmatrix()
{
for (int i = 0; i < p; i++)
{
for (int j = 0; j < r; j++)
{
Console.Write("a[{0},{1}]=", i, j);
a[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
}
public void printtwodimentionalarray()
{
for (int i = 0; i < p; i++)
{
for (int j = 0; j < r; j++)
{
Console.Write("{0}\t", a[i, j]);
}
Console.Write("\n");
}
}
public void convert()
{
int k = 0;
for (int i = 0; i < p; i++)
{
for (int j = 0; j < r; j++)
{
b[k++] = a[i, j];
}
}
}
public void printonedimentionalarray()
{
for (int i = 0; i < p * r; i++)
{
Console.WriteLine("{0}\t", b[i]);
}
}
35
C# programming example and solutions
Write C# Program to Demonstrate Jagged Arrays
using System;
class Program
{
static void Main()
{
int[][] jagArray = new int[3][];
jagArray[0] = new int[2];
jagArray[0][0] = 12;
jagArray[0][1] = 13;
jagArray[1] = new int[1] { 12 };
jagArray[2] = new int[3] { 15, 16, 17 };
Console.ReadLine();
}
}
class Program
{
static void Main()
{
int[] Arr = { 20, -10, -30, 0, 15, 10, 30 };
Console.ReadLine();
}
}
class Program
{
static void Main()
{
int[] arr = new int[100];
int i, num;
36
C# programming example and solutions
//Reading elements of array
Console.WriteLine("Enter elements in array: ");
for (i = 0; i < num; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
class Program
{
static void Main()
{
int[] arr = new int[100];
int i, num, sum = 0;
Console.ReadLine();
}
}
37
C# programming example and solutions
Write C# program to count even and odd elements in an array
using System;
class Program
{
static void Main()
{
int[] arr = new int[100];
int i, num, evennum, oddnum;
Console.ReadLine();
}
}
class Program
{
static void Main()
{
int[] arr = new int[100];
int i, num, size, position;
38
C# programming example and solutions
//Reading element to insert & position of the element
class Program
{
static void Main()
{
int[] arr = new int[100]; ;
int i, j, k, size, isUnique;
39
C# programming example and solutions
//Removing all duplicate elements from the array
for (i = 0; i < size; i++)
{
// Assuming cuurent element is unique */
isUnique = 1;
if (arr[i] == arr[j])
{
// Removing duplicate element
for (k = j; k < size - 1; k++)
{
arr[k] = arr[k + 1];
}
size--;
j--;
isUnique = 0;
}
}
/*
If array element is not unique
then also remove the current element
*/
if (isUnique != 1)
{
for (j = i; j < size - 1; j++)
{
arr[j] = arr[j + 1];
}
size--;
i--;
}
}
40
C# programming example and solutions
Write C# program to sort an array in ascending order
using System;
class Program
{
static void Main()
{
int[] arr = new int[100]; ;
int size, i, j, temp;
class Program
{
static void Main()
{
int[] arr = new int[100];
int i, num;
41
C# programming example and solutions
Console.ReadLine();
}
}
class Program
{
static void Main()
{
int i, j, count, num;
42
C# programming example and solutions
for (i = 0; i < num; i++)
{
count = 1;
for (j = i + 1; j < num; j++)
{
//If duplicate element is found
if (arr[i] == arr[j])
{
count++;
Console.ReadLine();
}
}
class Program
{
static void Main()
{
int[] arr = new int[100];
int num; // Total number of elements in array
int i, j, k;
43
C# programming example and solutions
// Delete the current duplicate element
for (k = j; k < num; k++)
{
arr[k] = arr[k + 1];
}
class Program
{
static void Main()
{
int[] arr = new int[100];
int i, j, num, count = 0;
Console.ReadLine();
}
}
44
C# programming example and solutions
Write C# program to merge two sorted array
using System;
class Program
{
static void Main()
{
int[] arr1 = new int[100];
int[] arr2 = new int[100];
int[] mergeArray = new int[100];
45
C# programming example and solutions
//Merging the remaining elements of array
while (index1 < size1)
{
mergeArray[mergeIndex] = arr1[index1];
mergeIndex++;
index1++;
}
while (index2 < size2)
{
mergeArray[mergeIndex] = arr2[index2];
mergeIndex++;
index2++;
}
Console.ReadLine();
}
}
Write C# Program to Find the Average Values of all the Array Elements
using System;
class Program
{
public void sumAverageElements(int[] arr, int size)
{
int sum = 0;
int average = 0;
for (int i = 0; i < size; i++)
sum += arr[i];
average = sum / size;
Console.WriteLine("Sum Of Array is : " + sum);
Console.WriteLine("Average Of Array is : " + average);
Console.ReadLine();
}
public static void Main(string[] args)
{
int size;
Console.WriteLine("Enter the Size :");
size = Convert.ToInt32(Console.ReadLine());
int[] a = new int[size];
Console.WriteLine("Enter the Elements of the Array : ");
for (int i = 0; i < size; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
int len = a.Length;
Program p = new Program();
p.sumAverageElements(a, len);
}
}
46
C# programming example and solutions
Write C# program to find reverse of an array
using System;
class Program
{
static void Main()
{
int[] array = new int[100];
int size, i;
Console.ReadLine();
}
}
47
C# programming example and solutions
// ignore the largest element and find the 2nd largest element in the array
lrg2nd = 0;
for (i = 0; i < num; i++)
{
if (i == j)
{
i++; //ignoring the largest element
i--;
}
else
{
if (lrg2nd < arr1[i])
{
lrg2nd = arr1[i];
}
}
}
TOPIC 6
All function programs in c# with an examples
Console.ReadLine();
}
}
48
C# programming example and solutions
Write a C# program to add two numbers using function
using System;
Console.ReadLine();
}
}
Console.ReadLine();
}
}
49
C# programming example and solutions
Write a C# program to find even or odd number using function
using System;
namespace Functionexcercise
{
class Program
{
CheckNumIsEvenOrOdd(number);
Console.ReadLine();
}
50
C# programming example and solutions
Write a C# program to create a function to check whether a number is
prime or not
using System;
if (checkprime(n))
Console.WriteLine(n + " is a prime number");
else
Console.WriteLine(n + " is not a prime number");
Console.ReadLine();
}
}
class functionexcercise
{
public static int Fibonaccisequence(int number)
{
int num1 = 0;
int num2 = 1;
51
C# programming example and solutions
Write a C# program to create a function to swap the values of two integer
numbers
using System;
newnum = num1;
num1 = num2;
num2 = newnum;
}
public static void Main()
{
int n1, n2;
Console.Write("Enter two number: ");
n1 = Convert.ToInt32(Console.ReadLine());
n2 = Convert.ToInt32(Console.ReadLine());
interchangenumber(ref n1, ref n2);
Console.WriteLine("After swapping the 1st number is : {0} , and the 2nd number
is : {1}", n1, n2);
Console.ReadLine();
}
}
class functionexcercise
{
static void Main()
{
decimal fact;
Console.Write("Enter a number : ");
int num = Convert.ToInt32(Console.ReadLine());
fact = Factorial(num);
Console.WriteLine("The factorial of number {0} is {1}", num, fact);
Console.ReadLine();
}
static decimal Factorial(int n1)
{
// The bottom of the recursion
if (n1 == 0)
{
return 1;
}
// Recursive call: the method calls itself
else
{
return n1 * Factorial(n1 - 1);
}
}
}
52
C# programming example and solutions
Write a C# program to Print Binary Equivalent of an Integer using
Recursion
using System;
class Program
{
public static int binaryconversion(int num)
{
int bin;
if (num != 0)
{
bin = (num % 2) + 10 * binaryconversion(num / 2);
Console.Write(bin);
return 0;
}
else
{
return 0;
}
}
public static void Main(string[] args)
{
int num;
Console.WriteLine("Enter a decimal number: ");
num = int.Parse(Console.ReadLine());
Console.Write("The binary equivalent of num is :");
binaryconversion(num);
Console.ReadLine();
}
}
TOPIC 7
class LinqExercise
{
static void Main()
{
int[] numbers = {1, 3, 6, 9, 10, -4, -2, -3, -88, 12, 19, 14
};
var nQuery =
from VrNum in numbers
where VrNum > 0
where VrNum < 10
53
C# programming example and solutions
select VrNum;
Console.Write("\nThe numbers within the range of 1 to 10 are : \n");
foreach (var VrNum in nQuery)
{
Console.Write("{0} ", VrNum);
}
Console.ReadLine();
}
}
class LinqExercise
{
static void Main(string[] args)
{
string[] dayWeek = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday" };
class LinqExercise
{
static void Main(string[] args)
{
List<int> list = new List<int>();
list.Add(10);
list.Add(20);
list.Add(30);
list.Add(40);
list.Add(90);
list.Add(50);
list.Add(70);
class LinqExercise
{
static void Main(string[] args)
{
Console.ReadLine();
}
}
class LinqExercise11
{
static void Main(string[] args)
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
list.Add(5);
list.Add(6);
list.Add(7);
list.Add(8);
list.Add(9);
list.Add(10);
list.Sort();
list.Reverse();
Console.Write("The top {0} records from the list are: \n", num);
55
C# programming example and solutions
foreach (int topn in list.Take(num))
Console.WriteLine(topn);
Console.ReadLine();
}
}
class LinqExercise
{
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 4, 5, 2, 3, 10 };
Console.Write("The numbers in the array are : \n");
Console.Write("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 4, 5, 2, 3, 10 \n");
Console.ReadLine();
}
}
class LinqExercise
{
static void Main(string[] args)
{
string str;
56
C# programming example and solutions
Write a program in C# to find the uppercase words in a string in LINQ
Query
using System;
using System.Linq;
using System.Collections.Generic;
class LinqExercise
{
static void Main(string[] args)
{
string str;
Console.Write("Input the string : ");
str = Console.ReadLine();
return upWord;
}
}
class LinqExercise
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("a");
list.Add("b");
list.Add("c");
list.Add("d");
list.Add("e");
57
C# programming example and solutions
string newstr = list.FirstOrDefault(n => n == "c");
list.Remove(newstr);
Console.ReadLine();
}
}
class LinqExercise21
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("a");
list.Add("b");
list.Add("c");
list.Add("d");
list.Add("e");
list.Add("f");
list.RemoveRange(1, 4);
Console.ReadLine();
}
}
58
C# programming example and solutions
Write a program in C# to generate a Cartesian Product of two sets in LINQ
Query
using System;
using System.Linq;
using System.Collections.Generic;
class LinqExercise23
{
public static void Main(string[] args)
{
char[] charlist = { 'A', 'B', 'C' };
int[] numlist = { 1, 2, 3 };
class LinqExercise
{
static void Main(string[] args)
{
var itemlist = (from c in Item_Mast.Getitem()
select c.ItemDes)
.Distinct()
.OrderBy(x => x);
59
C# programming example and solutions
list.Add(new Item_Mast() { ItemId = 6, ItemDes = "AUDI" });
list.Add(new Item_Mast() { ItemId = 5, ItemDes = "JAGUAR" });
list.Add(new Item_Mast() { ItemId = 6, ItemDes = "FERRARI" });
return list;
}
}
TOPIC 8
All string programs in C# with examples
Console.ReadLine();
}
}
60
C# programming example and solutions
Write a C# program to find the length of a string without using library
function
using System;
Console.ReadLine();
}
}
while (i < l)
{
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
alphabet++;
else if (str[i] >= '0' && str[i] <= '9')
digit++;
else
specialchar++;
i++;
}
Console.ReadLine();
}
}
61
C# programming example and solutions
Write a C# program to print individual characters of the string in reverse
order
using System;
length = str.Length - 1;
Console.Write("The characters of the string in reverse are : \n");
while (length >= 0)
{
Console.Write("{0} ", str[length]);
length--;
}
Console.ReadLine();
}
}
length = str1.Length;
string[] str2 = new string[length];
Console.ReadLine();
}
}
62
C# programming example and solutions
Write a C# program to count a total number of vowel or consonant in a
string
using System;
vowel = 0;
consonant = 0;
length = str.Length;
Console.ReadLine();
}
}
63
C# programming example and solutions
Write a C# program to find maximum occurring character in a string
using System;
i++;
}
max = 0;
for (i = 0; i < 255; i++)
{
if (i != 32)
{
if (frequency[i] > frequency[max])
max = i;
}
}
Console.Write("The Highest frequency of character '{0}' is appearing for
number of times : {1} \n\n", (char)max, frequency[max]);
Console.ReadLine();
}
}
Write a C# program to read a string through the keyboard and sort it using
bubble sort
using System;
64
C# programming example and solutions
num = Convert.ToInt32(Console.ReadLine());
arr1 = new string[num];
Console.Write("Input {0} strings below :\n", num);
for (i = 0; i < num; i++)
{
arr1[i] = Console.ReadLine();
}
lenghth = arr1.Length;
Console.ReadLine();
}
}
Console.ReadLine();
}
}
65
C# programming example and solutions
Write a C# program to compare (less than, greater than, equal to ) two
substrings
using System;
class StringExample
{
public static void Main()
{
string str1 = "computer";
string str2 = "system";
string str;
int result;
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
result = string.Compare(str1, 2, str2, 0, 2);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal
to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
Console.ReadLine();
}
}
Console.ReadLine();
}
}
66
C# programming example and solutions
Write a C# program to check the username and password
using System;
}
while ((username != "admin" || password != "123456") && (ctr != 3));
if (ctr == 3)
Console.WriteLine("\nLogin attemp three or more times. Try later!");
else
Console.WriteLine("\nThe password entered successfully!");
Console.ReadLine();
}
}
67
C# programming example and solutions
else
Console.Write(Char.ToLower(ch)); // Converts uppercase character to
lowercase.
}
Console.ReadLine();
}
}
Console.ReadLine();
}
}
68