0% found this document useful (0 votes)
27 views30 pages

Introduction To Programming Using C#: By: Eng - Omar Islam

Uploaded by

Sayed Younis
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)
27 views30 pages

Introduction To Programming Using C#: By: Eng - Omar Islam

Uploaded by

Sayed Younis
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/ 30

Introduction to

Programming
Using C#
Lab 2

By:
Eng.Omar Islam
Content

• Variables and Data types


• Type Casting
• Type Conversion
Part I

• What are variables ? And how they are declared


and used.
• How to assign values to these variables
• Getting values from the user
• Printing user’s values.
Variables is like boxes
SO instead of

• Console.WriteLine(“Omar”);
_______________________________________

• string firstName= “Omar”;


• Console.WriteLine(firstName);
• Console.WriteLine(firstName);
This is called expression

dataType variableName = value;

string firstName = “Omar” ;


int myAge = 25;
What are Variables/Constants
• Variables are the names you give to computer
memory locations which are used to store values in
a computer program
• Constants are also store a value but it can never
change in the application
Data Types
Variable Naming Rules
• The name must start with a letter or
an underscore (_). Example: omar or _omar
• The name cannot start with a number.
Example: 44omar
• Names are case-sensitive (e.g., Age and age are
different variables).
• It must not contain spaces, symbols (except
underscores), or C# reserved keywords (like int,
class, namespace, etc.).
Values Rules

• The value must be compatible with the data type.


• For numeric data types (int, float, double), it can be a
number.
• For char, the value must be a single character
enclosed in single quotes (e.g., 'A').
• For string, the value must be a sequence of
characters enclosed in double quotes (e.g., "Hello").
• For bool, the value must be true or false
Two ways of declaring variables

• int x =5;

• int x; //Declaration
x = 5; //Initialization

YOU CANNOT USE A VARIABLE THAT IS


NOT DECLARED
XXXX Y=5; XXXX
Practice 1

• Write the following statements in the editor:

int x=6,y=3;
s=x+Y;
Console.Writeline(“The first number is:”+ x);
Console.WriteLine(“The Second number is:”, y);
Console.WriteLine(“The Summation of {0} and {1} = {2}” ,x,y,s);

1- The application will not run for some errors .. Find these errors and correct them.\

2- Change the program to subtract the two numbers.


Now let’s take the input
dynamically from the user
Console.WriteLine(“Enter the first Number: ”);
int x=int.Parse(Console.ReadLine());
Console.WriteLine(“Enter the second Number: ”)
int y=int.Parse(Console.ReadLine());
int s=x+y;
Console.WriteLine(“The first number is:”+ x);
Console.WriteLine(“The Second number is:”+ y);
Console.WriteLine(“The Summation of {0} and {1} = {2}” ,x,y,s);
Practice 2

• Write a program that defines two variables:


The first variable is string which defines the user’s Name
,The second variable is int which is the user’s Age,
and print the values of the two variables.
Casting

• Definition: is the process of converting


a variable from one data type to another.
• Types of Casting:
1- Implicit Casting.
2- Explicit Casting.
Data Types
Implicit Casting(Automatic)

• Performed automatically by the compiler when


moving from a smaller data type to a larger
type.
• No data loss occurs.
• Example:
Explicit Casting (Manual)
• When converting from a larger to a smaller
type, you need to cast explicitly.
• Deals with compatible Data types
• There is a risk of data loss.
• The decimal part is truncated, not rounded.
• Example:
.Parse() Method

• The .Parse() method is used to


convert a string representation of a number
(or other data types) into its corresponding
numeric type.
It's a form of type conversion because It deals
with incompatible types, but it’s specifically
used when you are converting
a string (like "123") into a number
(like 123).
Casting-Conversion-Parsing
Let’s Try

• Write a program where the user enters an


integer, and it's implicitly cast to a double.
Then explicitly cast it back to an integer and
display the result.
Ans.
Practice 2

• Write a program that takes a floating-point


number as input and prints its rounded value to
the screen.
Steps

• Take the input from the user and store it in


double variable.
• Add 0.5 to the input
• If the input is 3.4, adding 0.5 gives 3.9
• If the input is 3.6, adding 0.5 gives 4.1.
• Cast the float into int to get the rounded value.
Practice 2 Answer
Assignment 1

• Ask the user to input a decimal number and a


string, cast the decimal to an integer, and
concatenate the string and integer. Print the
result.
Ans.
Assignment 2

• Ask the user for a string representation of a


floating-point number and an integer, then
convert both and perform addition.
Ans.
Thank You

You might also like