Objective:
- Methods in C#
Activity 1:
Write C# program
that will ask the user to enter the width and length of a rectangle and then display
the rectangle’s area. The program calls the following functions, which have not
been written:
- GetLength: This
function should ask the user to enter the rectangle’s length and then return
that value as a double .
- GetWidth: This
function should ask the user to enter the rectangle’s width and then return
that value as a double .
- GetArea: This
function should accept the rectangle’s length and width as arguments and return
the rectangle’s area. The area is calculated by multiplying the length by the
width.
- DisplayData: This function should accept the rectangle’s area as argument and display it in an appropriate message on the screen.
Solution:
using System;
class Program
{
static double GetLength()
{
double length;
Console.WriteLine("Enter Length of Rectangle: ");
length = Convert.ToDouble(Console.ReadLine());
return length;
}
static double GetWidth()
{
double width;
Console.WriteLine("Enter Width of Rectangle: ");
width = Convert.ToDouble(Console.ReadLine());
return width;
}
static double GetArea(double i, double j)
{
double area;
area = i * j;
return area;
}
static void DisplayData(double area)
{
Console.WriteLine("Area of Rectangle is: " + area);
}
static void Main()
{
double x, y, area;
x = GetLength();
y = GetWidth();
area = GetArea(x, y);
DisplayData(area);
}
}
Activity 2:
Write a
function called displayShape that
accepts size and shapetype as parameters and displays a square or a triangle of
the required size.
So e.g. if
the function is called with the following parameters:
displayShape(‘S’, 5); a square of
size 5 is displayed as given
1 |
2 |
3 |
4 |
5 |
2 |
4 |
6 |
8 |
10 |
3 |
6 |
9 |
12 |
15 |
4 |
8 |
12 |
16 |
20 |
5 |
10 |
15 |
20 |
25 |
displayShape(‘T’, 6); a triangle
of size 6 is displayed as given
1
1
1
1
2 1
1
3 3 1
1
4 6 4 1
1
5 10
10 5 1
Solution:
using System;
class Program
{
static void PrintSquare(int size)
{
int num, res;
for (int i = 1; i <= size; i++)
{
num = i;
for (int j = 1; j <= size; j++)
{
res = num * j;
Console.Write(res + "\t");
}
Console.Write("\n");
}
Console.Write("\n");
}
static void PrintTriangle(int size)
{
int coef = 1;
for (int i = 0; i < size; i++)
{
for (int space = 1; space <= size - i; space++)
{
Console.Write(" ");
}
for (int j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
{
coef = 1;
}
else
{
coef = coef * (i - j + 1) / j;
}
Console.Write(coef + " ");
}
Console.Write("\n");
}
}
static void Main()
{
char choice2 = 'y';
do {
int size;
char choice;
Console.WriteLine("Enter Size of Shape: ");
size = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter s for Square-shape or t for triangle-shape: ");
choice = Convert.ToChar(Console.ReadLine());
if (choice == 's')
{
PrintSquare(size);
}
if (choice == 't')
{
PrintTriangle(size);
}
Console.WriteLine("If you want to Repeat then press y. ");
choice2 = Convert.ToChar(Console.ReadLine());
} while (choice2 == 'y');
}
}
Activity 3:
Write a program that converts a binary number into decimal number.
Solution:
using System;
class Program
{
static int DecimalConverter(int num)
{
int dec = 0, i = 0, rem;
bool possible = true;
while (num != 0)
{
rem = num % 10;
num /= 10;
if (rem == 0 || rem == 1)
{
dec += rem * (int)Math.Pow(2, i);
++i;
}
else
{
Console.WriteLine("Conversion not possible: it is not a binary number.");
possible = false;
break;
}
}
if (possible == true)
{
return dec;
}
return 0; // Need to return a value in all paths
}
static void Main()
{
int n;
Console.Write("Enter a Binary Number: ");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Decimal Number: " + DecimalConverter(n));
}
}