0% found this document useful (0 votes)
11 views

Lab3 Methods

The document describes 7 methods to: 1) Calculate the summation of integers, 2) Get the factorial of a number, 3) Get the maximum of 3 integers, 4) Check the sign of a number, 5) Check if a number is odd or even, 6) Act as a calculator for basic math operations, and 7) Accept a name and print it inverted. Sample code is provided for each method. A note at the end instructs the reader to write a Main method to call the other methods.

Uploaded by

ezz Ayman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab3 Methods

The document describes 7 methods to: 1) Calculate the summation of integers, 2) Get the factorial of a number, 3) Get the maximum of 3 integers, 4) Check the sign of a number, 5) Check if a number is odd or even, 6) Act as a calculator for basic math operations, and 7) Accept a name and print it inverted. Sample code is provided for each method. A note at the end instructs the reader to write a Main method to call the other methods.

Uploaded by

ezz Ayman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1st Level | struct. Prog.

| Lab 3

Lab3:Methos
Task1:

Write a Method that:


• Calculates the summation of any n integer values.
• Gets the factorial of any integer number.
• Gets the maximum of any three integers.
• Checks the sign of a number.
• Checks if a number is odd or even.
• Acts as a Calculator to get the summation, production,
difference and division of two number.
• Accepts your name and prints it inverted.

Solution:
Method1:

static int summ(int n) {


int result = 0, input=0;
for(int i = 1; i <= n; i++)
{
Console.WriteLine("Enter number {0} ",i);
input = int.Parse(Console.ReadLine());
result += input;
}
return result;
}
Method2:

static int fact(int n) {


int factorial = 1;
for (int i = 1; i <= n; i++)
{
factorial *= i;
}
return factorial;}
Method3:

static int max(int x,int y,int z)


{
int maximum = x;
if (y > x && y > z)
{
maximum = y;
}
else if (z > x && z > y)
{
maximum = z;
}
return maximum;
}
Method4:

static void sign(double s)


{
if (s > 0)
Console.WriteLine("Positive");
else if (s < 0)
Console.WriteLine("Negative");
else
Console.WriteLine("Equal 0");
}

Method5:

static void oddevennum(int num)


{
if (num % 2 == 0)
Console.WriteLine("Even");
else
Console.WriteLine("Odd");
}

Method6:

static void calc(int x,int y,char operation)


{
switch (operation)
{
case '+':
Console.WriteLine( x + y);
break;
case '-':
Console.WriteLine(x - y);
break;
case '*':
Console.WriteLine(x * y);
break;
case '/':
if (y == 0)
{
Console.WriteLine("Unknown Div");
}
else
{
Console.WriteLine(x/y);
}
break;
default:
Console.WriteLine("Error!! ");
break;
}

Method7:

static void invertedName(string name)


{
for(int i = name.Length - 1; i >= 0; i--)
{
Console.Write(name[i]);
}

Note:
Write Main() Method For calling Previous Methods as you like.

You might also like