Project Report
Project Report
KATHMANDU
ICT Department
Nischal Ghimire
Sandhya Dahal
Contents
1 Write a c# program to test input number is odd or even..............................................4
1.1 Source code...........................................................................................................4
1.2 Output....................................................................................................................4
2 Write a c# program to test input number is positive or negative or zero.....................4
2.1 source code............................................................................................................4
2.2 Output....................................................................................................................5
3 Write a c# program to check whether input number is odd and divisible by 3............5
3.1 Source code...........................................................................................................5
3.2 Output....................................................................................................................6
4 Example of the jagged array.........................................................................................6
4.1 Source code...........................................................................................................6
4.2 Output....................................................................................................................7
5 Write a c# program to find largest and smallest number from three input number
using ternary operator..........................................................................................................7
5.1 Source code...........................................................................................................7
5.2 Output....................................................................................................................8
6 Write a program to use the single dimension array......................................................8
6.1 Source code...........................................................................................................8
6.2 Output..................................................................................................................10
7 write a program find sum of array elements using foreach loop................................10
7.1 Source code.........................................................................................................10
7.2 Output..................................................................................................................11
8 Example of interface...................................................................................................11
8.1 Source code.........................................................................................................11
8.2 Output..................................................................................................................12
9 Example of the multiple inheritance...........................................................................12
9.1 Source code.........................................................................................................12
9.2 Output..................................................................................................................14
10 Write a program to take some number from user and division by 5 or 7...............14
10.1 Source code.........................................................................................................14
10.2 Output..................................................................................................................15
11 Write a program to take some number from user and division by 5 and 7.............15
2
11.1 Source code.........................................................................................................15
11.2 Output..................................................................................................................16
12 Write a c# program to handle DividedByZeroException and
IndexOutOfRangeException..............................................................................................16
12.1 Source code.........................................................................................................16
12.2 Output..................................................................................................................17
13 Write a c# program that read balacnce and withdraw amout from user and display
remaining balance if is greater than withdraw amount otherwise throw application
exception with suitable message........................................................................................19
13.1 Source code.........................................................................................................19
13.2 Output..................................................................................................................21
14 Example of rectangular array..................................................................................21
14.1 Source code.........................................................................................................21
14.2 Output..................................................................................................................22
15 Example of jagged array.........................................................................................23
15.1 Source code.........................................................................................................23
15.2 Output..................................................................................................................24
16 Example of single thread/main thread....................................................................24
16.1 Source code.........................................................................................................24
17 Write c# program to create multiple thread in main thread....................................25
18 Write a c# program to create two thread that one thread prints odd number and
second thread print even number between 500 to 600.......................................................26
19 Example of the if else if statement..........................................................................27
19.1 Source code.........................................................................................................27
19.2 Output..................................................................................................................28
20 Example of WPF.....................................................................................................28
20.1 Source code.........................................................................................................28
20.2 Output..................................................................................................................30
21 Write a c# program to calculate the simple interest use WPA with one layout.....30
21.1 Source code.........................................................................................................30
21.2 Output..................................................................................................................32
3
1 Write a c# program to test input number is odd or even.
1.1 Source code
using System;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number");
int num = Convert.ToInt32(Console.ReadLine()); if
(num % 2 == 0)
{
Console.WriteLine("Èven number");
}
else
Console.WriteLine("odd number");
Console.ReadKey();
}
}
1.2 Output
namespace ConsoleApp8
{
internal class Class1
{
public static void Main(string []args)
4
{
Console.WriteLine("enter the some number");
int n = Convert.ToInt32(Console.ReadLine());
if(n>0)
{
Console.WriteLine("the number is postive");
}
else if(n<0)
{
Console.WriteLine("the number is negative");
}
else
Console.WriteLine("zero number");
Console.ReadKey();
}
}
}
2.2 Output
namespace ConsoleApp8
{
internal class Class2
{
public static void Main(string[] args)
{
Console.WriteLine("Enter any number:");
int x = Convert.ToInt32(Console.ReadLine());
5
if (x % 3 == 0)
{
Console.WriteLine("The input number is odd and divisible by 3.");
}
else
{
Console.WriteLine("The input number is odd and not divisible by 3.");
}
}
else // The number is even
{
if (x % 3 == 0)
{
Console.WriteLine("The input number is even and divisible by 3.");
}
else
{
Console.WriteLine("The input number is even but not divisible by 3.");
}
}
}
}
}
3.2 Output
namespace ConsoleApp9
6
{
internal class Program
{
static void Main(string[] args)
{
int[][] num = new int[3][];
num[0] = new int[4] { 1, 2, 3, 4 };
num[1] = new int[2] { 5, 6 };
num[2] = new int[3] { 7, 8, 9 };
7
Console.ReadKey();
}
}
5.2 Output
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
matrix[i, j] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(matrix[i, j]);
8
// sum of 2 multiple 2 matrix in
int sum = 0;
for(int i=0;i<2;i++)
//display matrix
for(int j=0;j<2;j++)
Console.Write(matrix[i, j] + "\t");
Console.WriteLine();
9
6.2 Output
10
7.2 Output
8 Example of interface
8.1 Source code
using System;
interface Example
class Example1:Example{
class test
11
{
obj.display();
8.2 Output
interface Teacher
int getData();
interface Student
void display();
12
class Person : Teacher, Student
this.roll = roll;
this.name = name;
return roll;
13
}
class Test
obj.display();
9.2 Output
class Example
{ int num;
14
num=Convert.ToInt32(Console.ReadLine()); if(num
%5==0 || num%7==0)
else
10.2 Output
class Example
{ int num;
num=Convert.ToInt32(Console.ReadLine());
15
if(num%5==0 && num%7==0)
else
11.2 Output
namespace ExceptionHadlingin
{
internal class flrogram
{
12.2 Output
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExceptionHadlingin
17
{
18
{
try
Console.WriteLine("array element="+a[6]);
catch (IndexOutOfRangeException e)
Console.WriteLine(e);
finally
Output
19
13 Write a c# program that read balacnce and withdraw amout from
user and display remaining balance if is greater than withdraw
amount otherwise throw application exception with suitable
message.
13.1 Source code
using System;
namespace ThrowsException
{
// Custom exception class inheriting from
ApplicationException public class
InsufficientBalanceException : ApplicationException
{
// Constructor that passes the message to the base class
constructor public InsufficientBalanceException(string msg) :
base(msg) { }
}
20
amou
nt:");
doubl
e
balan
ce =
doubl
e.flars
e(Con
sole.R
eadLi
ne());
doubl
e
withdr
aw =
doubl
e.flars
e(Con
sole.R
eadLi
ne());
21
// Checking if balance is
sufficient if (balance >=
withdraw)
{
balance -= withdraw; // Deducting withdraw amount from
balanc
e
Console.WriteLine("Remaining balance = " + balance);
}
else
{
// Throwing custom exception if balance is
insufficient throw new
sufficient"); InsufficientBalanceException("Balance is not
} }
catch (InsufficientBalanceException ex)
{
// Handling the custom exception
Console.WriteLine(ex.Message);
}
catch (Exception e)
{
// Handling any other general exceptions
Console.WriteLine(e.Message);
}
finally
{
// Code in the finally block always executes
Console.WriteLine("The transaction has ended.");
}
}
}
}
22
13.2 Output
class RectangularArrayExample
{ 1, 2, 3 },
{ 4, 5, 6 },
23
{ 7, 8, 9 }
};
for (int i = 0; i < matrix.GetLength(0); i++) // matrix.GetLength(0) gives the number of rows
Console.WriteLine();
Console.ReadKey();
14.2 Output
24
15 Example of jagged array
15.1 Source code
using System;
new int[] { 2, 5 }
};
System.Console.Write(arr[i][j]+" ");
System.Console.WriteLine();
25
}
15.2 Output
namespace ThreadExample
{
internal class flrogram
{
static void Main(string[] args)
{
Thread t1 =
Thread.CurrentThread; t1. ame
= "Main Thread";
Console.WriteLine("Current Thread" + t1. ame);
}
}
}
26
17 Write c# program to create multiple thread in main thread.
Source code
using System;
using System.Threading;
namespace MultipleThreadMain
{
internal class flrogram
{
public static void Task1()
{
for (int i = 1; i < 100; i++)
{
Console.WriteLine("Task-1 prints " + i);
}
}
// Starting the
threads t1.Start();
t2.Start();
// Ensuring the main thread waits for the other threads to finish
t1.Join();
t2.Join();
27
}
}
}
18 Write a c# program to create two thread that one thread prints odd
number and second thread print even number between 500 to 600.
using System;
using System.Threading;
namespace MultipleThreadMain
{
internal class flrogram
{
public static void Task1()
{
for (int i = 500; i < 600; i++)
{
if(i%2==1)
{
Console.WriteLine("the odd number is print"+ i);
}
Console.WriteLine("Task-1 prints " + i);
}
}
28
public static void Main(string[] args)
{
// Creating thread objects for Task1 and
Task2 Thread t1 = new Thread(Task1);
Thread t2 = new Thread(Task2);
// Ensuring the main thread waits for the other threads to finish
t1.Join();
t2.Join();
}
}
}
}
else if (num > 60)
{
Console.WriteLine("B grade");
}
else if (num > 50)
{
Console.WriteLine("C+ grade");
}
else if (num > 40)
{
Console.WriteLine("C");
}
else if (num >= 35)
{
Console.WriteLine("D");
}
else
Console.WriteLine("non grade");
Console.ReadLine();
19.2 Output
20 Example of WPF
20.1 Source code
For mainwindow.xaml
<Window x:Class="simpleWflFExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
30
xmlns:mc="http://schemas.openxmlformats.org/
markup- compatibility/2006"
xmlns:local="clr-
namespace:simpleWflFExample"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" FontSize="500px"
FontStyle="Italic">
<Grid>
<Button Content="Click Me" FontSize="50px"
HorizontalAlignment="Left" VerticalAlignment="Top" Click="Button_Click"
Height="70" Width="188" Margin="306,3,0,0" Grid.Row="2"
Grid.ColumnSpan="2"/>
</Grid>
</Window>
//For mainwindow.xaml.cs
using System.Windows;
namespace simpleWflFExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello, WflF!");
}
}
}
31
20.2 Output
<Window x:Class="SimpleInterestExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentati
on" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008
" xmlns:mc="http://schemas.openxmlformats.org/markup-
compatibility/2006"
xmlns:local="clr-
namespace:SimpleInterestExample"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Stackflanel Margin="0,10,0,0">
32
<Label Content="Simple Interest" FontSize=" 30"
HorizontalAlignment="center"/>
<Label Content="flrinciple" FontSize=" 25"/>
<TextBox x: ame="pname" Width="200" Height="35"
HorizontalAlignment="Left" VerticalAlignment="Stretch"/>
<Label Content="Rate" FontSize="30"/>
<TextBox x: ame="rname" Width="200" Height="35"
HorizontalAlignment="left" Margin="0,10,0,0"/>
<Label Content="Time" FontSize="30"/>
<TextBox x: ame="tname" Width="200" Height="35"
HorizontalAlignment="left"/>
<TextBlock x: ame="Result" Width="350" Height="30"
Background="red"/>
<Button Content="simple interest calculate"
Width="200" FontSize="30" Click="Butoon_click"/>
</Stackflanel>
</Grid>
</Window>
For mainwindow.xaml.cs
using System.Windows;
namespace SimpleInterestExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
33
float p =
float.flarse(pname.Text); float r
= float.flarse(rname.Text);
float t =
float.flarse(tname.Text); float
si = p * t * r / 100;
Result.Text = "simple interest is:" + si.ToString();
//MessageBox.Show("the simple interet is " + si);
}
}
}
21.2 Output
34
35