0% found this document useful (0 votes)
2 views

Visual Programming-1

The document explains the differences between the Write() and WriteLine() methods in C#, highlighting that Write() does not add a new line while WriteLine() does. It also discusses the ReadLine() method for reading user input and provides examples of type-casting for converting string inputs to various data types such as int, double, boolean, and char. Additionally, it includes sample programs demonstrating user interaction and calculations based on input data.

Uploaded by

razia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Visual Programming-1

The document explains the differences between the Write() and WriteLine() methods in C#, highlighting that Write() does not add a new line while WriteLine() does. It also discusses the ReadLine() method for reading user input and provides examples of type-casting for converting string inputs to various data types such as int, double, boolean, and char. Additionally, it includes sample programs demonstrating user interaction and calculations based on input data.

Uploaded by

razia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Zia system of Education Lodhran

Visual Programming:

Write() and WriteLine()

The difference between Write () and WriteLine () method is based on new


line character.

Write() method displays the output but do not provide a new line character.

WriteLine() method displays the output and also provides a new line character it
the end of the string, This would set a new line for the next output.

Let us see an example to learn about the difference between Write() and
WriteLine() method −

Example
using System;

class Program {

static void Main() {

Console.Write("One");

Console.Write("Two");

// this will set a new line for the next output

Console.WriteLine("Three");

Console.WriteLine("Four");

Console.WriteLine("Five");

}
Output

OneTwoThree

Four

Five

Uses of C# Readline Method

The C# readline method is mainly used to read the complete string until the user
presses the Enter key or a newline character is found.

Using this method, each line from the standard data input stream can be read. It is also
used to pause the console so that the user can take a look at the output.

Type-Casting for Non-string Type Inputs for C# Readline


Method

The default data type of the C# readline is string. Some of the typecasting methods for
non-string input data types in order to convert them from string to the respective data
types are as given below-

int data type

Method - Convert.ToInt32();

Syntax - Convert.ToInt32(Console.ReadLine());

double data type

Method - Convert.ToDouble();

Syntax - Convert.ToDouble(Console.ReadLine());
boolean data type

Method - Convert.ToBoolean();

Syntax - Convert.ToBoolean(Console.ReadLine());

char data type

Method - Convert.ToChar();

Syntax - Convert.ToChar(Console.ReadLine());

using System;

using System.IO; // defining the System package

class Program1 {

public static void Main(string[] args) // defining the main function

string feeling; //declaring variable feeling

Console.WriteLine("Hey, how do you feel today?");

feeling = Console.ReadLine(); // takes the input from the user

Console.WriteLine("Hello there! I am feeling "+ feeling + "!"); //


print the output

}
using System;
using System.IO; // defining the System package

class Program2 {

public static void Main() //defining the main method

// declaring variables

int price;

string name;

int icecreams;

int total;

Console.WriteLine("Enter price of each ice-cream: ");

price = Convert.ToInt32(Console.ReadLine()); // Converting string to int

Console.WriteLine("Enter the customer's name: ");

name = Console.ReadLine(); //reading input

Console.WriteLine("Enter the number of icecreams that "+name+" has ordered");

icecreams = Convert.ToInt32(Console.ReadLine()); // Converting string to int

Total = icecreams * price; // calculating total price

// printing the final output

Console.WriteLine(name+" has ordered "+icecreams+

" ice-creams. The total amount to be paid is "+total);


}

You might also like