1. How to write on screen in C#.
Console.WriteLine("I have been studying C# for 4 weeks!");
This will appear on screen, and will disappear immediately. So, We are writing another statement.
Console.WriteLine("I have benn studying C# for 4 weeks!"); Console.ReadLine();
Now it will remain on screen, unless we press Enter key.
Here we see that we can write letters, numbers and special characters (#) on screen.
We can add two strings, called string concatenation. For example, we are adding first name and second name to get full name.
string a = "John "; string b = "Smith"; string c = a + b; Console.WriteLine(c); Console.ReadLine();
3. Adding two numbers:
int a=10; int b=12; int c=a+b; Console.WriteLine(c); Console.ReadLine();
Here, we use integers as variables, because we want to add them (arithmetical operation).
Integers does NOT take fractional (decimal) values. If we want to perform arithmetical operation of fractional values; we can take double as variables.
double a = 3.4; double b = 5.2; double c = a + b; Console.WriteLine(c); Console.ReadLine();
4. How C# take input:
The program will take input from user, and will display it on screen
string name = Console.ReadLine(); Console.WriteLine(name); Console.ReadLine();
Integer as input:
Input is always in string. So for integer, we need to convert it first.
int number = Convert.ToInt16(Console.ReadLine()); Console.WriteLine(number); Console.ReadLine();
Remember: As the input is always in string, so for integers as input we need to convert it first.
5. Take two inputs (integers) from user and add them.
int number1 = Convert.ToInt16(Console.ReadLine()); int number2 = Convert.ToInt16(Console.ReadLine()); int number3 = number1 + number2; Console.WriteLine(number3); Console.ReadLine();
6. Take two inputs (string) from user, and add them.
string firstname = Console.ReadLine(); string lastname = Console.ReadLine(); string fullname = firstname + lastname; Console.WriteLine(fullname); Console.ReadLine();
As, input is always in string, so we did not need to convert it.
Conditional Statements in C#:
The if Statement:
Construction:
if (condition) {statement}
For example,
int a = Convert.ToInt16(Console.ReadLine()); if (a > 10) {Console.WriteLine("The number is greater than 10"); Console.ReadLine();
The if / else Statement:
Construction:
if (condition) {statement} else {statement}
For example,
int a = Convert.ToInt16(Console.ReadLine()); if (a > 10) {Console.WriteLine("The number is greater than 10");} else { Console.WriteLine("The number is 10 or less than 10");} Console.ReadLine();
The if / else if / else Statement- (also called nested if)
Construction:
if (condition) {statement} else if (condition) {statement} else {statement}
For example,
int a = Convert.ToInt16(Console.ReadLine()); if (a > 10) {Console.WriteLine("The number is greater than 10");} else if (a == 10) {Console.WriteLine("The number is 10");} else { Console.WriteLine("The number is less than 10");} Console.ReadLine();
NOTE: We write = two times.
The switch Statement:
Construction:
switch (integer a) { case 1: statement break; case 2: statement break; default: statement break; }
NOTE: The default in switch statement is equivalent to else in if statement.
For example,
int week = Convert.ToInt16(Console.ReadLine()); switch (week) { case 1: Console.WriteLine("Monday"); break; Console.WriteLine("Tuesday"); break; case 3: Console.WriteLine("Wednesday"); break; case 4: Console.WriteLine("Thursday"); break; case 5: Console.WriteLine("Friday"); break; case 6: Console.WriteLine("Saturday"); break; case 7: Console.WriteLine("Sunday"); break; default: Console.WriteLine("NOT KNOWN"); break; } Console.ReadLine();
The for loop in C#
Construction for (initial point; ending point; increament) { Statement(s) }
For example, the following program will write counting from 1 to 20.
for (int i = 1; i < 21; i++) { Console.WriteLine(i); } Console.ReadLine();
Q. Write table of 2 using for loop in C#.
for (int i = 1; i < 11; i++) { int tab = 2 * i; Console.WriteLine(tab); } Console.ReadLine();
Q. Write a program that print even numbers from 1 to 100.
for (int i = 1; i < 101; i++) { if (i % 2 == 0) { Console.WriteLine(i); } } Console.ReadLine();
Q. Write a program that take input from user, and write table of that number.
Console.WriteLine("Enter a number:"); int num = Convert.ToInt16(Console.ReadLine()); for (int i = 1; i < 11; i++) { int tab = i * num; Console.WriteLine(tab); } Console.ReadLine();
Q. Write a program in C sharp, to find the factorial of 5.
int fact = 1; for (int i = 5; i > 0; i--) { fact=fact*i; } Console.WriteLine(fact); Console.ReadLine();
Q. Write a program that take input from user, and find factorial of that number.
Console.WriteLine("Enter a number:"); int num = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Its factorial is:"); int fact = 1; for (int i = num; i > 0; i--) { fact=fact*i; } Console.WriteLine(fact); Console.ReadLine();
The while Loop
Construction:
while (condition) { statement(s) }
Q. Write a program in C# using while loop, that take a number from user and return cube of that number. And the program ends when the input is 11.
int num = Convert.ToInt16(Console.ReadLine()); int cube = num * num * num; while (num != 11) { Console.WriteLine(cube); break; } Console.ReadLine();
Q. Write a program that starts from 0, increase 1 by 1, and end before 10 using while loop in C Sharp.
int number = 0; while (number < 10) { Console.WriteLine(number); number = number + 1; } Console.ReadLine();
The do – while loop
Construction
do { statement(s) } while { statement(s) }
Q. Write a program in C Sharp using do – while loop, that take a number, and increase it 2 by 2, and ends before 30.
int num = Convert.ToInt16(Console.ReadLine()); do { Console.WriteLine(num); num = num + 2; } while (num < 30); Console.ReadLine();
Array in C#
Construction
variable type [] variable name = new variable type [length] Array of type integer with constant values int[] myarray = new int[3] { 2, 5, 9 }; Console.WriteLine(myarray[0]); Console.ReadLine();
NOTE:index 0 inside Console.WriteLine statement represents index of array, that is 2.
In the above myarray; index 0 = 2, index 1 = 5, index 2 = 9.
If we replace 0 by 1, the program will show 5, and for 2, the program will show 9.
Array of type string with constant values
string[] name = new string [3] { "Bilal", "Sohail", "Afzal" }; Console.WriteLine(name[0]); Console.ReadLine();
Q. Write an array in C# of type integer that take 3 numbers as input (the program must close after taking 3 inputs).
int[] myarray = new int[3]; myarray[0] = Convert.ToInt16(Console.ReadLine()); myarray[1] = Convert.ToInt16(Console.ReadLine()); myarray[2] = Convert.ToInt16(Console.ReadLine()); Console.WriteLine(myarray); Console.ReadLine();
Q. Write an array in C Sharp of type string that take 3 strings (names) as input (the program must ends after taking 3 inputs).
string[] name = new string[3]; name[0] = Console.ReadLine(); name[1] = Console.ReadLine(); name[2] = Console.ReadLine(); Console.WriteLine(name); Console.ReadLine();
Q. Write an array in C# of type integer that take 10 numbers as input (Use for loop for simplicity).
int[] myarray = new int[10]; for (int i = 0; i < 10; i++) { myarray[i] = Convert.ToInt16(Console.ReadLine()); } Console.WriteLine(myarray); Console.ReadLine();
Q. Write a program in C Sharp that take 10 inputs from user, and show their sum.
int[] myarray = new int[10]; for (int i = 0; i < 10; i++) { myarray[i] = Convert.ToInt16(Console.ReadLine()); } int a = 0; for (int j = 0; j < 10; j++) { a = a + myarray[j]; } Console.WriteLine(a); Console.ReadLine();
Q. Write a program in C# that take 10 numbers, and show their average (input could be decimal or fractional value also).
double[] myarray = new double[10]; for (int i = 0; i < 10; i++) { myarray[i] = Convert.ToInt16(Console.ReadLine()); } double a = 0; double b = 0; for (int j = 0; j < 10; j++) { a = a + myarray[j]; b = a / 10; } Console.WriteLine(b); Console.ReadLine();
Introduction to Windows Forms Application
Q. Take two inputs from user (first name and second name) and concatenates them and print it, using windows forms application in C#.
For this, take 2 textboxes and 1 button from toolbox menu.
string firstname = textBox1.Text; string secondname = textBox2.Text; string fullname = firstname + secondname; MessageBox.Show(fullname);
Q. Take 2 inputs from user and add them using windows forms application in C#.
int a = Convert.ToInt16(textBox1.Text); int b = Convert.ToInt16(textBox2.Text); int c = a + b; MessageBox.Show(Convert.ToString(c));
Q. Write a program that take input from user and show whether the number is odd or even, using windows forms application in C Sharp.
For this, take 1 textbox and 1 button.
int number = Convert.ToInt16(textBox1.Text); if (number % 2 == 0) { MessageBox.Show("EVEN"); } else { MessageBox.Show("ODD"); }
Functions in C#
Construction
output type (input type) { return (program); }
Now call the function
output type = function name;
The following example will make the matter clear.
Q. Write a function that take 2 numbers and add them using windows forms application in C#.
Function int add(int a, int b) { return (a + b); } Now, call the function int c = add(Convert.ToInt16(textBox1.Text), Convert.ToInt16(textBox2.Text)); MessageBox.Show(Convert.ToString(c));
Q. Write a function in C Sharp which takes three number as input parameter and return the largest of three.
Function int largest(int a, int b, int c) { if (a > b && a > c) { return a; } else if (b > a && b > c) { return b; } else { return c; } } Call the function int result = largest(Convert.ToInt16(textBox1.Text), Convert.ToInt16(textBox2.Text), Convert.ToInt16(textBox3.Text)); MessageBox.Show(Convert.ToString(result));
Q. Write a program in C# that take Temperature in Fahrenheit, and convert it to Centigrade.
Console.WriteLine("Enter Temperature in Fahrenheit:"); double ftemp = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Equivalent Temperature in Centigrade is:"); double ctemp= (ftemp-32) * 5 / 9; Console.WriteLine(ctemp); Console.ReadLine();
Q. Write a program in C Sharp that take Month and Date, and show number of days from the start of the year to that date.
Console.WriteLine("Enter Month"); Console.WriteLine("Enter Date"); int b = Convert.ToInt16(Console.ReadLine()); int c = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Number of days from the start of the year are:"); int a = 0; int d = 0; int[] month = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; for (int i = 0; i < b - 1; i++) { a = a + month[i]; d = a + c; } Console.WriteLine(d); Console.ReadLine();
Sajjad Miyan! This Really helped, thank you very much!
Love From India