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

CIT304 Chapter 5

The document discusses input and output statements in C#. It covers the console in C#, standard input, standard output, and formatting strings. It provides examples of reading input from the console and writing output to the console using different formatting.

Uploaded by

Bolaji Ogunlana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CIT304 Chapter 5

The document discusses input and output statements in C#. It covers the console in C#, standard input, standard output, and formatting strings. It provides examples of reading input from the console and writing output to the console using different formatting.

Uploaded by

Bolaji Ogunlana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Programming with C Sharp (CIT 304)

Input and Output Statement in C#

Adedoyin I. OYEBADE
[email protected]

Bowen University,
Iwo, Nigeria

May 2, 2023

OYEBADE A. (BU) CIT 304 May 2, 2023 1 / 19


Presentation Overview

1 Console Input and Output in C#


Console in C#
Standard Input and Standard Output
The ”formatString” Component
Assignment

OYEBADE A. (BU) CIT 304 May 2, 2023 2 / 19


Console in C#
1 Console
• The Console is a window of the operating system through which
users can interact with system programs of the operating
system or with other console applications
• The interaction consists of text input from the standard input
(usually keyboard) or text display on the standard output
2 Command Interpreter
• This is also known as Shell or command prompt.
• is a console-based program in the operating system, which
provides access to system commands as well as a wide range of
programs
3 Types of Command Interpreter
• CLI – Command Line Interface – is a console for commands
(such as cmd.exe in Windows and bash in Linux)
• GUI – Graphical User Interface – is a graphical work
environment (such as Windows Explorer)
OYEBADE A. (BU) CIT 304 May 2, 2023 3 / 19
Windows Console Commands

Figure: Window Console Commands

OYEBADE A. (BU) CIT 304 May 2, 2023 4 / 19


Standard Input

1 Standard Console Input


• The class Console provides two methods
• Console.Read() and Console.ReadLine()

2 Reading through Console.ReadLine()


• It waits for input from the console
• The method Console.ReadLine() returns as result the string
entered by the user.

OYEBADE A. (BU) CIT 304 May 2, 2023 5 / 19


Standard Input Cont..

1 Reading text from console


class UsingReadLine
{
static void Main()
{
Console.Write(”Please enter your first name: ”);
string firstName = Console.ReadLine();
Console.Write(”Please enter your last name: ”);
string lastName = Console.ReadLine();
Console.WriteLine(”Hello, 0 1!”, firstName, lastName);
}
}

OYEBADE A. (BU) CIT 304 May 2, 2023 6 / 19


Standard Input Cont..
1 Reading numbers from console
class ReadingNumbers
{
static void Main()
{
Console.Write(”a = ”);
int a = int.Parse(Console.ReadLine());
Console.Write(”b = ”);
int b = int.Parse(Console.ReadLine());
Console.WriteLine(”{0} + {1} = {2}”, a, b, a + b);
Console.WriteLine(”{0} ∗ {1} = {2}”, a, b, a ∗ b);
Console.Write(”f = ”);
double f = double.Parse(Console.ReadLine());
Console.WriteLine(”{0} ∗ {1}/{2} = {3}”, a, b, f , a ∗ b/f );
}
}
OYEBADE A. (BU) CIT 304 May 2, 2023 7 / 19
Standard Input and Standard Output Cont..

1 Reading floating numbers from console


class Reading Floating Numbers
{
static void Main()
{
Console.Write(”Enter a floating-point number: ”);
string line = Console.ReadLine();
double number = double.Parse(line);
Console.WriteLine(”You entered: {0}”, number);
}
}

OYEBADE A. (BU) CIT 304 May 2, 2023 8 / 19


Standard Output

1 Standard Console Output


• This can be used to print out all basic types of output
class WritinOutConsole
{
static void Main()
{
Console.WriteLine(”I love”);
Console.Write(”this ”);
Console.Write(”Book!”);
}
}

OYEBADE A. (BU) CIT 304 May 2, 2023 9 / 19


Standard Output

1 Concatenation of Strings
• This allows chaining of concatenate (+) operations one after
another in a sequence.
class WritinOutConsole
{
static void Main()
{
string age = ”twenty six”;
string text = ”He is ” + age + ” years old.”;
Console.WriteLine(text);
}
}

OYEBADE A. (BU) CIT 304 May 2, 2023 10 / 19


Standard Output

1 Formatted Output with Write(. . . ) and WriteLine(. . . )


