Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

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;
namespace program {
class program
{
static void Main(string[] args)
{
string drinks = Console.ReadLine();
double money;
int n = int.Parse(Console.ReadLine());
switch (drinks)
{
case "coffee":
money = 1.50;
coffee(money, n);
break;
case "water":
money = 1.00;
water(money, n);
break;
case "coke":
money = 1.40;
coke(money, n);
break;
case "snacks":
money = 2.00;
snacks(money, n);
break;
}
static void coffee(double money, int n)
{
Console.WriteLine(n * money);
}
static void water(double money, int n)
{
Console.WriteLine(n * money);
}
static void coke(double money, int n)
{
Console.WriteLine(n * money);
}
static void snacks(double money, int n)
{
Console.WriteLine(n * money);
}
}
}
}

II Returning Values And Overload


Caculate rectangle area:
using System;

namespace program
{
class Program
{
static void Main(string[] args)
{
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);
}
static int GetGetMultipleOfEvenAndOdds(int n)
{
int SumEven = GetSumEven(n);
int SumOdd = GetSumOdd(n);
int result = SumEven * SumOdd;
return result;
}
static int GetSumOdd(int n)
{
int sum = 0;
while(n > 0)
{
int lastDigits = n % 10;
if (lastDigits % 2 != 0)
{
sum += lastDigits;
}
n /= 10;
}
return sum;
}
static int GetSumEven(int n)
{
int sum = 0;
while (n > 0)
{
int lastDigits = n % 10;
if (lastDigits % 2 == 0)
{
sum += lastDigits;
}
n /= 10;
}
return sum;
}
}
}

Repeat String:
using System;

namespace program
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
int count = int.Parse(Console.ReadLine());
string repeat = RepeatString(str, count);
Console.WriteLine(repeat);
}
static string RepeatString(string str, int count)
{
string 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;
}
}
}

You might also like