Input/Output Statement Thanachat Thanomkulabut
Input Statement Console.ReadLine() Return string Use to get the input from user Convert string to other data type int.Parse() Convert string to integer double.Parse() Convert string to double Example string st; st = Console.ReadLine(); Statement 2
Example: Input Statement Ex1: string myname; myname = Console.ReadLine(); Ex2: int Width; string temp1; temp1 = Console.ReadLine(); Width = int.Parse(temp1); Statement 3
Output Statements Use the method Write or WriteLine in the Console class (which is in System namespace) Basic usage: Advanced usage: Even more advanced usage: Console.WriteLine(”Size {0}x{1}”, width, height); double salary=12000; Console.WriteLine("My salary is {0:f2}.", salary); Console.WriteLine("Hello");Console.WriteLine(area); Statement 4
Test I 5 Write the program which Input : Your name Output : Your name is.
The Math Class Method/ Constant Value returnedExample CallResult PI Value of Math.PI Max(x,y)Larger of the twoMath.Max(1,2)2 Abs(x)Absolute value of xMath.Abs(-1.3)1.3 Sqrt(x)Square-root of xMath.Sqrt(4.0)2.0 Round(x)Nearest integer to xMath.Round(0.8)1 Pow(x,y)xyxy Math.Pow(3,2)9.0 Log(x)Natural log of xMath.Log(10) Ceiling(x)Smallest integer greater than or equal to x Math.Ceiling(4.1)5 Cos(x)Cosine of x radiansMath.Cos(Math.PI) Math Class 6
Test II 7 Write the program which Input : Nothing Output : cos( π /3). | ln(3) |
Outline 8 Data Types Conversion Read Statement Write Statement
Data Types 9 TypeSizeDescriptionRange bool1 byteStore truth valuetrue / false char1 byteStore one charactercharacter code 0 – 255 byte1 byteStore positive integer0 – 255 short2 byteStore integer-32, ,767 int4 byteStore integer-2.1 x x 10 9 long8 byteStore integer-9.2 x x double16 byteStore real number± 5.0x ± 1.7x stringN/AStore sequence of characters N/A
Outline 10 Data Types Conversion Read Statement Write Statement
Convertion Number Convertion Type Convertion ConvertParse 11
Number Conversion Number conversion can be done with command = (type) Example byte b; int i; long l; double d; i = (int)b; l = (long)b+(long)i; 12 Bigger Smaller int byte long byte int long 100%
Example byte b; int i; long l; double d; d = (double)i /10; b =(byte) l +3; Number Conversion i =(int)d – 10; 13 int double byte long double int 100% May wrong in some case 100%
Type Conversion 14 Convertion Number Convertion Type Convertion ConvertParse
i = Convert.ToInt32(c); c = Convert.ToChar(b); st = Convert.ToString(i); Convert.To (); byte b=100; int i=100; double d=100.0; char c=‘d’; string st=“1”; b = Convert.ToByte(i); d = Convert.ToDouble(st); 15 int byte char int string double charbyte
.Parse(); Can Convert only from string to others Example d = double.Parse(st); 16 byte b=100; int i=100; double d=100.0; char c=‘d’; string st=“1”; string double i = int.Parse(st); c = char.Parse(st); b = byte.Parse(st); stringint
Outline 17 Data Types Conversion Read Statement Write Statement
Read Statement Read statement Console. Read() Console. ReadLine() Console.Read(); Read Acsii code of first character Console.ReadLine(); Read all character (in form of string) Use with Convert or Parse commands frequently 18
Console.Read() Example 19 char c; int i; i = Console.Read(); Console.WriteLine(i); c = Convert.ToChar(i); Console.WriteLine(c); c char i int ABCD 65 A A A monitor
Console.ReadLine() Example 20 string st; st = Console.ReadLine(); Console.WriteLine(st); st string ABCD monitor Console.ReadLine() will always get input in string form
Console.ReadLine() Example Console.ReadLine() will always get input in string type if you would like other type you must convert it again Example 21 string st; int i; st = Console.ReadLine(); i = int.Parse(st); string st; int i; i = int.Parse(Console.ReadLine()); OR
Outline 22 Data Types Conversion Read Statement Write Statement
Write Statement Console. Write() Console. WriteLine() Console.Write(); Show output Console.WriteLine(); Show output and shift to new line 23
Write Statement Example 24 Console.Write(“Happy”); Console.Write(2009); Happy2009 Output : Console.WriteLine(“Happy”); Console.WriteLine(2009); Happy 2009 Output : string integer
Write Statement Write() & WriteLine() can use in many form Console.Write( ); Console.Write(“ ”,,...); 25 Hello 5 Console.Write(“Hello”); int a=5; Console.Write(a); string name = “Mc”; int score=9; Console.Write(“Score of {0} is {1}”,name,score); Score of Mc is 9 {0}{1}
Write Statement Example 26 int a=7, b=2; Console.WriteLine(“a={0} and b={1}”,a,b); Console.WriteLine(“{1}+{0}={2}”,b,a,a+b); a=7 and b= = 9 {0}{1} {0}{1}{2}
27 Write Statement : Example 27
Write Statement : Formatting Output 28 Console.WriteLine(“{0}”,10); 10 Console.WriteLine(“{0:F}”,10); Console.WriteLine(“{0:X}”,10); A Console.WriteLine(“{0:D}”,10); 10 Console.WriteLine(“{0:P}”,10); 1, % Console.WriteLine(“{0:E}”,10); E+001
Write Statement : Formatting Output 29 SymbolMeaningExampleOutput D, dDecimalConsole.WriteLine( “ {0:D} ",10};10 E, eExponentialConsole.WriteLine( “ {0:E} ",10}; E+001 F, fFix PointConsole.WriteLine( “ {0:F} ",10};10.00 X, xHexadecimalConsole.WriteLine( “ {0:X} ",10};A P, pPercentConsole.WriteLine( “ {0:P} ",10};1, %
Special Character Symbols Output \’Single Quote \”Double Quote \\Backslash \nNew line \tHorizontal tab What should you do if you would like to WriteLine “C:\mydocument” 30 Console.WriteLine(““C:\mydocument””); Console.WriteLine(“\“C:\mydocument\””); Console.WriteLine(“\“C:\\mydocument\””);
Test III 31 Write the program which Input : 3 number Output : average of 3 input number
Test IV 32 Write the program which Input : lenght of radius of circle Output : Area of circle
Any question?