1. How to write on screen in C#.
Here we see that we can write letters, numbers and special characters (#) on screen.
String: String is name of variable, and it could be anything letters, numbers and special charecters. We can use string for numbers also, but when we need any arithmetical operation, we use integers, decimals etc as variables
2. String Concatenation:
We can add two strings, called string concatenation. For example, we are adding first name and second name to get full name.
Integers does NOT take fractional (decimal) values. If we want to perform arithmetical operation of fractional values; we can take double as variables.
The program will take input from user, and will display it on screen
Input is always in string. So for integer, we need to convert it first.
5. Take two inputs (integers) from user and add them.
Conditional Statements in C#:
The if Statement:
Construction:
Construction:
Construction:
The switch Statement:
Construction:
For example,
Construction
Construction:
Construction
Construction
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
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 menue.
For this, take 1 textbox and 1 button.
Construction
Q. Write a function that take 2 numbers and add them using windows forms application 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!");Now it will remain on screen, unless we press Enter key.
Console.ReadLine();
Here we see that we can write letters, numbers and special characters (#) on screen.
String: String is name of variable, and it could be anything letters, numbers and special charecters. We can use string for numbers also, but when we need any arithmetical operation, we use integers, decimals etc as variables
2. String Concatenation:
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 ";3. Adding two numbers:
string b = "Smith";
string c = a + b;
Console.WriteLine(c);
Console.ReadLine();
int a=10;Here, we use integers as variables, because we want to add them (arithmetical operation).
int b=12;
int c=a+b;
Console.WriteLine(c);
Console.ReadLine();
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;4. How C# take input:
double b = 5.2;
double c = a + b;
Console.WriteLine(c);
Console.ReadLine();
The program will take input from user, and will display it on screen
string name = Console.ReadLine();Integer as input:
Console.WriteLine(name);
Console.ReadLine();
Input is always in string. So for integer, we need to convert it first.
int number = Convert.ToInt16(Console.ReadLine());Remember: As the input is always in string, so for integers as input we need to convert it first.
Console.WriteLine(number);
Console.ReadLine();
5. Take two inputs (integers) from user and add them.
int number1 = Convert.ToInt16(Console.ReadLine());6. Take two inputs (string) from user, and add them.
int number2 = Convert.ToInt16(Console.ReadLine());
int number3 = number1 + number2;
Console.WriteLine(number3);
Console.ReadLine();
string firstname = Console.ReadLine();As, input is always in string, so we did not need to convert it.
string lastname = Console.ReadLine();
string fullname = firstname + lastname;
Console.WriteLine(fullname);
Console.ReadLine();
Conditional Statements in C#:
- The if, if / else, if / else if / else Statement
- The switch Statement
The if Statement:
Construction:
if (condition)For example,
{statement}
int a = Convert.ToInt16(Console.ReadLine());The if / else Statement:
if (a > 10)
{Console.WriteLine("The number is greater than 10");
Console.ReadLine();
Construction:
if (condition)For example,
{statement}
else
{statement}
int a = Convert.ToInt16(Console.ReadLine());The if / else if / else Statement- (also called nested if)
if (a > 10)
{Console.WriteLine("The number is greater than 10");}
else
{ Console.WriteLine("The number is 10 or less than 10");}
Console.ReadLine();
Construction:
if (condition)For example,
{statement}
else if (condition)
{statement}
else
{statement}
int a = Convert.ToInt16(Console.ReadLine());NOTE: We write = two times.
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();
The switch Statement:
Construction:
switch (integer a)NOTE: The default in switch statement is equaivalent to else in if statement.
{
case 1:
statement
break;
case 2:
statement
break;
default:
statement
break;
}
For example,
int week = Convert.ToInt16(Console.ReadLine());The for loop in C#
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();
Construction
for (initial point; ending point; increament)For example, the following program will write counting from 1 to 20.
{
Statement(s)
}
for (int i = 1; i < 21; i++)Q. Write table of 2 using for loop in C#.
{
Console.WriteLine(i);
}
Console.ReadLine();
for (int i = 1; i < 11; i++)Q. Write a program that print even numbers from 1 to 100.
{
int tab = 2 * i;
Console.WriteLine(tab);
}
Console.ReadLine();
for (int i = 1; i < 101; i++)Q. Write a program that take input from user, and write table of that number.
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
}
Console.ReadLine();
Console.WriteLine("Enter a number:");Q. Write a program in C sharp, to find the factorial of 5.
int num = Convert.ToInt16(Console.ReadLine());
for (int i = 1; i < 11; i++)
{
int tab = i * num;
Console.WriteLine(tab);
}
Console.ReadLine();
int fact = 1;Q. Write a program that take input from user, and find factorial of that number.
for (int i = 5; i > 0; i--)
{
fact=fact*i;
}
Console.WriteLine(fact);
Console.ReadLine();
Console.WriteLine("Enter a number:");The while Loop
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();
Construction:
while (condition)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.
{
statement(s)
}
int num = Convert.ToInt16(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 cube = num * num * num;
while (num != 11)
{
Console.WriteLine(cube);
break;
}
Console.ReadLine();
int number = 0;The do - while loop
while (number < 10)
{
Console.WriteLine(number);
number = number + 1;
}
Console.ReadLine();
Construction
doQ. Write a program in C Sharp using do - while loop, that take a number, and increase it 2 by 2, and ends before 30.
{
statement(s)
}
while
{
statement(s)
}
int num = Convert.ToInt16(Console.ReadLine());Array in C#
do
{
Console.WriteLine(num);
num = num + 2;
}
while (num < 30);
Console.ReadLine();
Construction
variable type [] variable name = new variable type [length]Array of type integer with constant values
int[] myarray = new int[3] { 2, 5, 9 };NOTE:index 0 inside Console.WriteLine statement represents index of array, that is 2.
Console.WriteLine(myarray[0]);
Console.ReadLine();
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" };Q. Write an array in C# of type integer that take 3 numbers as input (the program must close after taking 3 inputs).
Console.WriteLine(name[0]);
Console.ReadLine();
int[] myarray = new int[3];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).
myarray[0] = Convert.ToInt16(Console.ReadLine());
myarray[1] = Convert.ToInt16(Console.ReadLine());
myarray[2] = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(myarray);
Console.ReadLine();
string[] name = new string[3];Q. Write an array in C# of type integer that take 10 numbers as input (Use for loop for simplicity).
name[0] = Console.ReadLine();
name[1] = Console.ReadLine();
name[2] = Console.ReadLine();
Console.WriteLine(name);
Console.ReadLine();
int[] myarray = new int[10];Q. Write a program in C Sharp that take 10 inputs from user, and show their sum.
for (int i = 0; i < 10; i++)
{
myarray[i] = Convert.ToInt16(Console.ReadLine());
}
Console.WriteLine(myarray);
Console.ReadLine();
int[] myarray = new int[10];Q. Write a program in C# that take 10 numbers, and show their average (input could be decimal or fractional value also).
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();
double[] myarray = new double[10];Introduction to Windows Forms Application
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();
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 menue.
string firstname = textBox1.Text;Q. Take 2 inputs from user and add them using windows forms application in C#.
string secondname = textBox2.Text;
string fullname = firstname + secondname;
MessageBox.Show(fullname);
int a = Convert.ToInt16(textBox1.Text);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.
int b = Convert.ToInt16(textBox2.Text);
int c = a + b;
MessageBox.Show(Convert.ToString(c));
For this, take 1 textbox and 1 button.
int number = Convert.ToInt16(textBox1.Text);Functions in C#
if (number % 2 == 0)
{
MessageBox.Show("EVEN");
}
else
{
MessageBox.Show("ODD");
}
Construction
output type (input type)The following example will make the matter clear.
{
return (program);
}
Now call the function
output type = function name;
Q. Write a function that take 2 numbers and add them using windows forms application in C#.
FunctionQ. Write a function in C Sharp which takes three number as input parameter and return the largest of three.
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));
FunctionQ. Write a program in C# that take Temperature in Fahrenheight, and convert it to Centigrate.
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));
Console.WriteLine("Enter Temperature in Fahrenheight:");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.
double ftemp = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Equivalent Temperature in Centigrate is:");
double ctemp= (ftemp-32) * 5 / 9;
Console.WriteLine(ctemp);
Console.ReadLine();
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();
No comments:
Post a Comment