0% found this document useful (0 votes)
12 views

Project Report

The document is a report on an Online MCQ App, detailing various C# programming exercises including checking odd/even numbers, handling exceptions, and using arrays. Each section includes source code and output examples for clarity. It serves as a comprehensive guide for learning C# programming concepts.

Uploaded by

Nischal Ghimire
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Project Report

The document is a report on an Online MCQ App, detailing various C# programming exercises including checking odd/even numbers, handling exceptions, and using arrays. Each section includes source code and output examples for clarity. It serves as a comprehensive guide for learning C# programming concepts.

Uploaded by

Nischal Ghimire
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

CENTRAL DEPARTMENT OF EDUCATION TU, KIRTIPUR

KATHMANDU

Report on Online MCQ App

Submitted by: Submitted to:

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

2 Write a c# program to test input number is positive or negative or


zero.
2.1 source code
using System;
using System.Collections.Generic; using
System.Linq;
using System.Text;
using System.Threading.Tasks;

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

3 Write a c# program to check whether input number is odd and


divisible by 3.
3.1 Source code
using System;

namespace ConsoleApp8
{
internal class Class2
{
public static void Main(string[] args)
{
Console.WriteLine("Enter any number:");
int x = Convert.ToInt32(Console.ReadLine());

if (x % 2 != 0) // Check if the number is odd


{

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

4 Example of the jagged array.


4.1 Source code
using System;

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 };

for (int i = 0; i < num.Length; i++)


{
for (int j = 0; j < num[i].Length; j++)
{
Console.Write(num[i][j] + " ");
}
Console.WriteLine();
}
}
}
4.2 Output

5 Write a c# program to find largest and smallest number from three


input number using ternary operator.
5.1 Source code
using System;
public class Exmaple
{
public static void Main(string []args)
{
int a, b, c;
Console.WriteLine("Enter the three Number");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine());
int largest = (a < b) ? (b < c ? c : b) : ((a < c) ? c : a);
int smallest = (a < b) ? (a < c ? a : c) : ((b < c) ? b : c);
Console.WriteLine("largest number is:" + largest);
Console.WriteLine("The Smallest number is:" + smallest);

7
Console.ReadKey();

}
}

5.2 Output

6 Write a program to use the single dimension array.


6.1 Source code
using System;

public class Example

public static void Main(string []args)

int[,]matrix = new int[2, 2];

Console.WriteLine("Enter the element of the 2 mulply 2 matrix");

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++)

for (int j=0;j<2;j++)

sum = sum + matrix[i, j];

//display matrix

Console.WriteLine("the 2 multiple 2 matrix is :");

for (int i = 0; i < 2; i++)

for(int j=0;j<2;j++)

Console.Write(matrix[i, j] + "\t");

Console.WriteLine();

Console.WriteLine("the sum of element :" + sum);

9
6.2 Output

7 write a program find sum of array elements using foreach loop.


7.1 Source code
using System;
public class foreachloop
{
static void Main(string[] args)
{

int[] num = { 10, 20, 40, 40, 50 };


int sum = 0;
foreach(int number in num)
{
sum = sum + number;
Console.WriteLine("the sum number" +
sum);
}
}
}

10
7.2 Output

