Code
Let’s learn how to code from scratch.
Prerequisites
- Visual studio community edition (2019 - recommended)
Create first project
Let’s create our first project - console application. You can name it as you wish.
Your newly created project has following structure
Where Programm.cs
is file with Program
class and static void Main(string[] args)
method, which is entry point of your program.
Exercises
Printing messages
-
Print following message:
Hello world, my name is <YourName>
go to solution -
Write a program to print a big ‘C’ using hash (
#
)
######
## ##
#
#
#
#
#
## ##
######
0
00
000
0000
00000
- Print big
W
* ** *
* * * *
* * * *
* * * *
* *
Arithmetic exercies
Following line of code allows read line from console input stream
var line = Console::ReadLine();
For example, program sums two numbers and output result
class Program
{
static void Main(string[] args)
{
string aInput = Console.ReadLine();
string bInput = Console.ReadLine();
int a = int.Parse(aInput);
int b = int.Parse(bInput);
int sum = a + b;
Console.WriteLine(sum);
}
}
Note
Console.ReadLine()
returns string. String is data type which represents text. However, we wait for number as input data. So we need to get number from input string. We can use method int.Parse
of type int, which returns Integer (int) data type. Integer represents number.
Why we need data types
Every time we want to create variable in our source code, firstly we must define data type of variable. One of the reason is that different types behave differently. For example,
int a = 5;
int b = 10;
int sum1 = a + b; // will be 15
string x = "5";
string y = "10"
string sum2 = x + y; // will be "510"
-
Compute \((a + 4b)(a - 3b) + a^2\), where
a
andb
should be entered by user. Whena = 10, b = 15
, result is-2350
Note. Use Math.Pow method for getting a number raised to specified power
go to solution -
$$ |x| + x^7 $$
where
x
is entered by user go to solution -
$$ \displaystyle\frac{|x-5|-\sin{x}}{3}+\sqrt{x^2+2014}\cos{2x-3} $$
-
$$ e^{x-2}+|\sin(x)|-x^{4}\cdot\cos{\frac{1}{x}} $$
-
User enters 2 numbers, compute sum and production of these numbers
-
User enters 3 numbers. Double the first number, reduce second number by 3, square the third number and then the sum of the new three numbers.
-
User enters 3 numbers. Find average if these numbers and the difference between the doubled sum of first and third numbers and the tripple second number.
-
User enters side of square. Find perimeter and area of the square
- User enters price of one kilo of candies and 1kg of cookies. Find price of 300 grams candies and 400 grams cookies. Find price of 2kg cookies and 1.8kg of candies.
Solutions
Printing messages solutions
static void Main(string[] args)
{
Console.WriteLine("Hello world, my name is NightBaker");
}
Console.WriteLine(" ######\n" +
" ## ##\n" +
"#\n" +
"#\n" +
"#\n" +
"#\n" +
"#\n" +
" ## ##\n" +
" ######");
NOTE \n
- means new line
Console.WriteLine("0\n00\n000\n0000\n00000")
NOTE \n
- means new line
Console.WriteLine("* ** *");
Console.WriteLine(" * * * *");
Console.WriteLine(" * * * *");
Console.WriteLine(" * * * *");
Console.WriteLine(" * * ");
Arithmetic exercies solutions
1.
Console.WriteLine("Write 1st number");
string aInput = Console.ReadLine();
Console.WriteLine("Write 2nd number");
string bInput = Console.ReadLine();
int a = int.Parse(aInput);
int b = int.Parse(bInput);
double result = (a + (4 * b)) * (a-(3 * b)) - Math.Pow(a,2) ;
Console.WriteLine("(a+4b)(a−3b)+a2 = "+ result);
2.
Console.WriteLine("Write number");
string aInput = Console.ReadLine();
int a = int.Parse(aInput);
double result = Math.Abs(a) + Math.Pow(a, 7);
Console.WriteLine("|x|+x^7 = " + result);
3.
Console.WriteLine("Write number");
string aInput = Console.ReadLine();
int a = int.Parse(aInput);
double result = (Math.Abs(a - 5) - Math.Sin(a))/3 + Math.Sqrt(Math.Pow(a, 2) + 2014) * Math.Cos(2 * a) - 3;
Console.WriteLine("Answer = " + result);
4.
Console.WriteLine("Write number");
string aInput = Console.ReadLine();
int a = int.Parse(aInput);
double result = Math.Exp(a - 2) + Math.Abs(Math.Sin(a)) - Math.Pow(a, 4) * Math.Cos(1 / a);
Console.WriteLine("Answer = " + result);
5.
Console.WriteLine("Write 1st number ");
string aInput = Console.ReadLine();
Console.WriteLine("Write 2nd number ");
string bInput = Console.ReadLine();
int a = int.Parse(aInput);
int b = int.Parse(bInput);
int sum = a + b;
int prod = a * b;
Console.WriteLine(a + " + "+ b + " = "+ sum);
Console.WriteLine(a + " * " + b + " = " + prod);
Console.WriteLine("Write 1st number ");
string aInput = Console.ReadLine();
Console.WriteLine("Write 2nd number ");
string bInput = Console.ReadLine();
Console.WriteLine("Write 3rd number ");
string cInput = Console.ReadLine();
double a = double.Parse(aInput);
double b = double.Parse(bInput);
double c = double.Parse(cInput);
double result = a * 2 + b / 3 + Math.Pow(c, 2);
Console.WriteLine(result);