Practical File
.NET
TECHNOLGIES LAB
COAP-3118
Submitted To: Submitted By:
Ms. Rekha Name: Aakansha Kalra
ASSISTANT PROFESSOR Roll No.: B40517039
CSA Department BCA 6TH SEMESTER
PDM University
PDM UNIVERSITY
DEPARTMENT OF PHYSICAL SCIENCES
SECTOR – 3A, BAHADURGARH, HARYANA
INDEX
LIST OF EXPERIMENTS.
S.NO. Name of the Program Date Signature
1 Write a program to print “HELLO WORLD” 20/01/2020
using C#
2 Write a program to input and print an 27/01/2020
integer number using C#.
3 Write a program to display date and time 03/02/2020
in C#.
4 Write a program to implement 03/02/2020
conditional if statement.
5 Write a C# program to print pattern 0’s 10/02/2020
and 1’s.
6 Write a C# program to implement the 17/02/2020
concept of number is factorial or not.
7 Write a C# program to implement 24/02/2020
conditional switch statement.
8 Write a C# program to compute the sum 02/03/2020
of the first 500 prime numbers.
9 Write a C# program to implement the 23/03/2020
concept of array list.
10 Write a C# program for default 30/03/2020
arguments.
1. Write a program to print “HELLO WORLD” using C#.
Source Code:
using System;
class HelloWorld {
static void Main() {
Console.WriteLine("Hello World");
}
}
Output:
AAKANSHA KALRA (B40517039)
2. Write a program to input and print an integer number using C#.
Source Code:
using System;
class ReadIntExample {
static void Main() {
//declare an integer variable
int num = 0;
//prompt message to take input
Console.Write("Input an integer value: ");
num = Convert.ToInt32(Console.ReadLine());
//print the value
Console.WriteLine("num = " + num);
}
}
Output:
AAKANSHA KALRA (B40517039)
3. Write a program to display date and time in c#.
Source Code:
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.Write("Current Date and Time is : ");
DateTime now = DateTime.Now;
Console.WriteLine(now);
Console.ReadKey();
}
}
}
Output:
AAKANSHA KALRA (B40517039)
4. Write a program to implement conditional if statement.
Source Code:
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int number = 7;
if (number >5)
{
Console.WriteLine("{0} is greater than 5", number);
}
Console.WriteLine("This statement is always executed.");
Console.ReadKey();
}
}
}
Output:
AAKANSHA KALRA (B40517039)
5. Write a program to print pattern of 0’s and 1’s.
Source Code:
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int i, j, c = 0;
for (i = 0; i < 8; i++) //loop for row
{
for (j = 0; j <= i; j++) //loop for column
{
c++; //increment in count variable
if (c % 2 == 0)
Console.Write(0);
else
Console.Write(1);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
Output:
AAKANSHA KALRA (B40517039)
6. Write a C# program to implement the concept of number is factorial or not.
Source Code:
public class FactorialExample
{
public static void Main(string[] args)
{
int n;
Console.Write("Enter any Number: ");
n = int.Parse(Console.ReadLine());
for (int i = 1;;i++)
{
if (n % i == 0) {
n /= i;
if (n == 1) {
Console.Write("entered number is a factorial");
}
else {
Console.Write("entered number is not a factorial");
}
}
else {
break;
}
Console.ReadLine();
}
}
}
Output:
AAKANSHA KALRA (B40517039)
7. Write a C# program to implement conditional switch statement.
Source Code:
using System;
namespace Conditional
{
class SwitchCase
{
public static void Main(string[] args)
{
char op;
double first, second, result;
Console.Write("Enter first number: ");
first = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter second number: ");
second = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter operator (+, -, *, /): ");
op = (char)Console.Read();
switch(op)
{
case '+':
result = first + second;
Console.WriteLine("{0} + {1} = {2}", first, second, result);
break;
case '-':
result = first - second;
Console.WriteLine("{0} - {1} = {2}", first, second, result);
break;
AAKANSHA KALRA (B40517039)
case '*':
result = first * second;
Console.WriteLine("{0} * {1} = {2}", first, second, result);
break;
case '/':
result = first / second;
Console.WriteLine("{0} / {1} = {2}", first, second, result);
break;
default:
Console.WriteLine("Invalid Operator");
break;
}
}
}
Output:
AAKANSHA KALRA (B40517039)
8. Write a C# program to compute the sum of the 500 prime numbers.
Source Code:
using System;
public class Prime
{
public static void Main()
{
Console.WriteLine("\nSum of the first 500 prime numbers: ");
long sum = 0;
int ctr = 0;
int n = 2;
while (ctr < 500)
{
if (isPrime(n))
{
sum += n;
ctr++;
}
n++;
}
Console.WriteLine(sum.ToString());
}
public static bool isPrime(int n)
{
int x = (int)Math.Floor(Math.Sqrt(n));
if (n == 1) return false;
if (n == 2) return true;
for (int i = 2; i <= x; ++i)
{
if (n % i == 0) return false;
}
return true;
}
}
AAKANSHA KALRA (B40517039)
Output:
AAKANSHA KALRA (B40517039)
9. Write a C# program to implement the concept of array list.
Source Code:
using System;
using System.Collections;
namespace CollectionApplication {
class Program {
static void Main(string[] args) {
ArrayList al = new ArrayList();
Console.WriteLine("Adding some numbers:");
al.Add(45);
al.Add(78);
al.Add(33);
al.Add(56);
al.Add(12);
al.Add(23);
al.Add(9);
Console.WriteLine("Capacity: {0} ", al.Capacity);
Console.WriteLine("Count: {0}", al.Count);
Console.Write("Content: ");
foreach (int i in al) {
Console.Write(i + " ");
}
Console.WriteLine();
Console.Write("Sorted Content: ");
al.Sort();
foreach (int i in al) {
Console.Write(i + " ");
}
Console.WriteLine();
Console.ReadKey();
}
}
}
AAKANSHA KALRA (B40517039)
Output:
AAKANSHA KALRA (B40517039)
10. Write a C# program for default arguments.
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Demo
{
private int a, b, c;
//function definition with default arguments
public void setValue(int X, int Y = 10, int Z = 20)
{
a = X;
b = Y;
c = Z;
}
//printing the values
public void printValue()
{
Console.WriteLine("Values are : " + a + ", " + b + ", " + c);
}
class Program
{
static void Main()
{
Demo D = new Demo();
//passing one argument other will be assigned
//with default arguments
D.setValue(5);
D.printValue();
//passing two arguments other will be assigned
//with default arguments
D.setValue(5, 8);
D.printValue();
//passing all arguments
AAKANSHA KALRA (B40517039)
D.setValue(5, 8, 13);
D.printValue();
}
}
}
Output:
AAKANSHA KALRA (B40517039)