0% found this document useful (0 votes)
8 views

Learn C#_ Learn C#_ Numbers and Operators Cheatsheet _ Codecademy

The document is a cheatsheet for learning C# focusing on numbers and operators. It covers essential topics such as user input, comments, arithmetic operators, string interpolation, built-in math methods, and operator shortcuts. Each section includes code examples to illustrate the concepts discussed.

Uploaded by

Michael Okocha
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)
8 views

Learn C#_ Learn C#_ Numbers and Operators Cheatsheet _ Codecademy

The document is a cheatsheet for learning C# focusing on numbers and operators. It covers essential topics such as user input, comments, arithmetic operators, string interpolation, built-in math methods, and operator shortcuts. Each section includes code examples to illustrate the concepts discussed.

Uploaded by

Michael Okocha
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/ 3

10/3/24, 9:44 PM Learn C#: Learn C#: Numbers and Operators Cheatsheet | Codecademy

Cheatsheets / Learn C#

Learn C#: Numbers and Operators

Console.ReadLine()

The Console.ReadLine() method is used to get user Console.WriteLine("Enter your name: ");
input. The user input can be stored in a variable. This
method can also be used to prompt the user to press
string name = Console.ReadLine();
enter on the keyboard.

Comments

Comments are bits of text that are not executed. These // This is a single line comment
lines can be used to leave notes and increase the
readability of the program.
Single line comments are created with two /* This is a multi-line comment
forward slashes // . and continues until the end
Multi-line comments start with /* and end
of comment symbol is reached */
with */ . They are useful for commenting out
large blocks of code.

Console.WriteLine()

The Console.WriteLine() method is used to print Console.WriteLine("Hello, world!");


text to the console. It can also be used to print other
data types and values stored in variables.
// Prints: Hello, world!

https://www.codecademy.com/learn/learn-c-sharp/modules/learn-c-numbers-and-operators/cheatsheet 1/3
10/3/24, 9:44 PM Learn C#: Learn C#: Numbers and Operators Cheatsheet | Codecademy

Arithmetic Operators

Arithmetic operators are used to perform basic int result;


mathematical operations on numerical values:
+ addition operator
- subtraction operator result = 10 + 5; // 15
* multiplication operator
/ division operator result = 10 - 5; // 5
% modulo operator (returns the remainder)

result = 10 * 5; // 50

result = 10 / 5; // 2

result = 10 % 5; // 0

String Interpolation in C#

String interpolation provides a more readable and int id = 100


convenient syntax to create formatted strings. It allows
us to insert variable values and expressions in the
middle of a string so that we don’t have to worry about // We can use an expression with a string
punctuation or spaces. interpolation.
string multipliedNumber = $"The
multiplied ID is {id * 10}.";

Console.WriteLine(multipliedNumber);
// This code would output "The multiplied
ID is 1000."

Built-in Math Methods

In C#, Math provides many built-in methods for double x = -80;


performing advanced mathematical operations. Some
common methods include:
Math.Abs() — calculates the absolute value of double absValue = Math.Abs(x); // 80
a given number double sqRoot = Math.Sqrt(absValue); //
Math.Sqrt() — calculates the of a given
8.94427190999916
number
Math.Floor() — rounds the given number double floored = Math.Floor(sqRoot); // 8
down to the nearest integer double smaller = Math.Min(x, floored); //
Math.Min() — takes 2 values of the same type -80
and returns the value that is less

https://www.codecademy.com/learn/learn-c-sharp/modules/learn-c-numbers-and-operators/cheatsheet 2/3
10/3/24, 9:44 PM Learn C#: Learn C#: Numbers and Operators Cheatsheet | Codecademy

Operator Shortcuts

C# offers several shortcuts for condensing simple int a = 0;


operations.
The addition ( + ) and subtraction ( - ) operators can be
doubled to form the increment ( ++ ) and decrement a++; // a = 1
( -- ) operators. ++ increases a variable’s value by 1, a += 1; // a = 2
and -- lowers it by 1.
a *= 7; // a = 14
+ , - , * , / , and % can all be combined with the
a--; // a = 13
equals sign ( = ) to form compound assignment
operators, such as += , the compound addition a %= 7; // a = 6
operator. These operators take a variable to the left
and a value to the right, perform the specified
arithmetic operation, and assign the result back into
the variable.

Print Share

https://www.codecademy.com/learn/learn-c-sharp/modules/learn-c-numbers-and-operators/cheatsheet 3/3

You might also like