SlideShare a Scribd company logo
1 of 17
Write and WriteLine Methods
// Namespace Declaration
using System;
// Program start class
class InteractiveWelcome
{
// Main begins program execution.
public static void Main()
{
// Write to console/get input
Console.Write("What is your name?: ");
Console.Write("Hello, {0}! ", Console.ReadLine());
Console.WriteLine("Welcome to the C# Station Tutorial!");
}
}
Output:
>What is your Name? <type your name here> [Enter Key]
>Hello, <your name here>! Welcome to the C# Station Tutorial!
Read and ReadLine Methods
User input from the keyboard by using the Console.Read and
Console.ReadLine methods.
The Read Method
Read reads the next character from the keyboard. It returns the int
value –1 if there is no more input available. Otherwise it returns an
int representing the character read.
The ReadLine Method
ReadLine reads all characters up to the end of the input line (the
carriage return character). The input is returned as a string of
characters. You can use the following code to read a line of text from
the keyboard and display it to the screen:
string input = Console.ReadLine( );
Console.WriteLine(“{0}”, input);
The following codeshows some examples of how to use numeric formatting:
Console.WriteLine(“Currency formatting – {0:C} {1:C4}”, 88.8,-888.8);
Console.WriteLine(“Integer formatting – {0:D5}”, 88);
Console.WriteLine(“Exponential formatting – {0:E}”, 888.8);
Console.WriteLine(“Fixed-point formatting – {0:F3}”,888.8888);
Console.WriteLine(“General formatting – {0:G}”, 888.8888);
Console.WriteLine(“Number formatting – {0:N}”, 8888888.8);
Console.WriteLine(“Hexadecimal formatting – {0:X4}”, 88);
When the previous code is run, it displays the following:
Currency formatting – $88.80 ($888.8000)
Integer formatting – 00088
Exponential formatting – 8.888000E+002
Fixed-point formatting – 888.889
General formatting – 888.8888
Number formatting – 8,888,888.80
Hexadecimal formatting – 0058
BOOLEAN DATA TYPE
using System;
class BooleanType
{
static void Main()
{
bool male = false;
Random random = new Random();
male = Convert.ToBoolean(random.Next(0, 2));
if (male)
{
Console.WriteLine("We will use name John");
} else
{
Console.WriteLine("We will use name Victoria");
}
}
}
VB Alias .NET Type Size Range
sbyte System.SByte 1 byte -128 to 127
byte System.Byte 1 byte 0 to 255
short System.Int16 2 bytes -32,768 to 32,767
ushort System.UInt16 2 bytes 0 to 65,535
int System.Int32 4 bytes -2,147,483,648 to 2,147,483,647
uint System.UInt32 4 bytes 0 to 4,294,967,295
long System.Int64 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong System.UInt64 8 bytes 0 to 18,446,744,073,709,551,615
using System;
class Overflow
{
static void Main()
{
byte a = 254;
Console.WriteLine(a);
a++;
Console.WriteLine(a);
a++;
Console.WriteLine(a);
a++;
Console.WriteLine(a);
}
}
Output:
254
255
0
1
using System;
class Notations
{
static void Main()
{
int num1 = 31;
int num2 = 0x31;
Console.WriteLine(num1);
Console.WriteLine(num2);
}
}
Output:
31
49
using System;
public class Apples
{
static void Main()
{
int baskets = 16;
int applesInBasket = 24;
int total = baskets * applesInBasket;
Console.WriteLine("There are total of {0} apples", total);
}
}
Output:
There are total of 384 apples
C#
Alias
.NET Type Size Precision Range
Float System.Single
4
bytes
7 digits
1.5 x 10-45 to
3.4 x 1038
Double System.Double
8
bytes
15-16 digits
5.0 x 10-324 to
1.7 x 10308
decimal System.Decimal
16
bytes
28-29 decimal
places
1.0
using System;
class Floats
{
static void Main()
{
float n1 = 1.234f;
double n2 = 1.234;
decimal n3 = 1.234m;
Console.WriteLine(n1);
Console.WriteLine(n2);
Console.WriteLine(n3);
Console.WriteLine(n1.GetType());
Console.WriteLine(n2.GetType());
Console.WriteLine(n3.GetType());
}
}
To use a different type, we must use a suffix.
The F/f for float numbers and M/m for decimal numbers.
using System;
class Notations
{
static void Main()
{
float n1 = 1.234f;
float n2 = 1.2e-3f;
float n3 = (float) 1 / 3;
Console.WriteLine(n1);
Console.WriteLine(n2);
Console.WriteLine(n3);
}
}
Output:
1.234
0.0012
0.3333333
using System;
class CSharpApp
{
static void Main()
{
float n1 = (float) 1 / 3;
double n2 = (double) 1 / 3;
if (n1 == n2)
{
Console.WriteLine("Numbers are equal");
} else {
Console.WriteLine("Numbers are not equal");
}
}
}
Output:
Numbers are not equal
Enumerations
using System;
class Enumerations
{
enum Days
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
static void Main()
{
Days day = Days.Monday;
if (day == Days.Monday)
{
Console.WriteLine("It is Monday");
}
Console.WriteLine(day);
foreach(int i in Enum.GetValues(typeof(Days)))
Console.WriteLine(i);
}
}
ARRAYS
using System;
class ArrayExample
{
static void Main()
{
int[] numbers = new int[5];
numbers[0] = 3;
numbers[1] = 2;
numbers[2] = 1;
numbers[3] = 5;
numbers[4] = 6;
int len = numbers.Length;
for (int i=0; i<len; i++)
{
Console.WriteLine(numbers[i]);
}
}
}

