Write a program to calculate the factorial of the number entered by a user. Create this program by using static functions and static variables. Hint: The limit of users input should be 1-20. (Duration: 30 min)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication71
{
class factorial
{
static long Facl;
public static bool Fac(long n, out long answer)
{
long k;
bool ok = true;
if (n <= 0 || n > 20)
{
ok = false;
}
else
{
Facl = 1;
for (k = 2; k <= n; ++k)
{
Facl = Facl * k;
}
answer = Facl;
return ok;
}
}
static void Main(string[] args)
{
long facl;
bool ok;
long number;
Console.WriteLine("enter the number for calculating factorial(1-20):");
number = long.Parse(Console.ReadLine());
ok = factorial.Fac(number, out facl);
if (ok)
Console.WriteLine("factorial(" + number + ")=" + facl);
else
Console.WriteLine("incorrect value");
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication71
{
class factorial
{
static long Facl;
public static bool Fac(long n, out long answer)
{
long k;
bool ok = true;
if (n <= 0 || n > 20)
{
ok = false;
}
else
{
Facl = 1;
for (k = 2; k <= n; ++k)
{
Facl = Facl * k;
}
answer = Facl;
return ok;
}
}
static void Main(string[] args)
{
long facl;
bool ok;
long number;
Console.WriteLine("enter the number for calculating factorial(1-20):");
number = long.Parse(Console.ReadLine());
ok = factorial.Fac(number, out facl);
if (ok)
Console.WriteLine("factorial(" + number + ")=" + facl);
else
Console.WriteLine("incorrect value");
Console.ReadLine();
}
}
}
Write a program to identify the largest of three numbers entered by a user. Create this program by using methods. (Duration: 25 min)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication72
{
public class Program
{
static int number1, number2, number3;
public static void Greater()
{
if (number1 > number2 && number1 > number3)
{
Console.WriteLine("first number is the greatest:{0}", number2);
}
else if (number2 > number1 && number2 > number3)
{
Console.WriteLine("second number is the greatest:{0}", number2);
}
else
{
Console.WriteLine("this number is the greatest:{0}", number3);
}
}
static void Main(string[] args)
{
Console.WriteLine("enter the first number");
number1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter the second number");
number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter the third number");
number3 = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication72
{
public class Program
{
static int number1, number2, number3;
public static void Greater()
{
if (number1 > number2 && number1 > number3)
{
Console.WriteLine("first number is the greatest:{0}", number2);
}
else if (number2 > number1 && number2 > number3)
{
Console.WriteLine("second number is the greatest:{0}", number2);
}
else
{
Console.WriteLine("this number is the greatest:{0}", number3);
}
}
static void Main(string[] args)
{
Console.WriteLine("enter the first number");
number1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter the second number");
number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter the third number");
number3 = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
}
}
}
Predict the output of the following program:
Predict the output of the following program:
using System;
namespace Square_Number
{
class CalculateSquare
{
int number;
public void Square(int number)
{
Console.WriteLine("The number is: {0}",number);
number *= number;
Console.WriteLine("Square of 10 is: {0}", number);
}
CalculateSquare()
{
number = 10;
Square(number);
}
static void Main(string[] args)
{
CalculateSquare cal = new CalculateSquare();
Console.ReadLine();
}
}
}
using System;
namespace Square_Number
{
class CalculateSquare
{
int number;
public void Square(int number)
{
Console.WriteLine("The number is: {0}",number);
number *= number;
Console.WriteLine("Square of 10 is: {0}", number);
}
CalculateSquare()
{
number = 10;
Square(number);
}
static void Main(string[] args)
{
CalculateSquare cal = new CalculateSquare();
Console.ReadLine();
}
}
}
Predict the output of the following program:
using System;
namespace CalculateNumber
{
class Calculate
{
static int number1;
public void Display(int number)
{
Console.WriteLine(number);
}
Calculate()
{
number1++;
Display(number1);
}
static Calculate()
{
number1 = 10;
number1++;
}
static void Main(string[] args)
{
Calculate Cal1 = new Calculate();
Calculate Cal2 = new Calculate();
Console.ReadLine();
}
}
}
namespace CalculateNumber
{
class Calculate
{
static int number1;
public void Display(int number)
{
Console.WriteLine(number);
}
Calculate()
{
number1++;
Display(number1);
}
static Calculate()
{
number1 = 10;
number1++;
}
static void Main(string[] args)
{
Calculate Cal1 = new Calculate();
Calculate Cal2 = new Calculate();
Console.ReadLine();
}
}
}
No comments:
Post a Comment