Sie sind auf Seite 1von 5

T.Y.BSC Information technology (Mumbai university) Prepared by www.bscmscit.

com Call :- 91 9967590707 , 022 66752917 For notes, practical videos and tuitions Third year BSC (T.Y.B.S.C) Information technology: - Practical 1

Write a console application that obtains four int values from the user and displays the product. Overview Doing this practical is a 5 step procedure, first create a simple c# console application , take 4 input numbers , multiply them , display the results and finally run the application.

Step 1:- Create simple c# application project The first step is to create a simple c# console application project with a nice name as shown in the below figure. So start Microsoft visual studio, click on new project, select console application and press ok.

Step 2:- Take input numbers from console One the project is created you will find an empty method with name Main as shown in the below code snippet.
static void Main(string[] args) { }

The first code we need to write is to accept numbers in the program. So to take the first number we need to use the Console.ReadLine function as shown in the below code snippet. Just to make the user understand he is entering the first number we can use Console.WriteLine function to display Enter First number. When we take data from keyboard its always in string format , we need to convert that format to int ( Integer ) datatype. In order to do this conversion we need to use the Convert.ToInt16 function as shown in the below code snippet.

static void Main(string[] args)

{ // take the first number Console.WriteLine("Enter First number"); int num1 = Convert.ToInt16(Console.ReadLine()); .. .. }

There are other functions in the Convert class which help you to convert for other data types as well.

In the same way we can repeat the same code for other three numbers as shown in the below code snippet.
// take the first number Console.WriteLine("Enter First number"); int num1 = Convert.ToInt16(Console.ReadLine());

// take the second number Console.WriteLine("Enter Second number"); int num2 = Convert.ToInt16(Console.ReadLine());

// take the third number Console.WriteLine("Enter Third number"); int num3 = Convert.ToInt16(Console.ReadLine());

// take the Fourth number Console.WriteLine("Enter Fourth number");

int num4 = Convert.ToInt16(Console.ReadLine());

Step 3:- Make a product The final step is to multiply the numbers taken in these four variables. To multiply we need to use the * symbol. In the below code snippet you can see we have created one more variable called as product and the product of all the numbers are stored in this variable.
// Do a product int product = (num1 * num2 * num3 * num4);

Step 4 :- Display the output Once the product is completed we need to display the product variable. Using the Consol.WriteLine we can achieve the same easily as shown in the below code snippet.
// Display the output Console.WriteLine("The Product is :" + product);

Step 5:- Run the application Finally run the application by hitting CNTRL + F5 and you should be able to run the application.

Full code Below is the full code which we have discussed in the above section in small pieces.
static void Main(string[] args) {

// take the first number Console.WriteLine("Enter First number"); int num1 = Convert.ToInt16(Console.ReadLine());

// take the second number Console.WriteLine("Enter Second number"); int num2 = Convert.ToInt16(Console.ReadLine());

// take the third number Console.WriteLine("Enter Third number"); int num3 = Convert.ToInt16(Console.ReadLine());

// take the Fourth number Console.WriteLine("Enter Fourth number"); int num4 = Convert.ToInt16(Console.ReadLine());

// Do a product int product = (num1 * num2 * num3 * num4);

// Display the output Console.WriteLine("This product is :} " + product);

Das könnte Ihnen auch gefallen