Lu1 - Lo 5 - 8
Lu1 - Lo 5 - 8
PROGRAMMING 2A
PROG6221
Lecturer: Mr. Mpho Gift Doctor Gololo
Class contents
namespace User_Input
{
internal class Program
{
static void Main(string[] args)
{
// Type your username and press enter
Console.WriteLine("Enter username:");
// Create a string variable and get user input from the keyboard and
store it in the variable
string userName = Console.ReadLine();
// Print the value of the variable (userName), which will display the
input value
Console.WriteLine("Username is: " + userName);
}
}
}
Write a console program that requires user input
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Convert_methods
{
internal class Program
{
static void Main(string[] args)
{
string userInput;
int intVal;
double doubleVal;
Console.WriteLine(sum);
Console.ReadLine();
}
}
}
Write a console program that requires user input
• Write a console application to calculate the area of the rectangle with user input
• Calculate the area of the right-angle triangle with user input from the orange triangle
• Extra problem – use an to check if the area of the two right-angle triangles will sum to the area of
the rectangle
12 m
20 m 8m
Write a console program that requires user input
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Area_Calculation
{
internal class Program
{
static void Main(string[] args)
{
string userInput_length;
int length;
string userInput_height;
int height;
string userInput_base;
int base_side;
Console.WriteLine(Area_rectacngle);
Console.WriteLine(Area_triange);
Console.WriteLine(sum_Areas);
Console.WriteLine(sum_Areas == Area_rectacngle);
}
}
Apply string manipulation to solve a programming problem
• System.String provides a number of methods you would expect from
such a utility class, including methods that return the length of the
character data, find substrings within the current string, and convert
to and from uppercase/lowercase
Apply string manipulation to solve a programming problem
• Given a string text = “cdscghdc”, then we can get length with txt.length
Apply string manipulation to solve a programming problem
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;
• namespace Char_string
• {
• internal class Program
• {
• static void Main(string[] args)
• {
• char[] arr = { 'M', 'p', 'h','o' };
• Console.WriteLine(arr.Length);
•
• string Combined_characters = new string(arr);
• Console.WriteLine(Combined_characters);
• }
• }
• }
Apply string manipulation to solve a programming problem
Namespace Strings_Example{
class Program{
static void (){
string firstName = "John";
string lastName = "Doe";
string name = $"My full name is: {firstName} {lastName}";
Console.WriteLine(name);
}
}
}
Apply string manipulation to solve a programming problem
Formatting
• The tokens such as {0} and {1} are embedded within various string
literals.
• Used when you are defining a string literal that contains segments of
data whose value is not known until runtime, you are able to specify a
placeholder within the literal using this curly-bracket syntax.
• At runtime, the values passed into Console.WriteLine() are substituted
for each placeholder.
• Note that the first ordinal number of a curly-bracket placeholder
always begins with 0.
Apply string manipulation to solve a programming problem
Apply string manipulation to solve a programming problem
Apply string manipulation to solve a programming problem
String Methods
Apply string manipulation to solve a programming problem
• From the paragraph you get, check for the following substrings using
the Contains method
• Nights, sun, mountain, trip, reached, gone, wrong.
using System;
// Main method
Console.WriteLine(GFG.imp);
}
Use implicitly typed variables in a program
• var a = 20;
// Allowed
• var data = new int [] {23, 34, 455, 65};
Use implicitly typed variables in a program
using System;
// Main method
var a = 50;
var c = 340.67d;
var d = false;
}
Use implicitly typed variables in a program
// C# program to illustrate the
using System;
class GFG {
// Main method
// Display result
Console.WriteLine("Height of triangle is: " + Height + "\nBase of the triangle is: " + Base);
}
Use implicitly typed variables in a program
using System;
// Main method
Console.WriteLine(data);
}
Use implicitly typed variables in a program
// C# program to illustrate
using System;
{"Python", "C#"} };
// taking a string
string a;
a = language[1, 0];
Console.WriteLine(a);
a = language[0, 1];
Console.WriteLine(a);
}
Use implicitly typed variables in a program
// C# program to illustrate
using System;
class GFG {
// Main method
var jarray = new[] { new[] { 785, 721, 344, 123 }, new[] { 234, 600 }, new[] { 34, 545, 808 }, new[] { 200, 220 }
};
Console.WriteLine();
}
Use implicitly typed variables in a program
Problem of the day:
A=2x3 B=3x3
A = [1 4 2B = [3 4 2
2 5 1] 357
1 2 1]
• C = 2 x 3 Matrix
Purpose of nullable
• Null types can only work with Value Type, not with Reference Type.
• The Value Data Types will directly store the variable value in memory
and it will also accept both signed and unsigned literals.
• Null types can only work with Value Type, not with Reference Type.
• The Value Data Types will directly store the variable value in memory
and it will also accept both signed and unsigned literals.
• Signed & Unsigned Integral Types : There are 8 integral types which
provide support for 8-bit, 16-bit, 32-bit, and 64-bit values in signed or
unsigned form.
• Floating Point Types :There are different floating point data types
which contain the decimal point. E.g: Float, double, Decimal
Value types
// C# program to demonstrate ulong ul = 3624573;
// the above data types // by default fraction value
using System; // is double in C#
double d = 8.358674532;
namespace ValueTypeTest {
long l = 4564;
Reference data types
namespace ValueTypeTest {
class GeeksforGeeks {
// Main Function
// declaring string
string a = "Geeks";
//append in a
a+="for";
a = a+"Geeks";
Console.WriteLine(a);
object obj;
obj = 20;
Console.WriteLine(obj);
// using GetType()
Console.WriteLine(obj.GetType());
}
Purpose of nullable
• For example, in nullable of integer type you can store values from -
2147483648 to 2147483647, or null value.
• Nullable<data_type> variable_name = null;
int j = null;
// Valid declaration
Nullable<int> j = null;
// Valid declaration
int? j = null;
Purpose of nullable
• You will get the default value if it is null. The default value for null
will be zero.
Purpose of nullable
// C# program to illustrate Nullable Types // using Nullable type syntax
using System; // to define non-nullable
class Geeks { int? n2 = 47;
// Main Method
// using the method
static void Main(string[] args)
Console.WriteLine(n2.GetValueOrDefault());
{
// value of null is 0
Console.WriteLine(n1.GetValueOrDefault());
Purpose of nullable
• With the help of nullable type you can assign a null value to a variable
without creating nullable type based on the reference type.
using System;
class GFG {
// Main Method
// a is nullable type
int ? a = null;
int ? b = 2345;
// anything on console
Console.WriteLine(a);
Console.WriteLine(b);
}
Purpose of nullable
• If the object assigned with a value, then it will return “True” and if the
object is assigned to null, then it will return “False”.
• If the object is not assigned with any value then it will give compile-
time error.
Purpose of nullable
// C# program to illustrate the
// use of Nullable<L>.Hasvalue
using System;
class GFG {
// Main Method
// a is nullable type
Nullable<int> a = null;
Console.WriteLine(a.HasValue);
// b is nullable type
Nullable<int> b = 7;
Console.WriteLine(b.HasValue);
}
Purpose of nullable
using System;
class GFG {
// Main Method
// a is nullable type
int ? a = null;
// it means if a is null
// then assign 3 to b
int b = a ?? 3;
// It will print 3
Console.WriteLine(b);
}
Purpose of nullable
• Given the following variables, use the The Null Coalescing Operator
(??)
• How does this operator differ from the ? Operator for true and false