More Related Content

What's hot (10)

15. text files
15. text files15. text files
15. text files
 
Learn Java Part 4
Learn Java Part 4Learn Java Part 4
Learn Java Part 4
 
Stream
StreamStream
Stream
 
21csharp
21csharp21csharp
21csharp
 
Java 1
Java 1Java 1
Java 1
 
Dr. Rajeshree Khande - Java Interactive input
Dr. Rajeshree Khande - Java Interactive inputDr. Rajeshree Khande - Java Interactive input
Dr. Rajeshree Khande - Java Interactive input
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Networking
NetworkingNetworking
Networking
 
Classes of ip addresses
Classes of ip addressesClasses of ip addresses
Classes of ip addresses
 
Inputstream
InputstreamInputstream
Inputstream
 

Similar to Write and write line methods

04. Console Input Output
04. Console Input Output 04. Console Input Output
04. Console Input Output Intro C# Book
 
basic c++(1)
basic c++(1)basic c++(1)
basic c++(1)sajidpk92
 
c++ Lecture 1
c++ Lecture 1c++ Lecture 1
c++ Lecture 1sajidpk92
 
04 Console input output-
04 Console input output-04 Console input output-
04 Console input output-maznabili
 
434090527-C-Cheat-Sheet. pdf C# program
434090527-C-Cheat-Sheet. pdf  C# program434090527-C-Cheat-Sheet. pdf  C# program
434090527-C-Cheat-Sheet. pdf C# programMAHESHV559910
 
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfC# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfssuserc77a341
 
Csphtp1 03
Csphtp1 03Csphtp1 03
Csphtp1 03HUST
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++Marco Izzotti
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfssusera0bb35
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)ExcellenceAcadmy
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)ExcellenceAcadmy
 

Similar to Write and write line methods (20)

C#.net
C#.netC#.net
C#.net
 
04. Console Input Output
04. Console Input Output 04. Console Input Output
04. Console Input Output
 
basic c++(1)
basic c++(1)basic c++(1)
basic c++(1)
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
c++ Lecture 1
c++ Lecture 1c++ Lecture 1
c++ Lecture 1
 
04 Console input output-
04 Console input output-04 Console input output-
04 Console input output-
 
434090527-C-Cheat-Sheet. pdf C# program
434090527-C-Cheat-Sheet. pdf  C# program434090527-C-Cheat-Sheet. pdf  C# program
434090527-C-Cheat-Sheet. pdf C# program
 
C# basics
 C# basics C# basics
C# basics
 
C#
C#C#
C#
 
Basics1
Basics1Basics1
Basics1
 
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfC# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
Csphtp1 03
Csphtp1 03Csphtp1 03
Csphtp1 03
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdf
 
Cordovilla
CordovillaCordovilla
Cordovilla
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)
 

More from Dr.M.Karthika parthasarathy

IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...
IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...
IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...Dr.M.Karthika parthasarathy
 
