Principle Programming by C# Lec 5
Principle Programming by C# Lec 5
First Year
______________________________
Week: 5 C#
c#
Lecturer
Emad Majed Rashed
1
What Is the 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 (usually on the computer screen).
Using Console.Write(…) and Console.WriteLine(…)
some examples of printing various types of
// Print String
Console.WriteLine("Hello World");
// Print int
Output:
Console.WriteLine(5); Hello World
// Print double 5
Console.WriteLine(3.14159265358979); 3.14159265358979
2
Difference between Write(…) and WriteLine(…) for
Example:
{
Console.WriteLine("I love");
Console.Write("this ");
Console.Write("\n Book!");
}
3
Addition operation (+) which concatenates (joins) two
strings and returns as result a new string.
{
string age = "twenty six";
string text = "He is " + age + " years old.";
Console.WriteLine(text);
}
Output:
He is twenty six years old.
Concatenation of Mixed Types
int age = 26;
string text = "He is " + age + " years old.";
Console.WriteLine(text);
Output:
He is 26 years old.
4
Formatted Output – Examples
string name = "John";
int age = 18;
string town = "Akre";
Console.Write("{0} is {1} years old from {2}!\n", name, age, town);
Output:
John is 18 years old from Akre!
The expression {0} means to put in its place the first of the arguments
submitted after the format string (in this case name). Next is {1} which
means to replace with the second of the arguments (age). The last
{ Console.WriteLine("{0,6}", 123);
Console.WriteLine("{0,6}", 1234);
Console.WriteLine("{0,6}", 12);
Console.Write("{0,-6}", 123);
Console.WriteLine("--end");
}
6
StandardNumericFormats.
{
Console.WriteLine("{0:C2}", 123.456);
//Output: $123,46 .
Console.WriteLine("{0:D6}", -1234);
//Output: -001234
Console.WriteLine("{0:E2}", 123);
//Output: 1,23E+002
Console.WriteLine("{0:F2}", -123.456);
//Output: -123.46
Console.WriteLine("{0:N2}", 1234567.8);
//Output: 1 234 567.80
Console.WriteLine("{0:P}", 0.456);
//Output: 45.60 %
Console.WriteLine("{0:X}", 11);
//Output: B
}
7
Console Input
The class
Console provides two methods Console.Read() and
Console.ReadLine() that run on this stream and usually reading
from the console is done by them.
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
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);
}
}
}
// Output: Please enter your first name: John
// Please enter your last name: Smith
// Hello, John Smith! 8
Console.Read()
The method Read() is different than ReadLine(). Read() is
reads only one character and not the entire line. The other
significant difference is that the method does not return
directly the read character but its code. If we want to use the
result as a character we must convert it to a character or use
the method Convert.ToChar() on it. There is one important
characteristic: the character is read only when the [Enter]
key is pressed.
9
Reading Numbers
Reading numbers from the console in C# is not done
directly. In order to read a number we should have previously read the
input as a string (using ReadLine()) and then convert this string to a
number. The operation of converting a string into another type is called
parsing.
namespace ConsoleApplication4
{ class Program
{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);
}
}
} 10
2.Conditional Operator ?:
The conditional operator ?: uses the Boolean
value of an expression to determine which of two
other expressions must be calculated and returned
as a result.
The operator
?: has the following syntax:
operand1 ? operand2 : operand3
if operand1 is set to true, the operator returns as a result
operand2. Otherwise (if operand1 is set to false), the operator
returns as a result operand3.
namespace ConsoleApplication4
{ class Program
{ static void Main(string[] args)
{ int a = 6; int b = 14;
Console.WriteLine(a > b ? a : b);
}
} 11
}
3. Comma operator: (,)
• For example:
• int m, n, min;
12
4.Operator for Concatenation of Strings
The operator + is used to join strings (string). It
concatenates (joins) two or more strings and returns
the result as a new string. For Example:
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int a = 5; int b = 7;
string sum = "Sum = " + (a + b);
Console.WriteLine(sum);
String incorrect = "Sum = " + a + b;
Console.WriteLine(incorrect);
}
}
} 15
Comments
16
Q1: Write a program to calculate a sum of two numbers:
Q2:Write a program to calculate a sum of two numbers entered
from keyboard.
the user.
17
Q4. Write a C# Sharp program that takes three letters as input and display them in
reverse order.
Test Data
Enter letter: b
Enter letter: a
Enter letter: t
Expected Output :
tab
using System;
public class Exercise1
{ public static void Main()
{ char letter,letter1,letter2;
Console.Write("Input letter: ");
letter = Convert.ToChar(Console.ReadLine());
Console.Write("Input letter: ");
letter1 = Convert.ToChar(Console.ReadLine());
Console.Write("Input letter: ");
letter2 = Convert.ToChar(Console.ReadLine());
Console.WriteLine("{0} {1} {2}",letter2,letter1,letter); } } 18
Q5. Write a C# Sharp program that takes four numbers as input to calculate and print the average.
Enter the First number: 10
Enter the Second number: 15
Enter the third number: 20
Enter the four number: 30
Expected Output:
The average of 10 , 15 , 20 , 30 is: 18
using System;
using System.IO;
public class Exercise9
{ public static void Main()
{ double number1,number2,number3,number4;
Console.Write("Enter the First number: ");
number1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the Second number: ");
number2 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the third number: ");
number3 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the fourth number: ");
number4 = Convert.ToDouble(Console.ReadLine());
double result = (number1 + number2 + number3 + number4) / 4;
Console.WriteLine("The average of {0}, {1}, {2}, {3} is: {4} ", number1, number2, number3, number4, result);
}
} 19
Q7.Write a program that reads from the console
three numbers of type int and prints their sum.
Q8. Write a program that reads from the console the
radius "r" of a circle and prints its perimeter and
area.
20