Take input from users and printing output in console
Lets say, you're going for an interview and the interviewer asks you the following questions.
Q1: What's your name?
You answer, my name is 'Xyz'
So, in C# how to print some text on screen? On console you can do the following.
Use Console.WriteLine();
To print something on screen. Type in your Main() function:
- Console.WriteLine("What is your name");
WriteLine() function takes 1 parameter as string data-type.
Now, to take user's input we use Console.ReadLine() but it must be saved somewhere so we can use it. We must have a reference to it in our memory. We do it like this:
- String name = Console.ReadLine();
This will create a variable 'name' of data-type 'String' and assign it a value returned by 'Console.ReadLine()'
ReadLine() doesn't take any parameter, it fetches input from a source, in our case Console.
Programming Tutorials [Prorials]
Programming Tutorials for Computer Science and Software Engineering students.
Saturday, 15 November 2014
Programming Math tricks, swapping!
Swapping values of two variables without creating a new one!
Consider:
a = 10;
b = 5;
Swap it without making a new variable. Can you?
This is a really challenging task for some people. In my class it was hard for almost all of us but the
answer is really simple.
You simply do:
a = a + b;
b = a - b;
a = a - b;
and the values are swapped! :)
Same can be done with arrays:
array[1] = array[1] + array[2];
array[2] = array[1] - array[2];
array[1] = array[1] - array[2];
Good? Share please!
Tuesday, 23 September 2014
C# - Variables
Variables are those special keywords which holds a specific value assigned to them in our program.
Consider the following math equation:
- f(x) = x² + 2x
In this equation, X is the variable and f(x) is the function. In programming we can do the same and assign any value to variables to use them later. Variables can be created simply like this:
<data type> <variable name> = <value> ;
Example:
int myNumber = 5;
- Makes an int variable "myNumber" and stores 5 number in it.
string name = "Waleed";
- Makes a string variable "name" and stores my name in it.
float weight = 44.5;
- Makes a float variable "weight" and stores my weight in it.
We can also dynamically read input from users and assign it to variables too.
Example:
string name = Console.ReadLine();
Function Console.ReadLine() will read line in console what ever we have entered and store it in name variable which has a strng datatype.
Consider the following math equation:
- f(x) = x² + 2x
In this equation, X is the variable and f(x) is the function. In programming we can do the same and assign any value to variables to use them later. Variables can be created simply like this:
<data type> <variable name> = <value> ;
Example:
int myNumber = 5;
- Makes an int variable "myNumber" and stores 5 number in it.
string name = "Waleed";
- Makes a string variable "name" and stores my name in it.
float weight = 44.5;
- Makes a float variable "weight" and stores my weight in it.
We can also dynamically read input from users and assign it to variables too.
Example:
string name = Console.ReadLine();
Function Console.ReadLine() will read line in console what ever we have entered and store it in name variable which has a strng datatype.
Monday, 22 September 2014
C# - .NET Framework
.NET is a framework developed and maintained by Microsoft. It is a group of libraries, classes and methods that helps in developing desktop applications easily. It runs primarily on Microsoft Windows platform and supports number of programming languages including C#
The .NET Framework is a technology that supports building and running the next generation of applications and XML Web services. The .NET Framework is designed to fulfill the following objectives:
- To provide a consistent object-oriented programming environment whether object code is stored and executed locally, executed locally but Internet-distributed, or executed remotely.
- To provide a code-execution environment that minimizes software deployment and versioning conflicts.
- To provide a code-execution environment that promotes safe execution of code, including code created by an unknown or semi-trusted third party.
- To provide a code-execution environment that eliminates the performance problems of scripted or interpreted environments.
- To make the developer experience consistent across widely varying types of applications, such as Windows-based applications and Web-based applications.
- To build all communication on industry standards to ensure that code based on the .NET Framework can integrate with any other code.
We will be using .NET framework to work in C# that will help us in developing rich desktop applications.
C# - Data Types
Data types:
In general, data type can be simply understood as classification of data into its types or category. In programming languages, data type tells compiler what kind of data it it going to work on.For example:
My name 'XYZ' would be a collection of alphabets only and my age is going to be a numeric value.
In C# there are a bunch of old and new data types available for you to use, few most commonly data types are:
Int, Character, String, Float, Double
Whatever data type we use, it will of-course consume some memory and by memory we mean some space in RAM (Random Access Memory). So using precise data type in our program is good for us. Our goal should be to use less memory and fast program execution.
List of some C# data types and size in bytes with their ranges:
'Long' and 'Int' are two similar data types to hold numeric values without any decimal point. i.e 1, 10, 256, 5145 but using 'Int' would be more efficient since it uses half the size of 'Long'.
In decimal values you may use 'Double' instead of 'float'. Though they both hold decimal values but use 'Double' because it can hold more decimal precission values (i.e digits after decimal) then 'float'. For normal work like, weight, speed of car use float. For scientific value which requires more precision like the value of pi, acceleration, mass of electron, etc, use Double.
So each data type is used according to its requirement. Choose wisely! :)
C# - Introduction
C# pronounced as C Sharp is a multi-paradigm programming language encompassing strong typing, imperative,declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.
It was released in 2000 by Microsoft and currently being developer by Microsoft developers.
C# is intended to be a simple, modern, general-purpose, object-oriented programming language.[7] Its development team is led by Anders Hejlsberg. The most recent version is C# 5.0, which was released on August 15, 2012 (2 years ago).
It was released in 2000 by Microsoft and currently being developer by Microsoft developers.
C# is intended to be a simple, modern, general-purpose, object-oriented programming language.[7] Its development team is led by Anders Hejlsberg. The most recent version is C# 5.0, which was released on August 15, 2012 (2 years ago).
Subscribe to:
Comments (Atom)
