0% found this document useful (0 votes)
2 views6 pages

C# Programming Basics and Exercises

This document outlines the goals and content for Week 2 of an Object-oriented Programming course, focusing on C#.NET. It covers basic command structures, data types such as Enum and Array, and provides multiple programming exercises for students to practice their skills. The exercises include tasks related to finding numbers in lists, performing calculations, and working with matrices.

Uploaded by

Thanh Tín
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

C# Programming Basics and Exercises

This document outlines the goals and content for Week 2 of an Object-oriented Programming course, focusing on C#.NET. It covers basic command structures, data types such as Enum and Array, and provides multiple programming exercises for students to practice their skills. The exercises include tasks related to finding numbers in lists, performing calculations, and working with matrices.

Uploaded by

Thanh Tín
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object-oriented Programming

Week 2
Dr. Le Van Vinh

Faculty of Information Technology – HCMUTE

I. Goal:

+ Get familiar with basic statements and data structures in C#.NET

+ Write some simple programs

II. Introduction
1. Learn and familiarize yourself with the following command structures:
 If statement
 Switch statement
 While statement
 Do statement
 For statement
 Foreach statement
 Break statement
 Throw and try statement

Example of using Throw and try statement (1)

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Structs_array_enum
{
class Program
{
static void Main(string[] args)
{
double a = 3, b = 0;
double kq=0;
try
{
if (a==0)
throw new Exception("a phai khac 0");
if (b==0)
throw new DivideByZeroException();
kq = a/b;
}
catch (Exception e)
{
[Link]([Link]);
}
finally
{
[Link]("Goodbye!");
}
}
}
}

Example of using Throw and try statement (2)

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Structs_array_enum
{
class Program
{
static void Main(string[] args)
{
double a = 3, b = 0;
double kq=0;
try
{
kq = a/b;
}
catch {

}
finally
{
[Link]("Goodbye!");
}
}
}

2. Learn and familiarize yourself with Enum and Array data type

Example of using Enum

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace Structs_array_enum
{

class Program
{
enum NgayTrongTuan
{
ThuHai=2,
ThuBa=3,
ThuTu=4,
ThuNam=5,
ThuSau=6,
ThuBay=7,
ChuNhat=8
}
static void Main(string[] args)
{

[Link]("Ban chon thu may?");


int Thu = Convert.ToInt32([Link]());

switch (Thu)
{
case (int)[Link]:
[Link]("Ban chon thu hai");
break;
case (int)[Link]:
[Link]("Ban chon thu ba");
break;
case (int)[Link]:
[Link]("Ban chon thu tu");
break;
case (int)[Link]:
[Link]("Ban chon thu nam");
break;
case (int)[Link]:
[Link]("Ban chon thu sau");
break;
case (int)[Link]:
[Link]("Ban chon thu bay");
break;
case (int)[Link]:
[Link]("Ban chon chu nhat");
break;
default:
[Link]("Ban khong chon ngay");
break;
}
}

}
}

Example of using one-dimensional array

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace TestArray
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[10];
for (int i = 0; i < [Link]; i++)
{
a[i] = i * i;
}
for (int i = 0; i < [Link]; i++)
{
[Link]("a[{0}] = {1}", i, a[i]);
}
}
}
}

Example of using two-dimensional array

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace TestArray
{
class Program
{
static void Main(string[] args)
{
//int[,] a = new int[10,10];
int[,] a = new int[,] { {1,2}, {3, 4 }, {5, 6} };

for (int i = 0; i < [Link](0); i++)


{
for (int j = 0; j < [Link](1); j++)
{
[Link](a[i, j] + ", ");
}
[Link]("\n");
}

}
}
}

III. Exercises
1. Write a program to enter a list of integer numbers. Then do these following requests:

a. Find the smallest and largest number in that list

b. Print on screen all negative numbers in that list

c. Print on screen all prime numbers in that list


d. Print on screen all square numbers in that list

2. Write a program to find the greatest common divisor and the least common multiple of
two integer numbers.

3. Write a program to enter 2 integer numbers. Let the user choose what to do with those
numbers (addition, subtraction, multiplication, division, modulo) and calculate the result
of that operation. Must use Enum data type to do this exercise.

4. Write a program to enter the score of 3 subjects of a student: Mathematics, Physical, and
Chemistry. Calculate the average score and then print on screen the rank of that student
(Excellent, Good, Fair, Medium, or Weak).

5. Write a program to enter an array of “n” integer numbers. Find the smallest positive
number of that array.

6. Write a program to enter an array of “n” integer numbers. Calculate the average of
negative numbers of that array.

7. Write a program to calculate the k-combination of a set which has n elements.

8. Write a program to enter a positive integer number. Check if it is a prime number or not.

9. Write a program to enter an array of numbers and a number “x”. Check if “x” is present
in that array or not.

10. Write a program to enter an array of numbers. Find the smallest and greatest number in
that array.

11. Write a program to enter a matrix. Calculate all positive number in that matrix and find
the greatest number in that matrix.

12. Write a program to enter an array of “n” elements. Check if all elements of that array are
positive numbers or not.

13. Write a program to enter a matrix. Find the greatest and smallest number in each row and
column of that matrix.

14. Write a program to enter a two-dimensional array. Find the first negative number of that
array.

You might also like