0% found this document useful (0 votes)
38 views14 pages

C# Web Apps and Algorithms Guide

Studying
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views14 pages

C# Web Apps and Algorithms Guide

Studying
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1)Create a simple web page containing the student details (RollNo, Name,

Class, Phone, Email). Write a program to store the data in the database
and retrieve it using Data reader in tabular format.

code-

using System;

namespace ArrayOfStructs

class Program

struct Student

public string studid, name, cname;

public int day, month, year;

static void Main(string[] args)

Student[] s = new Student[5];

int i;

for (i = 0; i < 5; i++)

[Link]("Enter Student Id:");

s[i].studid = [Link]();

[Link]("Enter Student name : ");

s[i].name = [Link]();

[Link]("Enter Course name : ");

s[i].cname = [Link]();

[Link]("Enter date of birth\n Enter day(1-31):");

s[i].day = Convert.ToInt32([Link]());

[Link]("Enter month(1-12):");

s[i].month = Convert.ToInt32([Link]());

[Link]("Enter year:");

s[i].year = Convert.ToInt32([Link]());

[Link]("\n\nStudent's List\n");

for (i = 0; i < 5; i++)

[Link]("\nStudent ID : " + s[i].studid);


[Link]("\nStudent name : " + s[i].name);

[Link]("\nCourse name : " + s[i].cname);

[Link]("\nDate of birth(dd-mm-yy) : " + s[i].day + "-" + s[i].month +

"-" + s[i].year);

}}}}

2) Create simple application to perform following operations


i. Finding factorial Value
code-
using [Link];

using [Link];

using [Link];

namespace factorial

class Program

static void Main(string[] args)

int i, number, fact;

[Link]("Enter the Number");

number = [Link]([Link]());

fact = number;

for (i = number - 1; i >= 1; i--)

fact = fact * i;

[Link]("\nFactorial of Given Number is: "+fact);

[Link]();

}
[Link] Fibonacci series
code -

using System;

namespace ConsoleApplication3

class Program

static void Main(string[] args)

int num1=0,num2=1,num3,num4,num,counter;

[Link] ("Upto how many number you want fibonacci series:");

num=[Link]([Link]());

counter=3;
[Link](num1+"\t"+num2);

while(counter<=num)

num3 = num1 + num2;

if (counter >= num)

break;

[Link]("\t" + num3);

num1 = num2;

num2 = num3;

counter++;

3) Create a web application that demonstrates the functionality of


mathematical operations like addition, subtraction, multiplication, and
division. Users can enter values using text boxes to perform these
calculations.

using System;
class Program

static void Main(string[] args)

// Get first number from user

[Link]("Enter the first number: ");

double number1 = [Link]([Link]());

// Get second number from user

[Link]("Enter the second number: ");

double number2 = [Link]([Link]());

// Display options for mathematical operations

[Link]("Choose an operation:");

[Link]("1. Addition");

[Link]("2. Subtraction");

[Link]("3. Multiplication");

[Link]("4. Division");

// Get operation choice from user

[Link]("Enter the number corresponding to the operation: ");

int operation = Convert.ToInt32([Link]());

double result = 0;

bool validOperation = true;

// Perform the selected operation

switch (operation)

case 1:

result = number1 + number2;


[Link]($"The result of addition: {number1} + {number2} = {result}");

break;

case 2:

result = number1 - number2;

[Link]($"The result of subtraction: {number1} - {number2} = {result}");

break;

case 3:

result = number1 * number2;

[Link]($"The result of multiplication: {number1} * {number2} = {result}");

break;

case 4:

if (number2 != 0)

result = number1 / number2;

[Link]($"The result of division: {number1} / {number2} = {result}");

else

[Link]("Error: Division by zero is not allowed.");

break;

default:

validOperation = false;

[Link]("Invalid operation choice.");

break;

// Wait for the user to press a key before exiting

if (validOperation)

[Link]($"The result is: {result}");

[Link]("Press any key to exit...");

[Link]();

}
4) Create a web application for user defined exception handling.
code-

using System;

namespace UserDefinedExceptionHandling

// Custom exception class inheriting from Exception

public class InvalidNumberException : Exception

public InvalidNumberException(string message) : base(message)

class Program

static void Main(string[] args)

try

{
// Ask the user to enter a number

[Link]("Enter a positive number: ");

int number = Convert.ToInt32([Link]());

// Throw custom exception if the number is negative

if (number < 0)

throw new InvalidNumberException("Number cannot be negative.");

// Perform a calculation if the input is valid

int square = number * number;


[Link]($"The square of {number} is {square}.");

catch (InvalidNumberException ex)

// Catch and display the custom exception message

[Link]($"Error: {[Link]}");

catch (FormatException)

// Handle invalid input format (e.g., non-numeric input)

[Link]("Error: Please enter a valid number.");

catch (Exception ex)

// Handle any other generic exceptions

[Link]($"An unexpected error occurred: {[Link]}");

finally

// Code that always runs, whether an exception is thrown or not

[Link]("Thank you for using the application.");

// Wait for user to press a key before closing

[Link]("Press any key to exit...");

[Link]();

5) Write a program in C# to demonstrate multiple inheritance using


