Exercise 1
Write a program using methods that should accept two numbers from the users and perform the following mathematical operations:
Addition
Subtraction
Multiplication
Division
In case of division, an error message should be display if the second number is zero.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex1
{
class Program
{
static int res;
public static void add(int num1,int num2)
{
res=num1+num2;
Console.WriteLine("The sum of the two number entered is "+ res);
}
public static void sub(int num1,int num2)
{
res=num1-num2;
Console.WriteLine("The difference of the two number entered is "+ res);
}
public static void mul(int num1,int num2)
{
res=num1*num2;
Console.WriteLine("The product of the two number entered is "+ res);
}
public static void div(int num1,int num2)
{
if(num2==0)
{
Console.WriteLine("Can not divide by zero");
}
else
{
res=num1/num2;
Console.WriteLine("The division of the two number "+ res);
}
}
static void Main(string[] args)
{
int ip1,ip2,ip3;
string str1, str2, str3;
Console.WriteLine("Enter the first number");
str1 = Console.ReadLine();
ip1=Convert.ToInt32(str1);
Console.WriteLine("Enter the second number");
str2 = Console.ReadLine();
ip2 = Convert.ToInt32(str2);
Console.WriteLine("Main Menu");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Substraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
Console.WriteLine("Enter your choice");
str3 = Console.ReadLine();
ip3 = Convert.ToInt32(str3);
switch(ip3)
{
case 1:
add(ip1,ip2);
break;
case 2:
sub(ip1,ip2);
break;
case 3:
mul(ip1,ip2);
break;
case 4:
div(ip1,ip2);
break;
default:
Console.WriteLine("Invalid Selection!!");
break;
}
Console.ReadLine();
}
}
}
Exercise 2
Write a program to identify the largest of three numbers entered by a user. Create this program by using methods.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex2
{
public class GreaterNumber
{
static int num1, num2, num3;
public static void greater()
{
if(num1>num2 && num1>num3)
{
Console.WriteLine("First number is greater " + num1);
}
else if(num2>num1 && num2>num3)
{
Console.WriteLine("Second NUmber is greater " + num2);
}
else
{
Console.WriteLine("Third number is greater " + num3);
}
}
static void Main(string[] args)
{
Console.WriteLine("Enter first number ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter second number ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter third number");
num3 = Convert.ToInt32(Console.ReadLine());
greater();
Console.ReadLine();
}
}
}
Exercise 3
Write a Program to calculate the factorial of the number entered by the user. Create this program by using static function and variables
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex3
{
class Factorial
{
static long fac1;
public static bool fac(long n,out long answer)
{
long k;
bool ok = true;
if (n <= 0 || n > 20)
{
ok = false;
}
else
{
fac1 = 1;
for (k = 2; k <= n; ++k)
{
fac1 = fac1 * k;
}
}
answer = fac1;
return ok;
}
}
class factorial1
{
static void Main(string[] args)
{
long fac1, number;
bool ok;
Console.WriteLine("Enter the number to calculate factorial(1-20): ");
number = long.Parse(Console.ReadLine());
ok = Factorial.fac(number,out fac1);
if (ok)
Console.WriteLine("Factorial (" + number + ")=" + fac1);
else
Console.WriteLine("Incorrect Value");
Console.ReadLine();
}
}
}
Exercise 4
Predict the output of the following program:
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex4
{
class CalculateSquare
{
int number;
public void Square(int number)
{
Console.WriteLine("The number is:" + number);
number *= number;
Console.WriteLine("Square of 10 is:" + number);
}
CalculateSquare()
{
number = 10;
Square(number);
}
static void Main(string[] args)
{
CalculateSquare ca1 = new CalculateSquare();
Console.ReadLine();
}
}
}
Output of the preceding Program
Exercise 5
Predict the output of the program.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex5
{
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();
}
}
}
Output of the preceding code
Exercise 6
Write a program to maintain the details of the stock of a shop under the item categories.Accept item category from the user. In addition accept item name, number of units and unit price. Define all the attributes as properties. Finally, display all the details.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex6
{
class ShoppingItem
{
private string Category, nm, sz;
private decimal price;
public string Name
{
get
{
return nm;
}
set
{
if (nm == "")
nm = "item no Description";
else
nm = value;
}
}
public string Size
{
get
{
return sz;
}
set
{
sz = value;
}
}
public decimal UnitPrice
{
get
{
return price;
}
set
{
if (price < 0)
price = 0.00M;
else
price = value;
}
}
public static ShoppingItem Read()
{
ShoppingItem shop = new ShoppingItem();
string cat;
Console.WriteLine("Store Items Categories");
Console.WriteLine("\t1. Women");
Console.WriteLine("\t2. Men");
Console.WriteLine("\t3 Girls");
Console.WriteLine("\t4. Boys");
Console.WriteLine("\t5. Babies");
Console.WriteLine("Enter the Category (1-5): ");
cat = Console.ReadLine();
if(cat=="1")
shop.Category = "Women";
else if (cat=="2")
shop.Category = "Men";
else if(cat=="3")
shop.Category = "Girls";
else if(cat=="4")
shop.Category = "Boys";
else if(cat=="5")
shop.Category = "Babies";
else
{
Console.WriteLine("Invalid item category");
Console.ReadLine();
System.Environment.Exit(0);
}
Console.Write("Item Name : ");
shop.Name = Console.ReadLine();
Console.Write("Number of Units : ");
shop.Size = Console.ReadLine();
Console.Write("Unit Price : ");
shop.UnitPrice = decimal.Parse(Console.ReadLine());
return shop;
}
public static void Write(ShoppingItem item)
{
Console.WriteLine();
Console.WriteLine("Category: " + item.Category);
Console.WriteLine("Description: " + item.Name);
Console.WriteLine("Number of units: " + item.Size);
Console.WriteLine("Unit Price " + item.UnitPrice);
}
public static void Main(string[] args)
{
ShoppingItem si = new ShoppingItem();
si = ShoppingItem.Read();
ShoppingItem.Write(si);
Console.ReadLine();
}
}
}
Write a program using methods that should accept two numbers from the users and perform the following mathematical operations:
Addition
Subtraction
Multiplication
Division
In case of division, an error message should be display if the second number is zero.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex1
{
class Program
{
static int res;
public static void add(int num1,int num2)
{
res=num1+num2;
Console.WriteLine("The sum of the two number entered is "+ res);
}
public static void sub(int num1,int num2)
{
res=num1-num2;
Console.WriteLine("The difference of the two number entered is "+ res);
}
public static void mul(int num1,int num2)
{
res=num1*num2;
Console.WriteLine("The product of the two number entered is "+ res);
}
public static void div(int num1,int num2)
{
if(num2==0)
{
Console.WriteLine("Can not divide by zero");
}
else
{
res=num1/num2;
Console.WriteLine("The division of the two number "+ res);
}
}
static void Main(string[] args)
{
int ip1,ip2,ip3;
string str1, str2, str3;
Console.WriteLine("Enter the first number");
str1 = Console.ReadLine();
ip1=Convert.ToInt32(str1);
Console.WriteLine("Enter the second number");
str2 = Console.ReadLine();
ip2 = Convert.ToInt32(str2);
Console.WriteLine("Main Menu");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Substraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
Console.WriteLine("Enter your choice");
str3 = Console.ReadLine();
ip3 = Convert.ToInt32(str3);
switch(ip3)
{
case 1:
add(ip1,ip2);
break;
case 2:
sub(ip1,ip2);
break;
case 3:
mul(ip1,ip2);
break;
case 4:
div(ip1,ip2);
break;
default:
Console.WriteLine("Invalid Selection!!");
break;
}
Console.ReadLine();
}
}
}
Exercise 2
Write a program to identify the largest of three numbers entered by a user. Create this program by using methods.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex2
{
public class GreaterNumber
{
static int num1, num2, num3;
public static void greater()
{
if(num1>num2 && num1>num3)
{
Console.WriteLine("First number is greater " + num1);
}
else if(num2>num1 && num2>num3)
{
Console.WriteLine("Second NUmber is greater " + num2);
}
else
{
Console.WriteLine("Third number is greater " + num3);
}
}
static void Main(string[] args)
{
Console.WriteLine("Enter first number ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter second number ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter third number");
num3 = Convert.ToInt32(Console.ReadLine());
greater();
Console.ReadLine();
}
}
}
Exercise 3
Write a Program to calculate the factorial of the number entered by the user. Create this program by using static function and variables
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex3
{
class Factorial
{
static long fac1;
public static bool fac(long n,out long answer)
{
long k;
bool ok = true;
if (n <= 0 || n > 20)
{
ok = false;
}
else
{
fac1 = 1;
for (k = 2; k <= n; ++k)
{
fac1 = fac1 * k;
}
}
answer = fac1;
return ok;
}
}
class factorial1
{
static void Main(string[] args)
{
long fac1, number;
bool ok;
Console.WriteLine("Enter the number to calculate factorial(1-20): ");
number = long.Parse(Console.ReadLine());
ok = Factorial.fac(number,out fac1);
if (ok)
Console.WriteLine("Factorial (" + number + ")=" + fac1);
else
Console.WriteLine("Incorrect Value");
Console.ReadLine();
}
}
}
Exercise 4
Predict the output of the following program:
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex4
{
class CalculateSquare
{
int number;
public void Square(int number)
{
Console.WriteLine("The number is:" + number);
number *= number;
Console.WriteLine("Square of 10 is:" + number);
}
CalculateSquare()
{
number = 10;
Square(number);
}
static void Main(string[] args)
{
CalculateSquare ca1 = new CalculateSquare();
Console.ReadLine();
}
}
}
Output of the preceding Program
Exercise 5
Predict the output of the program.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex5
{
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();
}
}
}
Output of the preceding code
Exercise 6
Write a program to maintain the details of the stock of a shop under the item categories.Accept item category from the user. In addition accept item name, number of units and unit price. Define all the attributes as properties. Finally, display all the details.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch3_Ex6
{
class ShoppingItem
{
private string Category, nm, sz;
private decimal price;
public string Name
{
get
{
return nm;
}
set
{
if (nm == "")
nm = "item no Description";
else
nm = value;
}
}
public string Size
{
get
{
return sz;
}
set
{
sz = value;
}
}
public decimal UnitPrice
{
get
{
return price;
}
set
{
if (price < 0)
price = 0.00M;
else
price = value;
}
}
public static ShoppingItem Read()
{
ShoppingItem shop = new ShoppingItem();
string cat;
Console.WriteLine("Store Items Categories");
Console.WriteLine("\t1. Women");
Console.WriteLine("\t2. Men");
Console.WriteLine("\t3 Girls");
Console.WriteLine("\t4. Boys");
Console.WriteLine("\t5. Babies");
Console.WriteLine("Enter the Category (1-5): ");
cat = Console.ReadLine();
if(cat=="1")
shop.Category = "Women";
else if (cat=="2")
shop.Category = "Men";
else if(cat=="3")
shop.Category = "Girls";
else if(cat=="4")
shop.Category = "Boys";
else if(cat=="5")
shop.Category = "Babies";
else
{
Console.WriteLine("Invalid item category");
Console.ReadLine();
System.Environment.Exit(0);
}
Console.Write("Item Name : ");
shop.Name = Console.ReadLine();
Console.Write("Number of Units : ");
shop.Size = Console.ReadLine();
Console.Write("Unit Price : ");
shop.UnitPrice = decimal.Parse(Console.ReadLine());
return shop;
}
public static void Write(ShoppingItem item)
{
Console.WriteLine();
Console.WriteLine("Category: " + item.Category);
Console.WriteLine("Description: " + item.Name);
Console.WriteLine("Number of units: " + item.Size);
Console.WriteLine("Unit Price " + item.UnitPrice);
}
public static void Main(string[] args)
{
ShoppingItem si = new ShoppingItem();
si = ShoppingItem.Read();
ShoppingItem.Write(si);
Console.ReadLine();
}
}
}






No comments:
Post a Comment