C#, lesson 3
Every exercise on this site gives your program text on standard input (stdin) and checks what
you print. In C# you read one line at a time with Console.ReadLine().
ReadLine returns the next line without its line break. When there is no more input it returns
null, which is why its type is string? (a string that might be missing). The ?? operator
says "if the left side is null, use the right side instead", so name is always a real string.
The text "42" is not the number 42. Convert it with the method of the type you want:
2 exercises to try what you just read.
Parseint.Parse ignores spaces around the number, but it throws an error if the text is not a number
at all. You will learn to handle that gracefully in the lesson on errors.
When several values share a line, split it first:
RemoveEmptyEntries skips the empty pieces you would otherwise get from double spaces.
| Operator | Meaning | int example |
|---|---|---|
+ | add | 7 + 2 is 9 |
- | subtract | 7 - 2 is 5 |
* | multiply | 7 * 2 is 14 |
/ | divide | 7 / 2 is 3 |
% | remainder | 7 % 2 is 1 |
When both sides of / are integers, C# does integer division and drops the fraction
(rounding toward zero). If either side is a double or decimal, you get the fractional
result: 7 / 2.0 is 3.5. To divide two int variables exactly, cast one of them first:
(double)a / b.
/ and % work as a pair: 17 sweets shared by 5 children is 17 / 5 = 3 each with
17 % 5 = 2 left over. Dividing an integer by zero throws a DivideByZeroException.
The usual precedence applies (*, /, % before +, -); add parentheses when in doubt.
Compound assignment updates a variable in place: score += 10;, lives--;.
Inside an interpolated string, a colon after the value adds a format specifier:
F2 rounds for display only; the variable keeps its full value. For rounding a value you keep
using, call Math.Round(x, 2).
A group shares a restaurant bill. The input has two lines: the total in cents (a whole number), then the number of people. Everyone pays the same whole number of cents, and whatever cannot be split evenly is left over.
Print two lines: Each: <cents> and Left over: <cents>.
Sample input:
Sample output:
Read one temperature in degrees Celsius (it may have a decimal point) and print it in degrees
Fahrenheit with exactly one decimal place. The formula is F = C × 9 / 5 + 32.
Careful: 9 / 5 on two integers is 1, not 1.8.
Sample input:
Sample output:
using System;
string name = Console.ReadLine() ?? "";
Console.WriteLine($"Hi, {name}!");int age = int.Parse(Console.ReadLine() ?? "0");
double height = double.Parse(Console.ReadLine() ?? "0");
decimal price = decimal.Parse(Console.ReadLine() ?? "0");string[] parts = (Console.ReadLine() ?? "").Split(' ', StringSplitOptions.RemoveEmptyEntries);
int a = int.Parse(parts[0]); // "3 4" -> 3
int b = int.Parse(parts[1]); // -> 4double f = 98.6;
Console.WriteLine($"{f:F1}"); // 98.6 (fixed point, 1 decimal)
Console.WriteLine($"{3.14159:F2}");// 3.14
Console.WriteLine($"{1234567:N0}");// 1,234,56710000
3Each: 3333
Left over: 13798.6