Objective:
- Introduction to C#
- How to compile C# code
- Simple arithmetic problems solving
Activity 1:
Take two inputs from user (keyboard) length and breadth of rectangle. Compute the area of rectangle given by: area = length*breadth
Solution:
using System;
class Program
{
static void Main()
{
int length = 0;
int breadth = 0;
int area = 0;
Console.WriteLine("Enter Length and Breadth of Rectangle");
length = Convert.ToInt32(Console.ReadLine());
breadth = Convert.ToInt32(Console.ReadLine());
area = length * breadth;
Console.WriteLine("The Area is = " + area);
}
}
Activity 2:
Write C# code to solve the second-degree equation aX^2 + bX + c= 0 for any real numbers a, b and c. The program takes the values of a, b and c as input from the user, and calculates X.
Note: real numbers are taken in float.
Solution:
using System;
class Program
{
static void Main()
{
float a = 0;
float b = 0;
float c = 0;
float X = 0;
Console.WriteLine("Enter three Real Numbers a, b and c: ");
a = Convert.ToSingle(Console.ReadLine());
b = Convert.ToSingle(Console.ReadLine());
c = Convert.ToSingle(Console.ReadLine());
float eq1;
float sq;
float eq2;
float eq3;
eq1 = (b * b) - (4 * a * c);
sq = (float)Math.Sqrt(eq1);
eq2 = (-b - sq) / (2 * a);
Console.WriteLine("The First value of X is = " + eq2);
eq3 = (-b + sq) / (2 * a);
Console.WriteLine("The Second value of X is = " + eq3);
}
}