interfaces.
code-
using System;
namespace MultipleInheritanceDemo
{
// First interface
public interface IMovable
{
void Move();
}

// Second interface
public interface IWalkable
{
void Walk();
}

// A class implementing both interfaces (simulating multiple inheritance)


public class Person : IMovable, IWalkable
{
// Implement the Move method from the IMovable interface
public void Move()
{
[Link]("The person is moving.");
}

// Implement the Walk method from the IWalkable interface


public void Walk()
{
[Link]("The person is walking.");
}
}
class Program
{
static void Main(string[] args)
{
// Create an object of the Person class
Person person = new Person();

// Call methods from both interfaces


[Link](); // IMovable's method
[Link](); // IWalkable's method

// Wait for user input before closing the console


[Link]("Press any key to exit...");
[Link]();
}
}
}

6) Create a simple web page to display the Date properties (year,


month, day, hour, minute, second, millisecond etc.) as well as to
display the number of days of the year between two specified years.
code-

using System;

namespace DatePropertiesAndDaysBetweenYears

class Program

static void Main(string[] args)

// Display the current date and time properties


DateTime currentDate = [Link];

[Link]("Current Date and Time Properties:");

[Link]($"Year: {[Link]}");

[Link]($"Month: {[Link]}");

[Link]($"Day: {[Link]}");

[Link]($"Hour: {[Link]}");

[Link]($"Minute: {[Link]}");

[Link]($"Second: {[Link]}");

[Link]($"Millisecond: {[Link]}");

[Link]();

// Asking user for input: two years to calculate the number of days between them

[Link]("Enter the first year (YYYY): ");

int year1 = Convert.ToInt32([Link]());

[Link]("Enter the second year (YYYY): ");

int year2 = Convert.ToInt32([Link]());

// Calculate the total number of days between the two years

DateTime startDate = new DateTime(year1, 1, 1); // January 1st of the first year

DateTime endDate = new DateTime(year2, 1, 1); // January 1st of the second year

// Calculate the difference between the two dates

TimeSpan difference = endDate - startDate;

double totalDays = [Link]([Link]);

[Link]($"\nNumber of days between {year1} and {year2}: {totalDays} days");

// Wait for user to press a key before closing

[Link]("\nPress any key to exit...");


[Link]();

7) Write an Application to:


1. Generate Fibonacci series
code -

using System;

namespace ConsoleApplication3

class Program

static void Main(string[] args)

int num1=0,num2=1,num3,num4,num,counter;

[Link] ("Upto how many number you want fibonacci series:");

num=[Link]([Link]());

counter=3;

[Link](num1+"\t"+num2);

while(counter<=num)

num3 = num1 + num2;

if (counter >= num)

break;

[Link]("\t" + num3);

num1 = num2;

num2 = num3;

counter++;

}}}
2. Test for prime numbers
code-
using System;

namespace testprime
{
class Program
{
static void Main(string[] args)
{
int num;
bool isPrime = true;

[Link]("Enter a number: ");


num = [Link]([Link]());

// Handle edge cases


if (num <= 1)
{
[Link](num + " is neither prime nor composite");
return;
}

// Check for factors from 2 up to the square root of num


for (int counter = 2; counter <= [Link](num); counter++)
{
if (num % counter == 0)
{
isPrime = false;
break;
}
}

if (isPrime)
{
[Link](num + " is a prime number");
}
else
{
[Link](num + " is not a prime number");
}

// Optional: Wait for user input before closing


[Link]("Press any key to exit...");
[Link]();
}
}
}

8) Create a simple application to demonstrate the concepts boxing and


unboxing.
code-
using System;
class Program
{
static void Main(string[] args)
{
// Boxing: Convert a value type (int) to an object type
int num = 123; // Value type
object boxedNum = num; // Boxing: num is wrapped into an object

[Link]("Boxing:");
[Link]($"Value type (int): {num}");
[Link]($"After boxing, object type: {boxedNum}");

// Unboxing: Convert the object type back to a value type (int)


int unboxedNum = (int)boxedNum; // Unboxing: Unwrapping the int from
the object

[Link]("\nUnboxing:");
[Link]($"Unboxed value (int): {unboxedNum}");

// Boxing with other value types


double d = 45.67; // Value type (double)
object boxedDouble = d; // Boxing: d is wrapped into an object

[Link]("\nBoxing with a double:");


[Link]($"Value type (double): {d}");
[Link]($"After boxing, object type: {boxedDouble}");

// Unboxing the double


double unboxedDouble = (double)boxedDouble; // Unboxing: Unwrapping
the double from the object
[Link]("\nUnboxing the double:");
[Link]($"Unboxed value (double): {unboxedDouble}");
[Link]("\nPress any key to exit...");
[Link]();
}
}

You might also like