Skip to content

Commit cf9f317

Browse files
authored
Add files via upload
Conversion and TypeCasting and choosing the right data type
1 parent 1633dff commit cf9f317

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

RightDataType/Program.cs

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
using System;
2+
using System.Globalization;
3+
CultureInfo.CurrentCulture = new CultureInfo("en-US");
4+
5+
6+
Console.WriteLine("Signed integral types:");
7+
8+
Console.WriteLine($"sbyte : {sbyte.MinValue} to {sbyte.MaxValue}");
9+
Console.WriteLine($"short : {short.MinValue} to {short.MaxValue}");
10+
Console.WriteLine($"int : {int.MinValue} to {int.MaxValue}");
11+
Console.WriteLine($"long : {long.MinValue} to {long.MaxValue}");
12+
13+
Console.WriteLine("");
14+
Console.WriteLine("Unsigned integral types:");
15+
16+
Console.WriteLine($"byte : {byte.MinValue} to {byte.MaxValue}");
17+
Console.WriteLine($"ushort : {ushort.MinValue} to {ushort.MaxValue}");
18+
Console.WriteLine($"uint : {uint.MinValue} to {uint.MaxValue}");
19+
Console.WriteLine($"ulong : {ulong.MinValue} to {ulong.MaxValue}");
20+
21+
int[] data = new int[3];
22+
23+
int val_A = 2;
24+
int val_B = val_A;
25+
val_B = 5;
26+
27+
Console.WriteLine("--Value Types--");
28+
Console.WriteLine($"val_A: {val_A}");
29+
Console.WriteLine($"val_B: {val_B}");
30+
31+
int[] ref_A= new int[1];
32+
ref_A[0] = 2;
33+
int[] ref_B = ref_A;
34+
ref_B[0] = 5;
35+
36+
Console.WriteLine("--Reference Types--");
37+
Console.WriteLine($"ref_A[0]: {ref_A[0]}");
38+
Console.WriteLine($"ref_B[0]: {ref_B[0]}");
39+
40+
// int first = 2;
41+
// string second = "4";
42+
// string result = first + second;
43+
// Console.WriteLine(result);
44+
45+
// int myInt = 3;
46+
// Console.WriteLine($"int: {myInt}");
47+
48+
// decimal myDecimal = myInt;
49+
// Console.WriteLine($"decimal: {myDecimal}");
50+
51+
// decimal myDecimal = 3.14m;
52+
// Console.WriteLine($"decimal: {myDecimal}");
53+
54+
// int myInt = (int)myDecimal;
55+
// Console.WriteLine($"int: {myInt}");
56+
57+
// decimal myDecimal = 1.23456789m;
58+
// float myFloat = (float)myDecimal;
59+
60+
// Console.WriteLine($"Decimal: {myDecimal}");
61+
// Console.WriteLine($"Float : {myFloat}");
62+
63+
// int first = 5;
64+
// int second = 7;
65+
// string message = first.ToString() + second.ToString();
66+
// Console.WriteLine(message);
67+
68+
// string first = "5";
69+
// string second = "7";
70+
// int sum = int.Parse(first) + int.Parse(second);
71+
// Console.WriteLine(sum);
72+
73+
// string value1 = "5";
74+
// string value2 = "7";
75+
// int result = Convert.ToInt32(value1) * Convert.ToInt32(value2);
76+
// Console.WriteLine(result);
77+
78+
// int value = (int)1.5m; // casting truncates
79+
// Console.WriteLine(value);
80+
81+
// int value2 = Convert.ToInt32(1.5m); // converting rounds up
82+
// Console.WriteLine(value2);
83+
84+
// string value = "102";
85+
// int result = 0;
86+
// if (int.TryParse(value, out result))
87+
// {
88+
// Console.WriteLine($"Measurement: {result}");
89+
// }
90+
// else
91+
// {
92+
// Console.WriteLine("Unable to report the measurement.");
93+
// }
94+
95+
// if (result > 0)
96+
// Console.WriteLine($"Measurement (w/ offset): {50 + result}");
97+
98+
// string[] values = { "12.3", "45", "ABC", "11", "DEF" };
99+
100+
// decimal total = 0m;
101+
// string message = "";
102+
103+
// foreach (var value in values)
104+
// {
105+
// decimal number; // stores the TryParse "out" value
106+
// if (decimal.TryParse(value, out number))
107+
// {
108+
// total += number;
109+
// } else
110+
// {
111+
// message += value;
112+
// }
113+
// }
114+
115+
// Console.WriteLine($"Message: {message}");
116+
// Console.WriteLine($"Total: {total}");
117+
118+
int value1 = 11;
119+
decimal value2 = 6.2m;
120+
float value3 = 4.3f;
121+
122+
// Your code here to set result1
123+
int result1 = Convert.ToInt32(value1 / value2);
124+
// Hint: You need to round the result to nearest integer (don't just truncate)
125+
Console.WriteLine($"Divide value1 by value2, display the result as an int: {result1}");
126+
127+
// Your code here to set result2
128+
decimal result2 = value2 / (decimal)value3;
129+
Console.WriteLine($"Divide value2 by value3, display the result as a decimal: {result2}");
130+
131+
// Your code here to set result3
132+
float result3 = value3 / value1;
133+
Console.WriteLine($"Divide value3 by value1, display the result as a float: {result3}");

0 commit comments

Comments
 (0)