Unit 1 Introduction to Artificial Intelligence.pptx
Unit 1 Introduction to Artificial Intelligence.pptxUnit 1 Introduction to Artificial Intelligence.pptx
Unit 1 Introduction to Artificial Intelligence.pptxDr.M.Karthika parthasarathy
 

More from Dr.M.Karthika parthasarathy (20)

IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...
IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...
IoT Enabled Wireless Technology Based Monitoring and Speed Control of Motor U...
 
Linux Lab Manual.doc
Linux Lab Manual.docLinux Lab Manual.doc
Linux Lab Manual.doc
 
Unit 2 IoT.pdf
Unit 2 IoT.pdfUnit 2 IoT.pdf
Unit 2 IoT.pdf
 
Unit 3 IOT.docx
Unit 3 IOT.docxUnit 3 IOT.docx
Unit 3 IOT.docx
 
Unit 1 Introduction to Artificial Intelligence.pptx
Unit 1 Introduction to Artificial Intelligence.pptxUnit 1 Introduction to Artificial Intelligence.pptx
Unit 1 Introduction to Artificial Intelligence.pptx
 
Unit I What is Artificial Intelligence.docx
Unit I What is Artificial Intelligence.docxUnit I What is Artificial Intelligence.docx
Unit I What is Artificial Intelligence.docx
 
Introduction to IoT - Unit II.pptx
Introduction to IoT - Unit II.pptxIntroduction to IoT - Unit II.pptx
Introduction to IoT - Unit II.pptx
 
IoT Unit 2.pdf
IoT Unit 2.pdfIoT Unit 2.pdf
IoT Unit 2.pdf
 
Chapter 3 heuristic search techniques
Chapter 3 heuristic search techniquesChapter 3 heuristic search techniques
Chapter 3 heuristic search techniques
 
Ai mcq chapter 2
Ai mcq chapter 2Ai mcq chapter 2
Ai mcq chapter 2
 
Introduction to IoT unit II
Introduction to IoT  unit IIIntroduction to IoT  unit II
Introduction to IoT unit II
 
Introduction to IoT - Unit I
Introduction to IoT - Unit IIntroduction to IoT - Unit I
Introduction to IoT - Unit I
 
Internet of things Unit 1 one word
Internet of things Unit 1 one wordInternet of things Unit 1 one word
Internet of things Unit 1 one word
 
Unit 1 q&amp;a
Unit  1 q&amp;aUnit  1 q&amp;a
Unit 1 q&amp;a
 
Overview of Deadlock unit 3 part 1
Overview of Deadlock unit 3 part 1Overview of Deadlock unit 3 part 1
Overview of Deadlock unit 3 part 1
 
Examples in OS synchronization for UG
Examples in OS synchronization for UG Examples in OS synchronization for UG
Examples in OS synchronization for UG
 
Process Synchronization - Monitors
Process Synchronization - MonitorsProcess Synchronization - Monitors
Process Synchronization - Monitors
 
.net progrmming part4
.net progrmming part4.net progrmming part4
.net progrmming part4
 
.net progrmming part3
.net progrmming part3.net progrmming part3
.net progrmming part3
 
.net progrmming part1
.net progrmming part1.net progrmming part1
.net progrmming part1
 

Recently uploaded

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 

