Showing posts with label C sharp. Show all posts
Showing posts with label C sharp. Show all posts

Friday, 13 November 2015

Security Alarm System Project in C# for Beginners

This is the small interesting project for beginners to get motivation in programming. The program performs just like a security alarm system and generates different sounds and peeps on different situations.

Source Code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\t\t\tLEP SECURIETY SYSTEM - C#\n\n");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("\t1 : Police \n");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\t2: Fire Alarm \n");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\t3: Earthquake \n");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\t Press the Button for help\n");
int s = int.Parse(System.Console.ReadLine());

Console.ForegroundColor = ConsoleColor.DarkRed;
switch (s) {
case 1:
for (int i = 0; i < 10; i++) {
Console.Write("\a");
Console.Write("\tPlz Help us...\t");

}
break;
case 2:
for (int i = 0; i < 10; i++) {
System.Media.SystemSounds.Exclamation.Play();
Console.Write("\tPlz Help us...\t");

}
break;
case 3:
for (int i = 0; i < 10; i++)
{
System.Media.SystemSounds.Hand.Play();
Console.Write("\tPlz Help us...\t");
}
break;
default:

break;
}
int s1 = Console.Read();
Console.WriteLine("\a");
}
}
}

Output of the Program

security alarm system in C#

Tuesday, 3 November 2015

Insertion Sort Algorithm in C#

This is the simple piece of code of insertion sort algorithm. Insertion sort algorithm is less efficient as compared to other advanced algorithms but for beginner student it is a good programming practice. The insertion sort algorithm build the final sorted array one item at a time.

