Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Csharp File2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

PRACTICAL FILE

OF
.NET LAB

Submitted To Submitted By Raman Kaushik


Associate Professor Roll no.: B40519002
Dept. Of Computer Application BCA 6th SEM
SUB CODE: COAP3118

DEPARTMENT OF COMPUTER SCIENCE AND APPLICATION


Index
S NO. Programs Date Signature
1. WAP to print “Hello World” using C#. 00-00-2021

2. WAP to get input and print an integer number using C#. 00-00-2022

3. WAP to display date and time in C#. 00-00-2022

4. WAP to program to implement conditional if statement. 00-00-2022

5. WAP to program to print a pattern of 0’s and 1’s in C#. 00-00-2022

6. WAP to calculate the factorial of a given number in C#. 00-00-2022

7. WAP to replace a character with another character in 00-00-2022


string in C#.

8. WAP to implement conditional switch statement in C#. 00-00-2022

9. WAP to compute the sum of the first 500 prime numbers 00-00-2022
in C#.

10 WAP to check if a given integer is within 20 of 100 or 200 00-00-2022


in C#.

11. WAP to check if the first element or the last element of 00-00-2022
the two arrays are equal or not in C#.

12. WAP to implement the concept of array list in C#. 00-00-2022


.NET Practical File

Program – 1

WAP to print “Hello World” using C#.

using System;

namespace programs
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

****************OUTPUT****************

RAMAN KAUSHIK (RK1B19) 3


.NET Practical File

Program – 2

WAP to input and print an integer number using C#.

using System;

namespace programs
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number");
int num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Entered Number is : " + num);
Console.ReadLine();
}
}
}

****************OUTPUT****************

RAMAN KAUSHIK (RK1B19) 4


.NET Practical File

Program – 3

WAP to display date and time in C#.

using System;

namespace programs
{
class Program
{
static void Main(string[] args)
{
NewMethod();
Console.ReadLine();

void NewMethod()
{
Console.WriteLine("Current Time is : " + DateTime.Now);
}

}
}
}

****************OUTPUT****************

Program – 4

RAMAN KAUSHIK (RK1B19) 5


.NET Practical File

WAP to program to implement conditional if statement in C#.


using System;

namespace programs
{
class Program
{

static void Main(string[] args)


{
Console.WriteLine("Enter First number : ");
int i = Convert.ToInt32(Console.ReadLine());
if (i > 10)
{
Console.WriteLine(i + " is Greater than " + 10);
}
else
{
Console.WriteLine(i + " is Less than " + 10);
}
Console.ReadLine();

}
}
}

****************OUTPUT****************

RAMAN KAUSHIK (RK1B19) 6


.NET Practical File

Program – 5

WAP to program to print a pattern of 0’s and 1’s in C#.


using System;

namespace programs
{
class Program
{
static void Main(string[] args)
{
var v = new Random();
var limit = v.Next(1, 10);
String str = "";
for (int i = 0; i < limit; i++)
{
str += getNum();
}
Console.WriteLine(str);
string getNum()
{
string str = "";
switch (v.Next(0, 3)) {
case 0:str = "00";
break;
case 1:str = "01";
break;
case 2:str = "10";
break;
default:str = "11";
break;
}
return str;
}
}
}
}

****************OUTPUT****************

RAMAN KAUSHIK (RK1B19) 7


.NET Practical File

Program – 6

WAP to calculate the factorial of a given number in C#.


using System;;
namespace programs
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number to find its factorial :
");
try
{
int f = Convert.ToInt32(Console.ReadLine());

if (f < 0)
{
Console.WriteLine("Negative numbers do not have
factorials");
}
else
{
Console.WriteLine("Factorial of " + f + " is : " +
FactorialIs(f));
}
Console.ReadLine();
}
catch(FormatException ex)
{
Console.WriteLine("An input of type integer must be
entered");
Console.WriteLine(ex.Message);
}

int FactorialIs(int factNum)


{
if (factNum == 0 || factNum == 1)
{
return 1;
}
else
{
return factNum * FactorialIs(factNum - 1);
}
}
}

RAMAN KAUSHIK (RK1B19) 8


.NET Practical File

}
}

****************OUTPUT****************

RAMAN KAUSHIK (RK1B19) 9


.NET Practical File

Program – 7
WAP to replace a character with another character in string in C#.
using System;

namespace programs
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter a String : ");
string s = Console.ReadLine();
Console.WriteLine("Enter the charcter you want to
replace in '" + s + "'");
char c = Convert.ToChar(Console.ReadLine());
Console.WriteLine("Enter the character you want to
replace '" + c + "' with : ");
char c2 = Convert.ToChar(Console.ReadLine());
string s2 = s.Replace(c.ToString().ToUpper(),
c2.ToString().ToUpper());
s2 = s2.Replace(c.ToString().ToLower(),
c2.ToString().ToLower());
Console.WriteLine("New string is : " + s2);
Console.ReadLine();
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
}
}
}
****************OUTPUT****************

RAMAN KAUSHIK (RK1B19) 10


.NET Practical File

Program – 8