Recently uploaded (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 

Write and write line methods

  • 1. Write and WriteLine Methods // Namespace Declaration using System; // Program start class class InteractiveWelcome { // Main begins program execution. public static void Main() { // Write to console/get input Console.Write("What is your name?: "); Console.Write("Hello, {0}! ", Console.ReadLine()); Console.WriteLine("Welcome to the C# Station Tutorial!"); } } Output: >What is your Name? <type your name here> [Enter Key] >Hello, <your name here>! Welcome to the C# Station Tutorial!
  • 2. Read and ReadLine Methods User input from the keyboard by using the Console.Read and Console.ReadLine methods. The Read Method Read reads the next character from the keyboard. It returns the int value –1 if there is no more input available. Otherwise it returns an int representing the character read. The ReadLine Method ReadLine reads all characters up to the end of the input line (the carriage return character). The input is returned as a string of characters. You can use the following code to read a line of text from the keyboard and display it to the screen: string input = Console.ReadLine( ); Console.WriteLine(“{0}”, input);
  • 3. The following codeshows some examples of how to use numeric formatting: Console.WriteLine(“Currency formatting – {0:C} {1:C4}”, 88.8,-888.8); Console.WriteLine(“Integer formatting – {0:D5}”, 88); Console.WriteLine(“Exponential formatting – {0:E}”, 888.8); Console.WriteLine(“Fixed-point formatting – {0:F3}”,888.8888); Console.WriteLine(“General formatting – {0:G}”, 888.8888); Console.WriteLine(“Number formatting – {0:N}”, 8888888.8); Console.WriteLine(“Hexadecimal formatting – {0:X4}”, 88);
  • 4. When the previous code is run, it displays the following: Currency formatting – $88.80 ($888.8000) Integer formatting – 00088 Exponential formatting – 8.888000E+002 Fixed-point formatting – 888.889 General formatting – 888.8888 Number formatting – 8,888,888.80 Hexadecimal formatting – 0058
  • 5. BOOLEAN DATA TYPE using System; class BooleanType { static void Main() { bool male = false; Random random = new Random(); male = Convert.ToBoolean(random.Next(0, 2)); if (male) { Console.WriteLine("We will use name John"); } else { Console.WriteLine("We will use name Victoria"); } } }
  • 6. VB Alias .NET Type Size Range sbyte System.SByte 1 byte -128 to 127 byte System.Byte 1 byte 0 to 255 short System.Int16 2 bytes -32,768 to 32,767 ushort System.UInt16 2 bytes 0 to 65,535 int System.Int32 4 bytes -2,147,483,648 to 2,147,483,647 uint System.UInt32 4 bytes 0 to 4,294,967,295 long System.Int64 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ulong System.UInt64 8 bytes 0 to 18,446,744,073,709,551,615
  • 7. using System; class Overflow { static void Main() { byte a = 254; Console.WriteLine(a); a++; Console.WriteLine(a); a++; Console.WriteLine(a); a++; Console.WriteLine(a); } } Output: 254 255 0 1
  • 8. using System; class Notations { static void Main() { int num1 = 31; int num2 = 0x31; Console.WriteLine(num1); Console.WriteLine(num2); } } Output: 31 49
  • 9. using System; public class Apples { static void Main() { int baskets = 16; int applesInBasket = 24; int total = baskets * applesInBasket; Console.WriteLine("There are total of {0} apples", total); } } Output: There are total of 384 apples
  • 10. C# Alias .NET Type Size Precision Range Float System.Single 4 bytes 7 digits 1.5 x 10-45 to 3.4 x 1038 Double System.Double 8 bytes 15-16 digits 5.0 x 10-324 to 1.7 x 10308 decimal System.Decimal 16 bytes 28-29 decimal places 1.0
  • 11. using System; class Floats { static void Main() { float n1 = 1.234f; double n2 = 1.234; decimal n3 = 1.234m; Console.WriteLine(n1); Console.WriteLine(n2); Console.WriteLine(n3); Console.WriteLine(n1.GetType()); Console.WriteLine(n2.GetType()); Console.WriteLine(n3.GetType()); } } To use a different type, we must use a suffix. The F/f for float numbers and M/m for decimal numbers.
  • 12. using System; class Notations { static void Main() { float n1 = 1.234f; float n2 = 1.2e-3f; float n3 = (float) 1 / 3; Console.WriteLine(n1); Console.WriteLine(n2); Console.WriteLine(n3); } } Output: 1.234 0.0012 0.3333333
  • 13. using System; class CSharpApp { static void Main() { float n1 = (float) 1 / 3; double n2 = (double) 1 / 3; if (n1 == n2) { Console.WriteLine("Numbers are equal"); } else { Console.WriteLine("Numbers are not equal"); } } } Output: Numbers are not equal
  • 14. Enumerations using System; class Enumerations { enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
  • 15. Sunday } static void Main() { Days day = Days.Monday; if (day == Days.Monday) { Console.WriteLine("It is Monday"); } Console.WriteLine(day); foreach(int i in Enum.GetValues(typeof(Days))) Console.WriteLine(i); } }
  • 16. ARRAYS using System; class ArrayExample { static void Main() { int[] numbers = new int[5]; numbers[0] = 3; numbers[1] = 2; numbers[2] = 1; numbers[3] = 5; numbers[4] = 6; int len = numbers.Length; for (int i=0; i<len; i++) {