• No results found

Lab 2 and3(C).doc

N/A
N/A
Protected

Academic year: 2020

Share "Lab 2 and3(C).doc"

Copied!
8
0
0

Loading.... (view fulltext now)

Full text

(1)

CS(BUKC)

Lab # 2 and 3

Objectives

This lab deals with introduction of Sub procedures and Functions. The main focus is on how to implement procedures and functions. When to use functions instead of procedures and when to use procedures instead functions. Different methods of argument passing and their advantages are main concern in this lab.

Program 1

Make a simple GUI based program, make a function (procedure/method) named ”Addition”, which inputs two integers (whole numbers) provided by a user, compute the sum of these integers and displays the result

Steps

1) Click on the New Project button on the Start Page or from the File Menu

2) Type FunctionExample in the Name text box and click on the OK button

3) Change the title of the Form1. Locate the Text property in the Properties window and change its value to “Function Example C# and hit Enter

4) Now add three Label, three Textbox and Button controls to the Form. Select following control one by one from the Toolbox and drop it to the Form. once the control is on the form you can resize it or move to around to your desire location

5) Change the text of the Label control. Locate the Text property in the Properties window and change its value to

Label1  Input First Number Label2  Input Second Number Label3  Result

6) Change the name of the textbox control. Locate the Name property in the Properties window and change its value to

Textbox1  fnumtbox Textbox2  Snumtbox Textbox3  Rtbox

(2)

Properties window and change its value to “&Add” and hit Enter. The Button control reflect the change

9) Change the name of the Button control. Locate the Name property in the Properties window and change its value to “Addbtn”

10)To add code for a button control, just double click on it. This will open the code editor. Now add the following code into the procedure

private void button2_Click(object sender, EventArgs e) {

int a = Convert.ToInt32(textBox1.Text); int b = Convert.ToInt32(textBox2.Text); int result = sum(a, b);

textBox3.Text = " The result from function is " + result;

}

public int sum(int x, int y) {

int z = x + y; return z;

}

11) Now test your application by click on the Start button on the toolbar

Result

(3)

CS(BUKC)

Example:

using System;

namespace Example2 {

class Program

{

public static void parameter(int num1, int num2, out int add, out int sub, out int mul, out float div)

{

add = num1 + num2; sub = num1 - num2; mul = num1 * num2;

div = (float)num1 / num2; }

static void Main(string[] args) {

int num1,num2; int add, sub, mul; float div;

Console.Write("Enter 1st number\t");

num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("\nEnter 2nd number\t");

num2 = Convert.ToInt32(Console.ReadLine());

Program.parameter(num1, num2, out add, out sub, out mul, out div);

Console.WriteLine("\n\n{0} + {1} = {2}", num1, num2, add);

Console.WriteLine("{0} - {1} = {2}", num1, num2, sub);

Console.WriteLine("{0} * {1} = {2}", num1, num2, mul);

Console.WriteLine("{0} / {1} = {2}", num1, num2, div);

Console.ReadLine(); }

} }

(4)

Enter 1st number 7

Enter 2nd number 9

7 +9 = 16 7 - 9 = -2 7 * 9 = 63

7 / 9 = 0.7777778

Program 3 (Do it yourself)

Make a simple calculator, which provide facility of performing addition, subtraction, division and multiplication. Make Function procedure for each four function.

Result

Program 4

Make a simple GUI based program, which will calculate square of first 10 numbers using a function (procedure/method).

Steps

1) Click on the New Project button on the Start Page or from the File Menu

2) Type SquareFunctionExample in the Name text box and click on the OK

(5)

CS(BUKC)

3) Change the title of the Form1. Locate the Text property in the Properties window and change its value to “Square Function Example” and hit Enter

4) Now add a Textbox control and Button control to the Form. Select following control one by one from the Toolbox and drop it to the Form. once the control is on the form you can resize it or move to around to your desire location

5) Change the name of the textbox control. Locate the Name property in the Properties window and change its value to

