C# Lab Manual
C# Lab Manual
class Program
{
static void Main(string[] args)
{
int number;
Console.Write("Enter a number:");
number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("You entered :{0}",number);
Console.ReadLine();
}
}
3. C# Program to Add Two Integers
class Program
{
static void Main(string[] args)
{
int num1, num2, sum;
Console.WriteLine("Calculate the sum of two numbers:");
Console.Write("Input number1:");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input number2:");
num2 = Convert.ToInt32(Console.ReadLine());
sum = num1 + num2;
Console.Write("Result:"+sum);
Console.ReadKey();
}
}
4. Program to Calculate the Simple Interest in C# Code:
int P, T;
float R, SI;
Console.Write("Enter Amount :");
P = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Rate :");
R = Convert.ToSingle(Console.ReadLine());
Console.Write("Enter Time :");
T = Convert.ToInt32(Console.ReadLine());
SI = P * R * T / 100;
Console.WriteLine("Interest is :{0}", SI);
Console.ReadKey();
Console.ReadLine();
class Program
{
static void Main(string[] args)
{
int area, length, width;
Console.Write("Please write the length of your rectangle: ");
length = Convert.ToInt32(Console.ReadLine());
Console.Write("Please write the width of your rectangle: ");
width = Convert.ToInt32(Console.ReadLine());
area = length * width;
Console.WriteLine("The area of rectangle : {0}", area);
Console.ReadKey();
}
}
Console.WriteLine("==============================================");
for(int i=num1;i<num2;i++)
{
sayac = 0;
if(i>1)
{
for(int j=2;j<i;j++)
{
if(i%j==0)
{
sayac = 1;
break;
}
}
if(sayac==0)
{
Console.WriteLine(i);
}
}
}
Console.ReadKey();
}
12.Finding the biggest of three numbers in C#
Console.WriteLine(result);
Console.ReadLine();
}
{
if (chr.Key == ConsoleKey.Backspace && num.Length > 0)
{
num = num.Substring(0, (num.Length - 1));
Console.Write("\b \b");
}
}
}
while (chr.Key != ConsoleKey.Enter);
Console.ReadKey();
}
21. Calculate Sum and Average of an Array in C#
static void Main()
{
double sum=0, avg=0;
double[] numbers = { 10, 20, 50, 40};
for(int i=0;i<numbers.Length;i++)
{
sum += numbers[i];
}
avg = sum / numbers.Length;
Console.WriteLine("The Sum is : "+sum);
Console.WriteLine("The Average is : "+avg);
Console.ReadKey();
}
22. Find The Second Largest Number in Array Using C#
static void Main(string[] args)
{
int n, i, j = 0, largest, secondLargest;
int[] arr1 = new int[50];
Console.Write("\n\nFind the second largest element in an array :\n");
Console.Write("-----------------------------------------\n");
Console.Write("Input the size of array : ");
n = Convert.ToInt32(Console.ReadLine());
/* Stored values into the array*/
Console.Write("Input {0} elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
/* find location of the largest element in the array */
largest = 0;
for (i = 0; i < n; i++)
{
if (largest < arr1[i])
{
largest = arr1[i];
j = i;
}
}
/* ignore the largest element and find the 2nd largest element in the array */
secondLargest = 0;
for (i = 0; i < n; i++)
{
if (i == j)
{
i++; /* ignoring the largest element */
i--;
}
else
{
if (secondLargest < arr1[i])
{
secondLargest = arr1[i];
}}}
Console.Write("The Second largest element in the array is : {0} \n\n",
secondLargest);
Console.ReadKey();
}