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

Tuesday, 15 September 2015

Pascal Triangle Program in C#

One of the most thrilling variety styles is Pascal’s Triangle (named after Blaise Pascal, a well-known French Mathematician and truth seeker). Pascal’s Triangle became at first developed by means of the historic Chinese language, but Blaise Pascal changed into the primary individual to discover the significance of all the patterns it contained.

Source Code 


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

namespace PascalTriangle
{
class Program
{
static void Main(string[] args)
{
int row;
int position;
Console.Write("Please input a row Number : ");
row = Convert.ToInt32(Console.ReadLine());
Console.Write("Please input position along the row # "+row+": ");
position = Convert.ToInt32(Console.ReadLine());
if (row < position)
{
Console.Write( "\nRow : " + row);
Console.Write("\nPosition: " + position);

Console.Write( "\nInvalid entry. Position must be less than ( < ) or equal to ( = ) Row.");
int a1 = Convert.ToInt32(Console.ReadLine());
return;
}
else
{
Console.Write("\nValue at row " + row + " and position " + position + " is " + compute_pascal(row, position));
}
int a = Convert.ToInt32(Console.ReadLine());
}

private static int compute_pascal(int row, int position)
{
if (position == 1)
{
return 1;
}
else if(position == row)
{
return 1;
}
else
{
return compute_pascal(row-1, position) + compute_pascal(row-1, position-1);
}
}
}
}