Textbox1  txtsquare

6) Change the text of the TextBox control. Locate the Text property in the Properties window and remove its value (make it empty) and hit Enter. Locate “Multiline” property and make it true.

7) Change text of the Button control. Locate the Text property in the Properties window and change its value to “&Calculate” and hit Enter.

8) To add code for a button control, just double click on it. This will open the code editor. Now add the following code into the procedure

private void button1_Click(object sender, EventArgs e) {

textBox1.Text = "Number" + "\t" + "Square" + Environment.NewLine;

for (int i = 0; i <= 10; i++) {

textBox1.Text += i + "\t" + square(i) + Environment.NewLine;

(6)

}

Function definition is as follows:

public int square(int a) {

return a * a; }

Now test your application by click on the Start button on the toolbar

Result

Program 5 (Do it yourself)

Make a simple GUI based program, which will convert Fahrenheit into Centigrade. Use a sub procedure in this program to get the required result.

Operation

(7)

CS(BUKC)

 The application calculates the Celsius equivalent and displays it in a label on the form.

Specifications

 The formula for converting temperatures from Fahrenheit to Celsius is (F-32)*5/9.

Program 6 (Do it yourself)

Make a simple GUI based program, which will calculate the factorial of any given number. Use function in this program to get the required result.

Operation

 The user enters a number and clicks that Calculate button or presses the Enter key to activate that button.

 The program calculates the factorial of the number and displays it in a label on the form.

Specifications

 Before calculating the factorial, make sure that the user entry is an integer since a factorial can be calculated only on an integer.

 The factorial of an integer is that integer multiplied by every positive integer less than itself. A factorial number is identified by an exclamation point

following the number. Here’s how you calculate the factorial of the numbers 1 through 5:

1! = 1 which equals 1

2! = 2 * 1 which equals 2

3! = 3 * 2 * 1 which equals 6 4! = 4 * 3 * 2 * 1 which equals 24 5! = 5 * 4 * 3 * 2 * 1 which equals 120

Program 6 :

Write a program in which accept a number from the user and identify whether the number is even or odd.

using System;

(8)

class Program

{

static void Main(string[] args) {

int num;

Console.Write("Enter your number:\t"); num = Convert.ToInt32(Console.ReadLine());

if (num % 2 == 0) {

Console.WriteLine("{0} is Even Number", num); }

else {

Console.WriteLine("{0} is Odd number", num); }

Console.ReadLine(); }

} }

Output

Enter your number : 56 56 is Even Number

Program 7 (Do it yourself)

Write a program that will show the concept of method overloading by placing more than one function with the name and different parameters in one

program .

Program 8 (Do it yourself)

Write a program that will take student marks in a loop and finally will be calculating average result of the class.

Program 9 (Do it yourself)

Write a C# program that will take three numbers from the user and will pass these numbers to a function that will find out smallest number and will return the result to the caller program that will display the result in a text box.

References

Related documents

National Conference on Technical Vocational Education, Training and Skills Development: A Roadmap for Empowerment (Dec. 2008): Ministry of Human Resource Development, Department

I argue that positive global coverage of Jamaica’s outstanding brand achievements in sports, music and as a premier tourism destination, is being negated by its rival brands –

innovation in payment systems, in particular the infrastructure used to operate payment systems, in the interests of service-users 3.. to ensure that payment systems

Commercial aircraft programs inventory included the following amounts related to the 747 program: $448 of deferred production costs at December 31, 2011, net of previously

The encryption operation for PBES2 consists of the following steps, which encrypt a message M under a password P to produce a ciphertext C, applying a

Increased competition and the current economic crisis have brought about an unfavorable business climate for dental practices, but also have had a positive effect on the wider

This paper articulates how the first phase of a design research approach was applied to explore possible solutions for designing and implementing effective online higher

CICS Transaction Server for z/OS Version 3 provides an efficient and effective environment for applications that are written in COBOL, PL/I, C, C++, and Java.. This version