Week 1 Lecture 1
Week 1 Lecture 1
Click on
“Create a new project”
Select the Project
Select the Project
Press Create
C# Program Basic Structure
C# Program Basic Structure
By default it is
including some libraries.
Just like we did
#include<iostream>
In C++
C# Program Basic Structure
Here namespace is
the name of the project that we
wrote.
C# Program Basic Structure
In C#,
Alternative to getch() is
Console.Read();
Output on the Console
Displaying Output on Console with endl
Displaying Output on Console with endl
In C#,
Alternative to cout with endl is
Console.WriteLine(“ ”);
Declaring Variables
in C#
Declaring Variables: int datatype
Same as in C++
Declaring Variables: string datatype
Same as in C++
Declaring Variables: char datatype
Same as in C++
Declaring Variables: float datatype
Only difference is
that you have to
write F at the end
of the number to
explicitly mention
that it is a floating
point number
Taking Input
in C#
Taking Input
Alternative to cin is
Console.ReadLine();
Taking Input
Input is always in a
string. Therefore if
we want to take a
number as input then
we have do
typecasting.
Taking Input an int Number
TypeCasting,
Converting string into
integer.
Taking Input a float Number
TypeCasting,
Converting string into
float.
Working Example: Vision
Write a program that takes length of side of a square
as input and calculate its area using following formula:
Area= Length * Length
Working Example: Solution
Conditional Statements,
Comparison and Logical
Operators
in C#
Working Example: Vision
We want to print “You are Passed” when Marks are
greater than 50 but print “You are Failed” when Marks
are less than or equal to 50.
Working Example: Solution
Same as in C++
Loops
in C#
Loops in C#
We have two type of loops
● Counter Loops
● Conditional Loops
Counter Loop
Count the steps and execute until specific number of
times.
Condition Loop
Execute the steps until a specific condition is not met.
Working Example: Vision
We want to print “Welcome Jack” 5 times.
For Loop: Solution
Same as in C++
Working Example: Vision
Write a program that keeps taking input from the user and
sum up all these input until he enters -1. When user enters
-1 the program should print sum of all values.
While Loop: Solution
Same as in C++
Do While Loop: Solution
Same as in C++
Working Example: Vision
Write a program that takes 3 numbers as input and
print the largest of them.
Why do not we
Solution with Variables declare multiple
variables and use a
for loop ?
It would be easier if
Solution with Variables we have same
variable name for
same type of data
and if it require to
access any data we
just give its number
Arrays
in C#
Arrays
You can access variables through same name with different
index value [0]
Output
Print on the console one single line:
1. If Lilly's money is enough:
“Yes! {N}” – where N is the remaining money after the
purchase
2. If the money is not enough:
“No! {M}” – where M is the insufficiency amount
Self Assessment
Input Output Comments
10 Yes! 5.00 For the first birthday she gets a toy; 2nd -> 10 USD; 3rd -> toy;
170.00 4th -> 10 + 10 = 20 USD; 5th -> toy; 6th -> 20 + 10 = 30 USD;
7th -> toy; 8th -> 30 + 10 = 40 USD; 9th -> toy; 10th -> 40 + 10
6
= 50 USD.
She has saved: 10 + 20 + 30 + 40 + 50 = 150 USD. She sold 5 toys
for 6 USD each = 30 USD.
Her brother took 1 USD 5 times = 5 USD. Remaining amount: 150 +
30 – 5 = 175 USD.
175 >= 170 (price of the washing machine): she managed to buy it
and is left with 175-170 = 5 USD.
21 No! 997.98 She has saved 550 USD. She has sold 11 toys, 3 USD each = 33
1570.98 USD. Her brother has taken for 10 years 1 USD each year = 10
USD. Remaining amount: 550 + 33 - 10 = 573 USD.
3
573 < 1570.98: she did not manage to buy a washing machine. The
insufficiency amount is: 1570.98 - 573 = 997.98 USD.