0% found this document useful (0 votes)
104 views23 pages

AWP Practical 2

The document contains code examples for various C# programming concepts covered in Practical 2 of an introductory C# course, including: 1) A factorial program that takes a user input number and calculates the factorial. 2) A currency conversion program that allows the user to select converting between dollars, euros, or ringgit to rupees. 3) A quadratic equation solver that finds the roots of a quadratic equation. 4) Programs to convert between Celsius and Fahrenheit temperatures.

Uploaded by

shincha N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views23 pages

AWP Practical 2

The document contains code examples for various C# programming concepts covered in Practical 2 of an introductory C# course, including: 1) A factorial program that takes a user input number and calculates the factorial. 2) A currency conversion program that allows the user to select converting between dollars, euros, or ringgit to rupees. 3) A quadratic equation solver that finds the roots of a quadratic equation. 4) Programs to convert between Celsius and Fahrenheit temperatures.

Uploaded by

shincha N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Practical 2

Practical 2A
1) Factorial program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{class Program {
public static void Main(string[] args)
{ int i, fact = 1,num1;
Console.WriteLine("Enter the number");
num1 = int.Parse(Console.ReadLine());
for (i = 1; i <= num1; i++)
{ fact = fact * i; }
Console.WriteLine("Factorial of number " + num1 + " is " + fact);
Console.ReadLine();}}}
Output:-

1|P ag e
2) Currency Conversion
//1 euro 88.47 INR
//1 Dollar 74.00 INR
//1 ringgit 18.17 INR
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
class Program
{
public static void Main(string[] args)
{
int choice;
Console.WriteLine("Enter your choice:\n 1-Dollar to Rupee \n 2-Euro to
Rupee \n 3-Malaysian Ringgit to Rupee ");
2|P ag e
choice = int.Parse(Console.ReadLine());

switch (choice)
{
case 1:
Double dollar, rupee, val1;
Console.WriteLine("Enter the dollar amount");
dollar = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the dollar value");
val1 = double.Parse(Console.ReadLine());
rupee = dollar * val1;
Console.WriteLine("{0} Dollar Equals {1} Rupees", dollar, rupee);
break;
case 2:
Double euro, rupee2, val2;
Console.WriteLine("Enter the Euro amount");
euro = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the Euro value");
val2 = double.Parse(Console.ReadLine());
rupee2 = euro * val2;
Console.WriteLine("{0} Euro Equals {1} Rupees", euro, rupee2);
break;
case 3:
Double ringit, rupee3, val3;
Console.WriteLine("Enter the ringgit amount");
ringit = double.Parse(Console.ReadLine());

3|P ag e
Console.WriteLine("Enter the ringgit value");
val3 = double.Parse(Console.ReadLine());
rupee3 = ringit * val3;
Console.WriteLine("{0} Malaysian Ringgit Equals {1} Rupees",
ringit, rupee3);
break;
default:
Console.WriteLine("Invalid Choice");
break;
}
Console.ReadLine();
}}}

4|P ag e
Output:-

5|P ag e
3) Quadratic Equation
using System;
namespace ConsoleApplication4
{
class Program
{
void findRoots(int a, int b, int c)
{
if (a == 0)
{
Console.Write("invalid");
return;
}
int d = b * b - 4 * a * c;
Double sqrt_val = Math.Abs(d);
if (d > 0)
{
Console.WriteLine(" The roots are real and different\n");
Console.Write(
(Double)(-b + sqrt_val) / (2 * a) + "\n" +
(Double)(-b - sqrt_val) / (2 * a));}
else
{ Console.WriteLine("Roots are complex \n");
Console.Write(
-(Double)b / (2 * a) + "+i" + sqrt_val + "\n" +
-(Double)b / (2 * a) + "-i" + sqrt_val);

6|P ag e
}}
public static void Main(string[] args)
{
Program rt = new Program();
int a=1,b=-7,c=12;
rt.findRoots(a, b, c);
Console.ReadLine();
}}}

Output :-

