Csharp File2
Csharp File2
Csharp File2
OF
.NET LAB
2. WAP to get input and print an integer number using C#. 00-00-2022
9. WAP to compute the sum of the first 500 prime numbers 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#.
Program – 1
using System;
namespace programs
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
****************OUTPUT****************
Program – 2
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****************
Program – 3
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
namespace programs
{
class Program
{
}
}
}
****************OUTPUT****************
Program – 5
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****************
Program – 6
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);
}
}
}
****************OUTPUT****************
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****************
Program – 8
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;
}
****************OUTPUT****************
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;
}
}
}
}
****************OUTPUT****************
Program – 10
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****************
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****************
Program –12
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);
****************OUTPUT****************