WAP to implement conditional switch statement in C#.


using System;

namespace programs
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What would you like to buy");
getItemTemplate("Apples ", 0);
getItemTemplate("Bananas", 1);
getItemTemplate("Mangoes", 2);
getItemTemplate("Oranges", 3);
Console.Write("So, what you have chosen : ");
var input = Convert.ToInt32(Console.ReadLine());

switch (input)
{
case 0:
showOutput("Apples", 150);
getMoney(150);
break;
case 1:
showOutput("Bananas", 60);
getMoney(60);
break;
case 2:
showOutput("Mangoes", 120);
getMoney(120);
break;
case 3:
showOutput("Oranges", 80);
getMoney(80);
break;
default:
Console.WriteLine("Invalid input");
break;
}

void getItemTemplate(string item, int key)


{
Console.WriteLine("For " + item + ", Enter " + key);
}

RAMAN KAUSHIK (RK1B19) 11


.NET Practical File

void showOutput(string item, int cost)


{
Console.WriteLine("To get '" + item + "', give " + cost
+" rs.");
}

void getMoney(int amount)


{
int receivedMoney = 0;
Console.Write("Money : ");
receivedMoney = Convert.ToInt32(Console.ReadLine());
if (receivedMoney > amount)
{
Console.WriteLine("Here is your change : " +
(receivedMoney - amount));
receivedMoney = amount;
}
while (receivedMoney != amount)
{
Console.Write("Money : ");
receivedMoney =
Convert.ToInt32(Console.ReadLine());
if(receivedMoney < amount)
{
Console.WriteLine("Give " + (amount -
receivedMoney));
amount -= receivedMoney;
}else if (receivedMoney > amount)
{
Console.WriteLine("Here is your change : " +
(receivedMoney - amount));
receivedMoney = amount;
}
}
Console.WriteLine("\nHappy Shopping");
}
Console.ReadLine();
}
}
}

RAMAN KAUSHIK (RK1B19) 12


.NET Practical File

****************OUTPUT****************

RAMAN KAUSHIK (RK1B19) 13


.NET Practical File

Program – 9

WAP to compute the sum of the first 500 prime numbers in C#.
using System;

namespace programs
{
class Program
{
static void Main(string[] args)
{

int i = 1;
int primeOrNot = 3;
int sumOfPrimes = 2;
while(i < 500)
{
if (checkPrime(primeOrNot))
{
sumOfPrimes += primeOrNot;
i++;
}
primeOrNot += 2;
}
Console.WriteLine("Sum of first 500 primes is : " +
sumOfPrimes);
Console.ReadLine();

bool checkPrime(int i)
{
var limit = Convert.ToInt32(Math.Sqrt(i)) + 1;
for (int j = 2; j < limit; j++)
{
if(i % j == 0)
{
return false;
}
}
return true;
}
}
}
}

RAMAN KAUSHIK (RK1B19) 14


.NET Practical File

****************OUTPUT****************

RAMAN KAUSHIK (RK1B19) 15


.NET Practical File

Program – 10

WAP to check if a given integer is within 20 of 100 or 200 in C#.


using System;

namespace programs
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("To check if a number is within 20 of 100
& 200");
Console.Write("Enter the number : ");
int val = Convert.ToInt32(Console.ReadLine());
if(Math.Abs(100 - val) <= 20 || Math.Abs(200 - val) <= 20)
{
Console.WriteLine(true);
}
else
{
Console.WriteLine(false);
}
Console.ReadLine();
}
}
}

****************OUTPUT****************

RAMAN KAUSHIK (RK1B19) 16


.NET Practical File

Program – 11

WAP WAP to check if the first element or the last element of the two
arrays are equal or not in C#.
using System;

namespace programs
{
class Program
{
static void Main(string[] args)
{
int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, };
int[] arr2 = { 1, 2, 3, 4, 5, 6, 7, 89 };
if(
arr1[arr1.Length -1] == arr2[arr2.Length -1]
||
arr1[0] == arr2[0]
)
{
Console.WriteLine(true);
}
else
{
Console.WriteLine(false);
}
Console.ReadLine();
}
}
}

****************OUTPUT****************

RAMAN KAUSHIK (RK1B19) 17


.NET Practical File

Program –12

WAP WAP to implement the concept of array list in C#.


using System;
using System.Collections;

namespace programs
{
class Program
{
static void Main(string[] args)
{
// creating object of type ArrayList()
ArrayList arrayList = new ArrayList();
arrayList.Add("Raman 1");
arrayList.Add("Raman 2");
arrayList.Add("Raman 3");
arrayList.Remove("Raman 3");
Console.WriteLine(arrayList);
Console.WriteLine("Lenght of the arraylist is : " +
arrayList.Count);

arrayList.Insert(1, "Raman 4");


arrayList.Insert(0, "Raman 5");
arrayList.Remove("Raman 4");
Console.WriteLine(arrayList);
Console.WriteLine("Lenght of the arraylist is : " +
arrayList.Count);
Console.ReadLine();
}
}
}

****************OUTPUT****************

RAMAN KAUSHIK (RK1B19) 18

You might also like