CSharp_Tutorials CCC##
CSharp_Tutorials CCC##
C#
Example 1: Example 2:
output: Console.WriteLine(a); {
Console.WriteLine(i);
while (condition)
{
// Code to execute while the condition is true
}
1. a) Write a program to display Numbers 1 - 100 using a for
loop
Using System;
Class Program
{
Static Void Main()
{
// program to display Numbers 1 - 100 using a for loop
for ( int number = 1; number <=100; number++)
{
Console.WriteLine(number);
}
}
}
b) Write a program to display Numbers 1 - 100 using a while
loop
Using System;
class Program
{
Static void Main()
{
//program to display Numbers 1 - 100 using a while loop
Int number = 1;
while (number <= 100)
{
Console.WriteLine(number)
number++
}
}
}
}
4. Write a program to write the following data into a TextFile
called marks.txt
ABC23-001 | Alice | Johnson | Female | 50 | 85 | 75
using System;
using.System.IO;
Class Program
{
Static void Main()
{
string data = “ABC23-001 | Alice | Johnson | Female | 50 | 85 | 75”;
string path = “marks.txt”;
try
{
File.WriteAllText(path, data);
Console.WriteLine(“Data has been written to marks.txt successfully”);
}
Catch (Exception ex)
{
Console.WriteLine(“ An error has occurred: + ex.Message);
}
}
}
5. Write a program to produce the following pattern:
1 = 1
1 2 = 3
1 2 3 = 6
1 2 3 4 = 10
1 2 3 4 5 = 15
1 2 3 4 5 6 = 21
Using System;
Class Program
{
int sum;
sum = 0;
Console.Write(j + “ “);
sum+= j;
Console.Write(“=” + sum);
Console.WriteLine();
Question 2
a) The following information has been mistakenly captured into wrong
variables.
String country = "[email protected]";
Using System;
Class Program
{
Static void Main()
{
// initial incorrect variable
String country = "[email protected]";
String cellno = "Botswana";
String email = "74740404";
//temp correct variables
string tempCountry = “Botswana”;
string tempCellno = "74740404";
string tempEmail = "[email protected]";
//assign correct values
country = tempCountry;
cellno = tempCellno;
email = tempEmail;
// Output correct values
Console.WriteLine(“Country: ” + country);
Console.WriteLine(“Cell Number: + cellno);
Console.WriteLine(“Email: + email);
}
}
[5
Marks]
b) Write down the value stored in the variable result in each of the
following;
i. bool result; no value [1 Marks]
ii. var result = (100 == (Math.Pow((3+7),2))); TRUE [1
Marks]
iii. var result = ((3 + 9 / 3) > 7) && ((45 / 9) < 6);
= FALSE (0) && TRUE (1)
= FALSE [1
Marks]
iv. bool result = Convert.ToInt32("1" + "1") == 11;
= TRUE
[1 Marks]
v. bool result = "A" == "a";
= false [1
Marks]
RangeFr
om RangeTo Grade
0 49 FAIL
50 59 PASS
60 69 CREDIT
70 79 MERIT
80 100 DISTINCTION
The following form is proposed for the program, and the control
names are shown on the right.
i. Write C# code for the Calculate Button to accept user input
and calculate student grade, button click event handler is
not necessary. [10 Marks]
using System;
class Program;
{
Static void Main()
{
ii. Given that a student called Melinda Jones whose marks for
Chemistry are; Test1 = 83, Test2 = 75, Test3 = 79. Write
down the output from your program for this student.
[5 Marks]
Question 3
a) The iteration control structure is implemented through looping in
programs. There are two main types of looping constructs, those that
tests the condition at the beginning and those that tests the condition
at the end.
i. Define the term Loop as used in programming.
A loop is a control structure used for repetitive execution of block
code when specific condition is met.
[2 Marks]
ii. Identify any one (1) looping construct that tests the condition at the
beginning.
While loop [1Mark]
iii. State when the looping construct in ii above is suitable to be used.
The while loop is suitable for when you want to test a repetitive of a
block code condition that runs for an unknow number of times.
[2 Marks]
iv. Write a program code to implement the looping construct in ii above
to display numbers from 1 to 100.
[5 Marks]
v. Name any one (1) looping construct that tests the condition at the
end. [1 Mark]
Do while loop
vi. State when the looping construct in v above is suitable to be used.
when you need to execute the block of code at least once regardless
of whether the condition is true or false at the beginning.
[2 Marks]
vii. Write a program code to implement the looping construct in ii above
to display numbers from 1 to 100.
[5 Marks]
viii. What name is given to a loop embedded in another loop?
Nested loop [2 Marks]
Dry run the program below and determine the values of; a, b, c, d,
and e. [5 Marks]
private void button1_Click(object sender, EventArgs e) {
int sum = 0, n = 0;
double avg = 0;
for (int k = 10; k >= 1; k--) {
if(k % 3 == 0) {
sum += k; SUM = K VS SUM
+= K
n += 1;
avg = sum / n;
Console.WriteLine(k);
}
}
Console.WriteLine("Sum is : {0}", sum);
//displaySum
Console.WriteLine("Average is : {0}", avg);
//display avg
}
k n sum Avg
10 0 0 0
9 A=1 9 9
8 1 9 9
7 1 9 9
6 2 15 7.5
5 2 15 7.5
4 2 15 7.5
3 3 18 6
2 3 18 6
1 C =3 D=18 E=6
Question 4
a) You have been hired by Molapo Pizza to develop a program to
calculate ticket prices for their shows. The ticket prices vary based
on age and there is a discount given for customers who purchase
tickets online.
AgeFro
Online
m AgeTo AgeGroup TicketPrice Discount
0 5 Infant 0 0
6 12 Child 50 10%
ix.
x.
Use the information given in the table above to write down the program
which should display the customer's name, age group, ticket price,
discount, and actual price. [10 Marks]
using System;
class Program
{
static void Main()
{
CalculateTicketPrice("John Doe", 25, true);
}
Question 5