Educational Applications Using C# 2.0 84
Educational Applications Using C# 2.0
(Volume 1)
© Pinumalla Prashant
Author : Pinumalla Prashant
Education : B.Tech.(c.s.e)
College : Jyothishmathi Institute of Technology and Science
Designation : System Designer in Centre for Good Governance
Dedicated : This book is dedicated to my family members namely
P. Veda Prakash, P. Saroja, P.Praveen, P.Mayuri
Foreword
I had developed different educational applications in c#2.0, which will be useful for students and other aspirants to know the subject easily and to understand easily. To understand the subject, I had given description for each educational application. The main purpose of developing these applications is to spread education into the people, so that everyone understands the concept easily and quickly. For this purpose, I’am giving coding with comments and screenshots and description, so that young aspirants takes this as a challenge and develops many applications in this area.
If any mistakes are found in this book, please bring to notice to my email-id or .
Projects List:- Page No.
1) Addition with carry 05
2) Alphabets sorter 09
3) Arithmetic progression series 12
4) ASCII table 15
5) Binary addition 20
6) Binary to decimal calculator 25
7) Check even or odd 28
8) Check prime number 31
9) Checksum of text string 34
10) Colors 37
11) Constant values checking 48
12) Convert Binary IP to IP address 51
13) Convert domain name into IP address 53
14) Convert text into selected font 56
15) Counting 58
16) Date formats 61
17) Decimal to binary calculator 64
18) Decimal to hex 67
19) Decimal to Oct 70
20) Different shapes 73
21) Directions of the compass 76
22) Disappear game 81
23) Distinguish alphabet 84
24) Distinguish numbers 89
#1#
Project Title: Addition with Carry
Description : This project is aimed for calculating addition of numbers which is in the format like 100+23+45 etc. . In this addition of numbers carry of specific digits of numbers will be calculated and displayed in the screen. In this project you can know the carries generated by addition of numbers and total of those numbers.
Screenshot :
Code :
#region "Import namespaces"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
#endregion
#region "Addtion with carry"
namespace AdditionWithCarry
{
#region "class AdditionWithCarry"
public partial class AdditionWithCarry : Form
{
#region "Constructor"
//Constructor
public AdditionWithCarry()
{
InitializeComponent();
}
#endregion
#region "Button calculate click event"
//Button click event.
private void btnCalculate_Click(object sender, EventArgs e)
{
// calling method to calculate the addition of numbers in the format
// like 100+23+34
this.AdditionWithCarryMethod(txtAddition.Text);
}
#endregion
#region "Addition with carry method"
//
public void AdditionWithCarryMethod(string strValues)
{
try
{
// class which takes regualr expression for pattern matching.
Regex r1 = new Regex(@"^([0-9]+\+[0-9]+)+$");
if (!r1.IsMatch(strValues))
{
MessageBox.Show("Invalid string. Enter in the format like 100+23+34");
lblAddition.Text = "";
txtAddition.Focus();
return;
}
// getting separate values from input string which is in the format like 100+23+34 //etc.
string[] vals = strValues.Split('+');
long maxlength = 0;
long TotalAddition = 0;
// Finding maxlength of each separated each numeric value and total value of those numbers.
for (long i = 0; i < vals.Length; i++)
{
if (vals[i].Trim().ToString().Length > maxlength)
maxlength = vals[i].Trim().ToString().Length;
TotalAddition += Convert.ToInt64(vals[i].Trim().ToString());
}
// Fill with zeros of each separated numeric values in before of
// the number, so that each value has the maxlength.
for (long k = 0; k < vals.Length; k++)
{
long len = vals[k].Trim().Length;
string str = "";
for (long j = 1; j <= maxlength - len; j++)
str += "0";
vals[k] = str + vals[k].Trim();
}
// carrys getting after addition is stored in carry[] array.
string[] carry = new string[maxlength];
long carryRevIndex = maxlength - 1;
long carrVal = 0;
for (long each = maxlength - 1; each >= 0; each--)
{
long total = 0;
for (long m = 0; m < vals.Length; m++)
{
//total of specifc position of separated numbers
total += carrVal +Convert.ToInt64(vals[(int)m][(int)each].ToString());
}
//if total is greather than 9 , carry has digits except last digit, Otherwise
//carry is zero.
if (total > 9)
{
carry[carryRevIndex] = total.ToString().Substring(0, total.ToString().Length - 1);
}
else
{
carry[carryRevIndex] = "0";
}
carrVal = Convert.ToInt64(carry[carryRevIndex].ToString());
carryRevIndex--;
}
string strCarry = "C=";
string strVals = "";
// generating carry string
for (long p = 1; p < maxlength; p++)
{
strCarry += carry[p].ToString() + ",";
}
strCarry += "\n\n";
for (int d = 0; d < vals.Length; d++)
{
strVals += vals[d].ToString() + "\n";
}
lblAddition.Text = "Addition of " + txtAddition.Text + " is: \n\n" + strCarry + "\n" + strVals +
"\n------\n\n" + TotalAddition.ToString() + "\n------\n\n";
}
catch (Exception ex)
{
//if input is not in correct format, this block is executed.
lblAddition.Text = "";
txtAddition.Focus();
MessageBox.Show(ex.Message);
}
}
#endregion
}
#endregion
}
#endregion
#2#
Project Title: Alphabets sorter
Description: This project aim is to sort the alphabetical (a-z) string. You can check radio button, for sorting in ascending or descending order.
ScreenShot:
i) Ascending Order:-
ii) Descending Order:-
Code:-
#region "Importing Namespaces"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
#endregion
#region "Alphabets Sorter"
namespace AlphabetsSorter
{
#region "Alphabets Sorter Class"
public partial class AlphabetsSorter : Form
{
#region "Constructor"
public AlphabetsSorter()
{
InitializeComponent();
}
#endregion
#region "Alphabets sorter form load"
private void AlphabetsSorter_Load(object sender, EventArgs e)
{
// By default ascending order is checked.
rdAsc.Checked = true;
}
#endregion
#region "button sort click event"
private void btnSort_Click(object sender, EventArgs e)
{
//sort
// Regular expression for checking alphabets.
Regex r = new Regex(@"^[a-zA-Z\s]+$");
if (!r.IsMatch(txtAlphabets.Text.Trim()))
{
MessageBox.Show("Enter only alphabets");
txtAlphabets.Text = "";
txtAlphabets.Focus();
return;
}
string str = txtAlphabets.Text.Trim();
//converting into character array, so that it can be sorted.
Array a = str.ToCharArray();
//sorting array
Array.Sort(a);
string revStr = "";
//getting reversed string ie ascending order
for (int i = 0; i < a.Length; i++)
{
revStr += a.GetValue(i);
}
if (rdAsc.Checked)
{
lblAlphabets.Text = "Ascending Order : " + revStr;
return;
}
if (rdDes.Checked)
{
string stDes = "";
//getting descending order of string.
for (int k = revStr.Length - 1; k >= 0; k--)
{
stDes += revStr[k].ToString();
}
lblAlphabets.Text = "Descending Order : " + stDes;
return;
}
}
#endregion
}
#endregion
}
#endregion
#3#
Project Title: - Arithmetic Progression Series
Description :- This project is aimed to display the series. You have to enter first term and difference and total terms, then it will display total series up to total terms as you specified.
First Term : Means first number of the series.
Difference: Difference between two terms which are adjacent.
Total terms: Number of terms to display the series.
Screenshot:-
Code:-
#region "Importing namespaces"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
#endregion
#region "Arithmetic Progression Series"
namespace ArithmeticProgressionSeries
{
#region " class Arithmetic Progression "
public partial class ArithmeticProgression : Form
{
#region "Constructor"
public ArithmeticProgression()
{
InitializeComponent();
}
#endregion
#region "Find Series"
private void btnFind_Click(object sender, EventArgs e)
{
//find series
try
{
long firstTerm = Convert.ToInt64(txtFirstTerm.Text),diff = Convert.ToInt64(txtDiff.Text),
totalTerms = Convert.ToInt64(txtTotalterms.Text);
if (totalTerms <= 0)
{
MessageBox.Show("Enter valid number");
txtTotalterms.Text = "";
txtTotalterms.Focus();
return;
}
if (diff < 0)
{
MessageBox.Show("Enter valid number");
txtDiff.Text = "";
txtDiff.Focus();
return;
}
string strSeries = ""+firstTerm +",";
long temp;
for (long i = 1; i < totalTerms; i++)
{
temp = firstTerm + diff;
strSeries += temp.ToString() + ",";
firstTerm = temp;
}
lblSeries.Text = "Series is : " + strSeries;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
}
#endregion
}
#endregion
#4#
Project Title: - ASCII Table
Description :- ASCII (American standard code for information interchange) consists of codes for alphabets , numbers, non-printable characters. It is 128 – bit character set. This project will displays all ascii characters with ascii codes. These codes are useful , when you develop application which checks alphabets, numbers , special characters etc.
Screenshot:-
Code:-
#region "Importing namespaces"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
#endregion
#region "ASCII Table"
namespace ASCIITable
{
public partial class ASCIITable : Form
{
#region "Constructor"
public ASCIITable()
{
InitializeComponent();
}
#endregion
#region "ASCII Table form load"
private void ASCIITable_Load(object sender, EventArgs e)
{
lblASCII.Text = "";
int i1 = 0;
// getting first 33 ascii string
for (i1 = 0; i1 <= 32; i1++)
{
lblASCII.Text += i1.ToString() + " " + GetASCIIChar(i1.ToString()) + "\n";
}
// getting symbols
for (i1 = 33; i1 <= 37; i1++)
{
lblASCII.Text += i1.ToString() + " " + char.ConvertFromUtf32(i1).ToString() + "\n";
}
//getting 38th symbol.
lblASCII.Text += i1.ToString() + " " + "&" + "\n";
//getting symbols and alphabets etc.
for (i1 = 39; i1 <= 126; i1++)
{
lblASCII.Text += i1.ToString() + " " + char.ConvertFromUtf32(i1).ToString() + "\n";
}
// getting 127th symbol
lblASCII.Text += "127" + " " + GetASCIIChar("127");
}
#endregion
#region "Getting ascii string of some ascii codes"
public string GetASCIIChar(string val)
{
switch (val)
{
case "0":
return "NUL";
case "1":
return "SOH (Start of Heading) ";
case "2":
return "STX (Start of Text)";
case "3":
return "ETX (End of Text)";
case "4":
return "EOT (End of Transmission)";
case "5":
return "ENQ (Enquiry)";
case "6":
return "ACK (Acknowledge)";
case "7":
return "BEL (Bell)";
case "8":
return "BS (Backspace)";
case "9":
return "TAB (Horizonatal Tab)";
case "10":
return "NF (NL Line feed, new line) ";
case "11":
return "VT (Vertical Tab)";
case "12":
return "FF (NP form feed , new page)";
case "13":
return "CR (Carriage return)";
case "14":
return "SO (Shift out)";
case "15":
return "SI (Shift in)";
case "16":
return "DLE (Data link escape)";
case "17":
return "DC (Device control 1)";
case "18":
return "DC (Device control 2)";
case "19":
return "DC (Device control 3)";
case "20":
return "DC (Device control 4)";
case "21":
return "NAK (Negatice acknowledge)";
case "22":
return "SYN (Synchronous idle)";
case "23":
return "ETB (End of trans. block)";
case "24":
return "CAN (Cancel)";
case "25":
return "EM (End of medium)";
case "26":
return "SUB (Substitute)";
case "27":
return "ESC (Escape)";
case "28":
return "FS (File separator)";
case "29":
return "GS (Group separator)";
case "30":
return "RS (Record separator)";
case "31":
return "US (Unit separator)";
case "32":
return "Space";
case "127":
return "DEL";
}
return val;
}
#endregion
}
}
#endregion
#5#
Project Title:- Binary Addition
Description:- This project is aimed for calculating addition of binary numbers which is in the format like 1001+01+1010 etc. . Binary number consists of two symbols 0 or 1 ie it has base 2. On adding binary numbers following binary number will be generated. See below for more information:
Binary addition of two specific binary digits:
0 + 0 =0 , carry=0
0 + 1 =1 , carry=0
1 + 0 =1 , carry=0
1 + 1 =1 , carry=1
On addition of binary numbers, carry of specific binary digits will be calculated and displayed in the screen. It also displays the total of addition of binary numbers.
Screenshot:-
Code:-
#region "Importing namespaces"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
#endregion
#region "Binary Addition"
namespace BinaryAddition
{
#region "Binary Add"
#region "Binary Add Class"
public partial class BinaryAdd : Form
{
#region "Constructor"
public BinaryAdd()
{
InitializeComponent();
}
#endregion
#region "Button calculate event"
private void btnCalculate_Click(object sender, EventArgs e)
{
Regex r=new Regex (@"^([0-9]+\+[0-9]+)+$");
// validating with a regular expression
if(! r.IsMatch (txtAddition .Text .Trim ()))
{
MessageBox.Show ("Enter in this format: 0000 + 1111 ");
txtAddition .Text ="";
txtAddition .Focus ();
return ;
}
this.BinaryAddition(txtAddition.Text);
}
#endregion
#region "Binary addition"
public void BinaryAddition(string strValues)
{
try
{ // getting separate values
string[] vals = strValues.Split('+');
long maxlength = 0;
//Finding maxlength out of seperate values
for (long i = 0; i < vals.Length; i++)
if (vals[i].Trim().ToString().Length > maxlength)
maxlength = vals[i].Trim().ToString().Length;
//filling with zeros
for (long k = 0; k < vals.Length; k++)
{
long len = vals[k].Trim().Length;
string str = "";
for (long j = 1; j <= maxlength - len; j++)
str += "0";
vals[k] = str + vals[k].Trim();
}
//carry values array
string[] carry = new string[maxlength];
//tot val of each bit addition , array
string[] totVal = new string[maxlength];
//count
int cou = 0;
long carryRevIndex = maxlength-1;
long carrVal = 0;
//getting carry values
for (long each = maxlength - 1; each >= 0; each--)
{
long total = 0;
for (long m = 0; m < vals.Length; m++)
{
if(m == 0)
total += carrVal + Convert.ToInt64(vals[(int)m][(int)each].ToString());
else
total += Convert.ToInt64(vals[(int)m][(int)each].ToString());
}
if (total == 2) // 1+1=0 c=1
{
carry[carryRevIndex] = "1";
total = 0;
}
else if (total ==1) // 0+1 =1 1+0=1
{
total = 1;
carry[carryRevIndex] = "0";
}
else if (total == 0) //0+0=0
{
total = 0;
carry[carryRevIndex] = "0";
}
else if (total == 3)// 1+1+1=3
{
total = 1;
carry[carryRevIndex] = "1";
}
carrVal = Convert.ToInt64(carry[carryRevIndex].ToString());
totVal[cou] = total.ToString();
cou++;
carryRevIndex--;
}
string strCarry = "C=";
string strVals = "";
//getting carry string
for (long p = 1; p < maxlength; p++)
{
strCarry += carry[p].ToString() + ",";
}
strCarry += "\n\n";
// getting updated binary values
for (int d = 0; d < vals.Length; d++)
{
if (carry[carryRevIndex + 1].ToString().Equals("1"))
{
strVals += " " + vals[d].ToString() + "\n";
}
else
{
strVals += vals[d].ToString() + "\n";
}
}
string strTot="";
//adding carry to binary resulted number if carry is generated for first
//digit's addition
if (carry[carryRevIndex+1].ToString().Equals("1"))
{
strTot += "1";
}
//getting resulted binary values of addition.
for (long each1 = cou-1 ; each1 >= 0; each1--)