• The main idea is to adopt a special string, formatted with special
formatting characters and list of values, which should be
substituted in place of “the format specifiers”.
class WritinOutConsole
{
static void Main()
{
string str = ”Hello World!”;
Console.Write(str);
Console.Write(”{0}”, str); }
}

OYEBADE A. (BU) CIT 304 May 2, 2023 11 / 19


Standard Output
1 Composite Formatting
• used for console printing as well as in certain operations with
strings
2 Alignment Component
• It is a positive or negative integer
• the positive values indicate alignment to the right
• the negative values indicate alignment to the left
class WritinOutformatting
{
static void Main()
{
Console.WriteLine(”{0,6}”, 123);
Console.WriteLine(”{0,6}”, 1234);
Console.WriteLine(”{0,6}”, 12);
Console.Write(”{0,-6}”, 123);
Console.WriteLine(”–end”);
}
}
OYEBADE A. (BU) CIT 304 May 2, 2023 12 / 19
Standard Output: Composite Formatting

1 Composite Formatting

class WritinOutConsole
{
static void Main()
{
string name = ”John”;
int age = 18;
string town = ”Seattle”;
Console.Write(”{0} is {1} years old from {2}!”, name, age,
town);
}
}

OYEBADE A. (BU) CIT 304 May 2, 2023 13 / 19


The ”formatString” Component

1 The ”formatString” Component


• The component is used for specific string formating
2 The ”formatString” Component Types are
• for numerical types of arguments

• for arguments of type date (DateTime)

• for arguments of type enumeration (listed types)


3 Format String Components for Numbers
• standard-defined formats

• user-defined formats

OYEBADE A. (BU) CIT 304 May 2, 2023 14 / 19


Standard Output: Composite Formatting
1 Standard Formats for Numbers
• These formats are defined by one of several format specifiers,
which are letters with particular importance
class WritinOutConsole
{
static void Main()
{
Console.WriteLine(”{0:C2}”, 123.456);
Console.WriteLine(”{0:D6}”, -1234);
Console.WriteLine(”{0:E2}”, 123);
Console.WriteLine(”{0:F2}”, -123.456);
Console.WriteLine(”{0:N2}”, 1234567.8);
Console.WriteLine(”{0:P}”, 0.456);
Console.WriteLine(”{0:X}”, 254);
}
}
OYEBADE A. (BU) CIT 304 May 2, 2023 15 / 19
Standard Output: Composite Formatting

1 Custom Formats for Numbers


• All formats that are not standard are assigned to the user
formats
class WritinOutConsole
{
static void Main()
{
Console.WriteLine(”{0:0.00}”, 1);
Console.WriteLine(”{0 : #.##}”, 0.234);
Console.WriteLine(”{0 : #####}”, 12345.67);
Console.WriteLine(”{0 : (0#)#######}”, 29342525);
Console.WriteLine(”{0 : %##}”, 0.234);
}
}

OYEBADE A. (BU) CIT 304 May 2, 2023 16 / 19


Standard Output: Composite Formatting

1 Format String Components for Dates


• This component is used to format date and time in a format
suitable for the user
class WritinOutConsole
{
static void Main()
{
DateTime d = new DateTime(2012, 02, 27, 17, 30, 22);
Console.WriteLine(”{0:dd/MM/yyyy HH:mm:ss}”, d);
Console.WriteLine(”{0:d.MM.yy}”, d);
}
}

OYEBADE A. (BU) CIT 304 May 2, 2023 17 / 19


Standard Input and Output: Practice Questions
1 Format String Components for Dates
class WritinOutConsole
{
static void Main()
{
Console.WriteLine(”This program calculates ” + ”the area
of a rectangle or a triangle”);
Console.WriteLine(”Enter a and b (for rectangle) ” + ”or a
and h (for triangle): ”);
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
Console.WriteLine(”Enter 1 for a rectangle or ” + ”2 for a
triangle: ”);
int choice = int.Parse(Console.ReadLine());
double area = (double) (a ∗ b) / choice;
Console.WriteLine(”The area of your {0} is {1:#.##} ”,
choice, area)
OYEBADE A. (BU) CIT 304 May 2, 2023 18 / 19
Assignment

1 Write a program that reads from the console three numbers of


type int and prints their sum

2 Write a program that reads from the console the radius ”r” of a
circle and prints its perimeter and area.

3 Write a program that reads two numbers from the console and
prints the greater of them.

4 Write a program that prints on the console the first 100


numbers in the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
55, 89, 144, 233, . . .

OYEBADE A. (BU) CIT 304 May 2, 2023 19 / 19

You might also like