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

Principle Programming by C# Lec 5

The document provides an overview of programming fundamentals in C# for first-year students at Duhok Polytechnic University, focusing on the Console class for input and output operations. It covers various topics including string concatenation, formatted output, reading user input, and basic arithmetic operations. Additionally, it includes examples and exercises to reinforce the concepts discussed.

Uploaded by

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

Principle Programming by C# Lec 5

The document provides an overview of programming fundamentals in C# for first-year students at Duhok Polytechnic University, focusing on the Console class for input and output operations. It covers various topics including string concatenation, formatted output, reading user input, and basic arithmetic operations. Additionally, it includes examples and exercises to reinforce the concepts discussed.

Uploaded by

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

Programming Fundamentals by C#

Duhok Polytechnic University

Akre Technical Institute

Information Technology Department

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!");
}

The output of this example is:


I love
this 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

placeholder is {2}, which means to replace with the next parameter

(town). Last is \n, which is a special character that indicates moving to a


new line.
5
Alignment Component:
The alignment component is optional and indicates the
string alignment. It is a positive or negative integer
and the positive values indicate alignment to the right
and the negative – alignment to the left.

{ 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: (,)

• Multiple expressions can be combined into one

expression using the comma operator.

• For example:

• int m, n, min;

• int mCount = 0, nCount = 0;

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:

{string csharp = "C#";


string dotnet = ".NET";
string csharpDotNet = csharp + dotnet;
Console.WriteLine(csharpDotNet); // C#.NET
string csharpDotNet4 = csharpDotNet + " " + 5;
Console.WriteLine(csharpDotNet4); // C#.NET 5
} 13
Explicit Type Conversion – Example

{double myDouble = 5.1d;


Console.WriteLine(myDouble); // 5.1
long myLong = (long)myDouble;
Console.WriteLine(myLong); // 5
myDouble = 5e9d; // 5 * 10^9
Console.WriteLine(myDouble); // 5000000000
Console.WriteLine(int.MaxValue); // 2147483648
Console.WriteLine(int.MinValue); // -2147483648
Console.WriteLine(byte.MinValue); //0
Console.WriteLine(byte.MaxValue); //255
Console.WriteLine(float.MinValue);
Console.WriteLine(float.MaxValue);
} 14
Conversion to String – 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

• A comment is a piece of descriptive text which


explains some aspect of a program. C#
provides two types of comment delimiters:
• Anything after // (until the end of the line on
which it appears) is considered a comment.
• Anything enclosed by the pair /* and */ is
considered a comment.

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.

Q3.Write a program that prints on the console the perimeter

and the area of a rectangle by given side and height entered by

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.

Q8: What do you means by identifiers?

Q9: What do you means by case-sensitive?

Q10: What do you means by reserved word?

20

You might also like