WAP to display the property(book id,


book title,

8 Example of interface
8.1 Source code
using System;

interface Example

public void display();

class Example1:Example{

public void display()

Console.WriteLine("Hello i am nabraj Awasthi from dadeldhurea");

class test

11
{

public static void Main(string []args)

Example1 obj=new Example1();

obj.display();

8.2 Output

9 Example of the multiple inheritance


9.1 Source code
using System;

interface Teacher

int getData();

interface Student

void display();

12
class Person : Teacher, Student

public int roll;

public string name;

public Person(int roll, string name)

this.roll = roll;

this.name = name;

public void display()

Console.WriteLine("Hello! This is an example of multiple inheritance in C#.");

Console.WriteLine("Roll: " + roll);

Console.WriteLine("Name: " + name);

// Implementing the getData method from Teacher interface

public int getData()

return roll;

13
}

class Test

static void Main(string[] args)

Person obj = new Person(45, "Nabraj Awasthi");

obj.display();

9.2 Output

10 Write a program to take some number from user and division by 5


or 7
10.1 Source code
using System;

class Example

public static void Main(string []args)

{ int num;

Console.WriteLine("Enter the number");

14
num=Convert.ToInt32(Console.ReadLine()); if(num

%5==0 || num%7==0)

Console.WriteLine("the number is divisual by 5 and 7 ");

else

Console.WriteLine("the number is not divisual by 5 and 7");

10.2 Output

11 Write a program to take some number from user and division by 5


and 7
11.1 Source code
using System;

class Example

public static void Main(string []args)

{ int num;

Console.WriteLine("Enter the number");

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

15
if(num%5==0 && num%7==0)

Console.WriteLine("the number is divisual by 5 and 7 ");

else

Console.WriteLine("the number is not divisual by 5 and 7");

11.2 Output

12 Write a c# program to handle DividedByZeroException and


IndexOutOfRangeException.
12.1 Source code
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExceptionHadlingin
{
internal class flrogram
{

static void Main(string[] args)


{
try
{
Console.WriteLine("Enter the two number");
16
int a =
int.flarse(Console.ReadLine()); int
b =
int.flarse(Console.ReadLine()); int
div = a / b;
Console.WriteLine("Division=" +
div);
}
catch(Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.WriteLine("End of the program");
}
}
}
}

12.2 Output

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ExceptionHadlingin

17
{

internal class indexofrange

18
{

public static void Main(string[] args)

try

int[] a = new int[] {10,12,34,56,90};

Console.WriteLine("array element="+a[6]);

catch (IndexOutOfRangeException e)

Console.WriteLine(e);

finally

Console.WriteLine("the block of code is exectured");

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) { }
}

internal class flrogram


{
static void Main(string[] args)
{
tr
y
{
// Taking balance and withdraw amount input from the
user Console.WriteLine("Enter balance and withdraw

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

14 Example of rectangular array


14.1 Source code
using System;

class RectangularArrayExample

static void Main(string[] args)

// Declare and initialize a 2D rectangular array

int[,] matrix = new int[3, 3]

{ 1, 2, 3 },

{ 4, 5, 6 },

23
{ 7, 8, 9 }

};

// Output the values of the rectangular array

for (int i = 0; i < matrix.GetLength(0); i++) // matrix.GetLength(0) gives the number of rows

for (int j = 0; j < matrix.GetLength(1); j++) // matrix.GetLength(1) gives the number of


columns

Console.Write(matrix[i, j] + " ");

Console.WriteLine();

Console.ReadKey();

14.2 Output

24
15 Example of jagged array
15.1 Source code
using System;

public class JaggedArrayTest

public static void Main()

int[][] arr = new int[3][]{

new int[] { 11, 21, 56, 78 },

new int[] { 2, 5, 6, 7, 98, 5 },

new int[] { 2, 5 }

};

// Traverse array elements

for (int i = 0; i < arr.Length; i++)

for (int j = 0; j < arr[i].Length; j++)

System.Console.Write(arr[i][j]+" ");

System.Console.WriteLine();

25
}

15.2 Output

16 Example of single thread/main thread.

16.1 Source code


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

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);
}
}

public static void Task2()


{
for (int i = 101; i < 200; i++)
{
Console.WriteLine("Task-2 prints " + i);
}
}

public static void Main(string[] args)


{
// Creating thread objects for Task1 and
Task2 Thread t1 = new Thread(Task1);
Thread t2 = new Thread(Task2);

// 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);
}
}

public static void Task2()


{
for (int i = 500; i < 600; i++)
{
if(i%2==0)
{
Console.WriteLine("the even number is print" + 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);

// Starting the threads


t1.Start();
t2.Start();

// Ensuring the main thread waits for the other threads to finish
t1.Join();
t2.Join();
}
}
}

19 Example of the if else if statement


19.1 Source code
using System;

public class HelloWorld


{
public static void Main(string[] args)
{
int num;
Console.WriteLine("Enter the
number"); num =
Convert.ToInt32(Console.ReadLine()); if
(num > 90)
{
Console.WriteLine("A+ grade ");
}
else if (num > 80)
{
Console.WriteLine("A grade");
}
else if (num > 70)
29
{
Console.WriteLine("B +grade");

}
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

21 Write a c# program to calculate the simple interest use WPA with


one layout.
21.1 Source code
For Window.xaml

<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();
}

private void Butoon_click(object sender, RoutedEventArgs e)


{

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

You might also like