Docsity

C# Programming: Declaring and Invoking Methods, Lab Reports of Network Design

Toggle sidebar
bg1
I Declaring and invoking methods:
Sign of interger number:
using System;
namespace program {
class program
{
static void Main(string[] args)
{
PrintNumber(int.Parse(Console.ReadLine()));
}
static void PrintNumber (int number)
{
if(number > 0)
{
Console.WriteLine("The number {0} is positive", number);
} else if (number < 0)
{
Console.WriteLine("The number {0} is negative", number);
} else
{
Console.WriteLine("The number {0 is zero}", number);
}
}
}
}
Caculations:
using System;
namespace program {
class program
{
static void Main(string[] args)
{
string command = Console.ReadLine();
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
switch (command) {
case "Sum":
Sum(a, b);
break;
case "Subtract":
Subtract(a, b);
break;
case "Multiply":
Multiply(a, b);
break;
case " Divided":
Divided(a, b);
break;
}
}
static void Sum(int a, int b)
{
pf3
pf4
pf5

Partial preview of the text

Download C# Programming: Declaring and Invoking Methods and more Lab Reports Network Design in PDF only on Docsity!

I Declaring and invoking methods:

Sign of interger number:

using System; namespace program { class program { static void Main(string[] args) { PrintNumber(int.Parse(Console.ReadLine())); } static void PrintNumber (int number) { if(number > 0) { Console.WriteLine("The number {0} is positive", number); } else if (number < 0) { Console.WriteLine("The number {0} is negative", number); } else { Console.WriteLine("The number {0 is zero}", number); } } } }

Caculations:

using System; namespace program { class program { static void Main(string[] args) { string command = Console.ReadLine(); int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); switch (command) { case "Sum": Sum(a, b); break; case "Subtract": Subtract(a, b); break; case "Multiply": Multiply(a, b); break; case " Divided": Divided(a, b); break; } } static void Sum(int a, int b) {

Console.WriteLine(a + b); } static void Subtract(int a, int b) { Console.WriteLine(a - b); } static void Multiply(int a, int b) { Console.WriteLine(a * b); } static void Divided(int a, int b) { Console.WriteLine(a / b); } } }

Print Triangle:

using System; using System.Linq; namespace program { class program { static void PrintLine(int start, int end) { for(int i = start; i <= end; i++) { Console.Write(i + " "); } Console.WriteLine(); } static void PrintTriangle(int n) { for(int line = 1; line <= n; line++) { PrintLine(1, line); } for(int line = n - 1; line >= 1; line--) { PrintLine(1, line); } } static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); PrintTriangle(n); } } }

Orders:

using System; using System.Linq;

double width = double.Parse(Console.ReadLine()); double height = double.Parse(Console.ReadLine()); double area = GetWidthAndHeight(width, height); Console.WriteLine(area); } static double GetWidthAndHeight(double width, double height) { return width * height; } } }

Math Power:

using System; namespace program { class Program { static void Main(string[] args) { double number = double.Parse(Console.ReadLine()); int power = int.Parse(Console.ReadLine()); Console.WriteLine(RaiseToPower); } static double RaiseToPower(double number,int power) { double result = 0; result = Math.Pow(number, power); return result; } } }

Greater of Two Valute:

using System; namespace program { class Program { static void Main(string[] args) { string input = Console.ReadLine(); switch(input) { case "int": int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); int result = GetMax(a, b); Console.WriteLine(result); break; case "char": char first = char.Parse(Console.ReadLine()); char second = char.Parse(Console.ReadLine());

char greater = GetMaxChar(first, second); Console.WriteLine(greater); break; case "string": string firstletter = Console.ReadLine(); string secondletter = Console.ReadLine(); string greaterletter = GetMaxString(firstletter, secondletter); Console.WriteLine(greaterletter); break; } } static int GetMax(int a, int b) { if (a > b) { return a; }else { return b; } } static char GetMaxChar(char first, char second) { if (first > second) { return first; } else { return second; } } static string GetMaxString(string firstletter, string secondletter) { if (firstletter.CompareTo(secondletter) >= 0) { return firstletter; } else { return secondletter; } } } }

Multiply Even By Odd

using System; namespace program { class Program { static void Main(string[] args) { int n = Math.Abs(int.Parse(Console.ReadLine())); int result = GetGetMultipleOfEvenAndOdds(n); Console.WriteLine(result);

for(int i = 0; i < count; i++) { result = result + str; } return result; } } }

Math operations:

using System; namespace program { class Program { static void Main(string[] args) { int a = int.Parse(Console.ReadLine()); string @operator = Console.ReadLine(); int b = int.Parse(Console.ReadLine()); double c = Caculate(a, @operator, b); Console.WriteLine(c); } private static double Caculate(int a, string @operator, int b) { double result = 0; switch(@operator) { case "+": result = a + b; break; case "-": result = a - b; break; case "*": result = a * b; break; case "/": result = a / b; break; } return result; } } }