Source Code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace insertionSort
{
class Program
{
static void Main(string[] args)
{
welcomeScreen();
Sort();

}

private static void welcomeScreen()
{
Console.Write("\n"
+ "\t ======================================" + "\n"
+ "\t Welcome " + "\n"
+ "\t ======================================" + "\n"
+ "\n");
}

private static void Sort()
{
int i, j, s, temp;
int[] a = new int[20];

Console.Write("\n"
+ "\t Enter total elements: " + "\n"
+ "\t ---------------------" + "\n"
+ "\t");
s = Convert.ToInt32(Console.ReadLine());

Console.Write("\tEnter " + s + " elements: "+ "\n");
for (i = 0; i < s; i++)
{
Console.Write("\t");
a[i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 1; i < s; i++)
{
temp = a[i];
j = i - 1;
while ((j >= 0)&&(temp < a[j]) )
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = temp;
}

Console.Write("\tAfter sorting: ");
Console.Write("\n"
+ "\t After sorting: " + "\n"
+ "\t -------------- " + "\n"
+ "");
for (i = 0; i < s; i++)
{
Console.Write("\t" + a[i] + "\n");
}
int s1 = Convert.ToInt32(Console.ReadLine());
return;
}
}
}

Output of the Program


Insertion Sort Algorithm in C#

Sunday, 20 September 2015

C# Name Sorting Program

Its is a simple C# program that sort the student name in ascending order. The program take the student name and grade and sort the name in ascending order.

Source Code 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sortStdNames
{
class Program
{
struct student
{
public string name;
public int id;
public char grade;
};
student[] stdInstance = new student[2];
int size;
static void Main(string[] args)
{
Program m = new Program();
m.size = 0;
welcomeScrean();
int j = 2;
Console.Write( "\t Enter Detail of 2 students " +"\n");
for (int i=0; i < j; i++)
{
getStudentsWithGrade(m);
}

displayAllStudents(m);
sortAndDisplayAllStudents(m);
String s = Console.ReadLine();
}

private static void sortAndDisplayAllStudents(Program m)
{

String[] temp= new String[m.size];
List<string> l = new List<string>();
for ( int h = 0 ; h<m.size ; h++ )
{
try
{
l.Add(m.stdInstance[h].name);
}
catch (IndexOutOfRangeException e){

}
}
l.Sort();
Console.Write("\n"
+ "\a"
+ "\t *******************************************************" + "\n"
+ "\t ** Sorted names are **" + "\n"
+ "\t *******************************************************" + "\n");
Console.Write("\t \n");
foreach (String c in l)
{
Console.WriteLine("\t"+c);
}
}

private static void displayAllStudents(Program m)
{
Console.Write("\n"
+ "\t Students added with following data" + "\n"
+ "\t ======================================" + "\n"
+ "\n");

for (int n = 0; n < m.size; n++)
{
try
{
Console.Write("\n\t Name: " + m.stdInstance[n].name + "\n"
+ "\t Grade: "+m.stdInstance[n].grade);
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("IndexOutOfRangeException caught and " + e);
}
}
}

private static void getStudentsWithGrade(Program m)
{
Console.Write( "\n"
+"\a"
+"\t *******************************************************" +"\n"
+"\t ** Adding Student No # " +m.size +" **" +"\n"
+"\t *******************************************************" +"\n");

Console.Write( "\n"
+"\n\tEnter Name : ");


try
{
m.stdInstance[m.size].name = Console.ReadLine();

Console.Write( "\n\tEnter Grade ");

m.stdInstance[m.size].grade = Convert.ToChar(Console.ReadLine());//taking grade

m.stdInstance[m.size].id = m.size;//settind customer id
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("IndexOutOfRangeException caught and " + e);
}
m.size++;
Console.Write( "\n"
+"\t Press Enter to continue . . ." +"\n");
String s= Console.ReadLine();
}

private static void welcomeScrean()
{

}
}
}

Output of the Program

Sort student name according to their names

Friday, 18 September 2015

Fibonacci sequence series in C#

The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so forth.

Source Code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace evenFibonacci
{
class Program
{
static void Main(string[] args)
{
welcomeScrean();
evenFibonacci();
int wait = Convert.ToInt32(Console.ReadLine());
}

private static void welcomeScrean()
{
Console.Write("\a"
+ "\t*******************************************************" + "\n"
+ "\t** **" + "\n"
+ "\t** Welcome To **" + "\n"
+ "\t** **" + "\n"
+ "\t** Find Even Fibonacci **" + "\n"
+ "\t** **" + "\n"
+ "\t** and there sum **" + "\n"
+ "\t** **" + "\n"
+ "\t** Program By LEP **" + "\n"
+ "\t** **" + "\n"
+ "\t*******************************************************" + "\n");
}

private static void evenFibonacci()
{
int num, count = 0;
long previous = 1, next = 0, sum;
Console.Write("\t Enter Your Number to Find Fibonacci ");
num = Convert.ToInt32(Console.ReadLine());

Console.Write("\n\t First " + num + " Even Fibonacci Numbers Are \n");

do
{
if (previous % 2 == 0)
{
Console.Write("\n\t " + previous + " ");
count++;
}
sum = previous + next;
next = previous;
previous = sum;

} while (count != num);

}
}
}

Output of the Program

C# Program code for Fibonacci sequence series

Thursday, 17 September 2015

C# Program to computes the marks of student in 6 subjects and displays the total marks and average marks of the student.

It is a simple c# program to calculate the result of student in different subjects. The program takes obtained marks from 6 different courses and displays their total marks and average marks of the class.

Source Code   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarksCalculator
{
class Program
{
struct student
{
public string name;
public string[] grade;
};

//Struct Instance
student[] stdInstance = new student[50];


//Class variables
int size = 0;

static void Main(string[] args)
{
Program m = new Program();
m.size = 0;
String exit_choice;
do
{
Console.Write("\a\n\n\n"
+ "\t*******************************************************" + "\n"
+ "\t** **" + "\n"
+ "\t** Welcome **" + "\n"
+ "\t** **" + "\n"
+ "\t*******************************************************" + "\n");
Console.Write("\t\t\t");
student_menu(m);
Console.Write("\n"
+ "\a\t Do you want to continue ( y / n)");
exit_choice = Console.ReadLine();
} while (exit_choice == "y" || exit_choice == "Y");
}

private static void student_menu(Program m)
{

int student_menu_choice;
Console.Write("\n"
+ "\a\t *******************************************************" + "\n"
+ "\t ** **" + "\n"
+ "\t ** You Are In **" + "\n"
+ "\t ** **" + "\n"
+ "\t ** Student Menu **" + "\n"
+ "\t ** **" + "\n"
+ "\t *******************************************************" + "\n"
+ "\n");

Console.Write("\t\t What do you want to do for student " + "\n"
+ "\t\t ===================================" + "\n"
+ "\n"
+ "\n\n\t 1 => Add student "
+ "\n\n\t 2 => Student menu Exit \n"
+ "\t=== ------------------ " + "\n"
+ "\t\t\t\tYou Select : ");

student_menu_choice = Convert.ToInt32(Console.ReadLine());

if (student_menu_choice > 0)
{
switch (student_menu_choice)
{
case 1:
{
add_student(m);//Function
break;
}
case 2:
{
return;
}
default:
{
return;
}
}//end switch
}//end if
else
{
Console.Clear();

}//end else
return;
}

private static void add_student(Program m)
{
Console.Clear();
Console.Write("\n" + "\n" + "\n"
+ "\n" + "\n" + "\n"
+ "\a\t *******************************************************" + "\n"
+ "\t ** **" + "\n"
+ "\t ** Adding Student **" + "\n"
+ "\t ** **" + "\n"
+ "\t *******************************************************" + "\n");


Console.Write("\n"
+ "\t\tCOLLECTING DATA FOR STUDENT NO : " + m.size
+ "\n"
+ "\n\tEnter name : ");


m.stdInstance[m.size].name = Console.ReadLine();//taking name
m.stdInstance[m.size].grade = new String[6];
for (int i = 0; i < 6; i++)
{
Console.Write("\n\tEnter Score For course # " + (i + 1) + " : ");
m.stdInstance[m.size].grade[i] = Console.ReadLine();

}

Console.Write("\n"
+ "Student added with following data" + "\n"
+ "\n"
+ "Name : " + m.stdInstance[m.size].name + "\n");
int sum = 0, average = 0;

for (int j = 0; j < 6; j++)
{
Console.Write("\n\tScore For course # " + j + 1 + " : " + m.stdInstance[m.size].grade[j]);
if (Convert.ToInt32(m.stdInstance[m.size].grade[j]) < 50)
{
Console.Write("\n\tGrade is : F ");
}
else
{
Console.Write("\n\tGrade is : Not F ");
}
sum = sum + Convert.ToInt32(m.stdInstance[m.size].grade[j]);
}
Console.Write("\n\n\tSum For courses : " + sum);
average = sum / 6;
Console.Write("\n\n\tAverage For courses : " + average);
if (average < 50)
{
Console.Write("\n\n\tNot Promoted ");
}
else
{
Console.Write("\n\n\tPromoted");
}
m.size++;
}
}
}

Output of the Program

C# Program to calculate the marks of different student in the class

Friday, 6 March 2015

Extract Substring From a String

Structure of the Problem Requirements 

This Program extract substring from a string in C# program. The program extract semicolon which occurred in string many times and in new string put all the alphabet of previous string except semicolon.  

Source Code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LEPPOST
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" \t \t \t Programming Hub\n\n");
String expression = "The; Hands; That; Rock; the Cradl;e Rules; The World;";
String[] strExpression = expression.Split(';');
foreach (String st in strExpression)
{
Console.Write(st);
}
Console.ReadKey();
}
}
}

Output of the Program

How to extract Substring From a String