7|P ag e
4) Temperature Conversion
A) Fahrenheit to Celsius
using System;
namespace ConsoleApplication4
{
class Program
{
public static void Main(string[] args)
{
double celsius;
Console.WriteLine("Enter fahrenheit temperature: ");
double fahreinheit = double.Parse(Console.ReadLine());
celsius = (fahreinheit - 32) * 5 / 9;
Console.WriteLine("The Temperature is "+Celsius.ToString(“0.00”)+"
C");
Console.ReadLine();
}}}

Output:-

8|P ag e
B) Celsius to Fahrenheit
using System;
namespace ConsoleApplication4
{class Program
{
public static void Main(string[] args)
{
float fahreinheit;
Console.WriteLine("Enter Celsius temperature: ");
float celsius = float.Parse(Console.ReadLine());
fahreinheit = (celsius*9)/5+32;
Console.WriteLine("The Temperature is
"+fahreinheit.ToString("0.00")+" F");
Console.ReadLine();
}}}
Output:-

9|P ag e
Practical 2B
1) Function Overloading
using System;
namespace ConsoleApplication4
{
class Program
{
public void swap(ref int n,ref int m)
{
int t;
t = n;
n=m;
m = t;
}
public void swap(ref float f1, ref float f2)
{
float f;
f = f1;
f1 = f2;
f2 = f;
}
public static void Main(string[] args)
{

Program p1 = new Program();


int n = 10, m = 20;
Console.WriteLine("Before Swap "+"\tN=" + n + "\tM=" + m);
p1.swap(ref n,ref m);
Console.WriteLine("After Swap " + "\tN=" + n + "\tM=" + m);
float f1 = 10.5f, f2 = 20.6f;
Console.WriteLine("Before Swap " + "\tN=" + f1 + "\tM=" + f2);
p1.swap(ref f1, ref f2);
Console.WriteLine("After Swap " + "\tN=" + f1 + "\tM=" + f2);
Console.ReadLine();

10 | P a g e
}}}
Output:-

11 | P a g e
2) Inheritance
A) Single Inheritance

using System;
namespace ConsoleApplication4
{
public class base1
{
protected int a = 50;
protected int b = 60;
}
public class base2 : base1
{
public void show()
{
int c;
c = a + b;
Console.WriteLine("Example of Single inheritance with protected mode "
+ "\n\nSum is " + c);
}}
class Program
{
public static void Main(string[] args)
{
base2 c2 = new base2();
c2.show();
Console.ReadLine();
12 | P a g e
}}}
Output:-

13 | P a g e
B) Multilevel Inheritance
using System;
namespace ConsoleApplication4
{
class test
{
public void show()
{
Console.WriteLine("show of level 1");
}
}
class testme : test
{
public void showme()
{
base.show();
Console.WriteLine("show of level 2");
}
}
class Testus : testme
{
public void showus()
{

base.showme();
Console.WriteLine("show of level 3");

14 | P a g e
}}
public class program
{
public static void Main(string[] args)
{
Testus t1 = new Testus();
t1.showus();
Console.ReadLine();
}}}
Output:-

15 | P a g e
3) Constructor Overloading
using System;
namespace ConsoleApplication4
{
class Add {
int x, y;
double f;
string s;
public Add(int a, double b)
{
x = a;
f = b;
}
public Add(int a, string b)
{
y = a;
s = b;
}
public void show()
{
Console.WriteLine("First Constructor (int+float): {0} ",(x+f));
}

public void show1()


{
Console.WriteLine("Second Constructor (int+string): {0} ", (y + s));

16 | P a g e
}}
class Program
{ public static void Main(string[] args)
{
Add g = new Add(10, 15.5);
g.show();
Add m = new Add(10," Name");
m.show1();
Console.ReadLine();
}}}

Output:-

17 | P a g e
4) Interfaces
using System;
namespace ConsoleApplication4{
public interface Class1
{
void draw();
}
public class Class1A : Class1
{
public void draw()
{
Console.WriteLine("Section A of Class 1");
}}
public class Class1B : Class1
{public void draw()
{
Console.WriteLine("Section B of Class 1 Interface");
}}
public class TestInterface
{ public static void Main()
{ Class1 d;
d = new Class1A();
d.draw();
d = new Class1B();
d.draw();
Console.ReadLine();

18 | P a g e
}}}
Output:-

19 | P a g e
Practical 2C
1) Delegate and Events

using System;
namespace ConsoleApplication4
{
delegate int NumberChanger(int n);
class Program
{ static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
public static int MultNum(int q)
{
num *= q;
return num;
}
public static int getNum()
{
return num;
}
public static void Main(string[] args)
{
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
//calling the methods using the delegate objects
nc1(25);
Console.WriteLine("Value of ADD Num: {0}", getNum());
nc2(5);
Console.WriteLine("Value of Product Num: {0}", getNum());
Console.ReadKey();
//Console.ReadLine();
}}}

20 | P a g e
Output:-

21 | P a g e
2) Exception handling
using System;
namespace ExceptionHandlingExample
{
class NotEvenException : Exception
{
public NotEvenException(String msg)
: base(msg) { }
}
class Program
{
public static void Main(string[] args)
{
int num;
try
{
Console.WriteLine("Enter a Number: ");
num = int.Parse(Console.ReadLine());
if ((num % 2) != 0)
{
throw new NotEvenException("Not an Even number");

}
else
{
Console.WriteLine("It is an even Number");
Console.ReadLine();
}
}
catch (NotEvenException e) { Console.WriteLine(e.Message); }
Console.ReadLine();
}}}

22 | P a g e
Output:-

23 | P a g e

You might also like