0% found this document useful (0 votes)
36 views14 pages

1618 - Lab01 - Basic Review

The document provides examples of C# programs using loop statements like for loops and while loops. It contains 11 exercises demonstrating how to use loops to calculate sums, averages, factorials, check for prime numbers, display Fibonacci sequences, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views14 pages

1618 - Lab01 - Basic Review

The document provides examples of C# programs using loop statements like for loops and while loops. It contains 11 exercises demonstrating how to use loops to calculate sums, averages, factorials, check for prime numbers, display Fibonacci sequences, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Loop Statements

Exercise 1:
Write a program in C# to read n numbers from keyboard and find their sum and average.
int n, average;
int sum = 0;
Console.WriteLine("Enter the ammount of numbers: ");
n = Convert.ToInt32(Console.ReadLine());

for(int i = 0; i < n; i++)


{
Console.WriteLine("Enter the #{0} number: ", (i+1));
sum = Convert.ToInt32(Console.ReadLine()) + sum;
}
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Average: " + (sum / n));
Console.ReadKey();

Exercise 2:
Write a program in C# to display the multiplication table of a given integer n (n from 2 to
100, ask user re-enter if n is out of this range)
int n;
do
{
Console.WriteLine("Enter number from 2-100 for it's multiplication table: ");
n = Convert.ToInt32(Console.ReadLine());
if (2 <= n && n <= 100)
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("{0} x {1} = {2}", n, i, (n * i));
}
break;
}
else
{
Console.WriteLine("Error ! Please enter value from 2-100");
}
} while (true);
Console.ReadKey();

Exercise 3:
Write a C# program to calculate the factorial of a given number
int n;
int factorial = 1;
Console.WriteLine("Enter number to calculate factorial: ");
n = Convert.ToInt32(Console.ReadLine());
for(int i = 1; i <= n; i++)
{
factorial = factorial * i;
}
Console.WriteLine("{0}! = {1}", n, factorial);
Console.ReadKey();

Exercise 4:
Write a C# program to determine whether a given number is prime number (số nguyên
tố) or not.
Định nghĩa số nguyên tố: Là số số nguyên dương > 1 và chỉ chia hết cho 1 và chính nó.
Ví dụ: 2, 3, 5, 7, 11, …..
int n;
bool IsPrime = true;
Console.WriteLine("Enter a number to see if it's a prime number: ");
n = int.Parse(Console.ReadLine());
for( int i = 2 ; i < n ; i++ )
{
if (n % i == 0)
{
Console.WriteLine("{0} is not a prime number.", n);
IsPrime = false;
break;
}
}
if ( IsPrime == true )
{
Console.WriteLine("{0} is a prime number.", n);
}
Console.ReadKey();

Exercise 5:
Write a program in C# to find the prime numbers within a range of numbers. The
program will ask user input starting and ending numbers of range and display the prime
numbers in this range.
Example:
Input starting number of range: 10
Input ending number of range: 30
The prime number between 10 and 30 are : 11, 13, 17, 19, 23, 29
int n1, n2, i, a;
int count;
Console.WriteLine("Enter 2 number to see if it's a prime number: ");
Console.WriteLine("1st number: ");
n1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("2nd number: ");
n2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Prime numbers from {0} to {1} are: ");

for (a = n1; a <= n2; a++)


{
count = 0;
for (i = 2; i <= a/2 ; i++)
{
if (a % i == 0)
{
count++;
break;
}
}
if (count == 0)
{
Console.Write("{0} ", a);
}
}
Console.ReadKey();

Exercise 6:
Write a program in C# to display the first n terms of Fibonacci series.
Fibonacci series: 1 1 2 3 5 8 13 .....
Công thức truy hồi của dãy fibonacci có dạng: f(n) = f(n-1) + f(n-2)
Với f(1) = 1;  f(2) =1;
int a, b, c, n;
a = 0; b = 1; c = 0;
Console.WriteLine("input numbers of the first n values of the Fibonacci
numbers");
n = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i <= n; i++)
{
c = a + b;
Console.Write("{0} ", b);
a = b;
b = c;
}
Console.ReadKey();

Exercise 7:
Write a program in C# to find the numbers and sum of all integer between a and b (a and
b are entered from keyboard) which are divisible by 7
int a, b;
Console.WriteLine("Enter a and b to see the sum of which number between the
two is divisible by 7: ");
Console.WriteLine("a: ");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("b: ");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Those numbers are: ");
for (int i = a; i <= b; i++)
{
for(int j = a; i <= b; i++)
{
if( (i+j) % 7 == 0)
{
Console.WriteLine("{0} and {1} ", i, j);
}
}
}
Console.ReadLine();

Exercise 8:
Write a C# program to input n positive integer numbers, print their sum and largest
number
int n, sum, max, a;
sum = 0;
max = 0;
Console.WriteLine("Enter ammount of number: ");
n = Convert.ToInt32(Console.ReadLine());
for(int i = 1; i <= n; i++ )
{
Console.WriteLine("Enter number #{0}: ", i);
a = Convert.ToInt32(Console.ReadLine());
sum = sum + a;
if(a <=0 )
{
Console.WriteLine("ERROR ! PLEASE ENTER A POSITIVE NUMBER !");
}else if(a > max)
{
max = a;
}
}
Console.WriteLine("Sum: {0}, Max: {1}", sum, max);
Console.ReadLine();

Exercise 9:
Write a C# program to input an integer number which should be greater than 10 and less
than 100, number check should be there and input number recursively until number value
is not valid. Calculate and display the factorial of that number.
int n;
double fact = 1;
do
{
Console.WriteLine("Input Number within 10 to 100");
n = Convert.ToInt32(Console.ReadLine());
if( n < 10 || n > 100 )
{
Console.WriteLine("Not within 10 to 100, goodbye.");
break;
}
for (int i = 1; i <= n; i++)
{
fact = fact * i;
}
Console.WriteLine("Factorial of that number is: {0}", fact);
} while (true);
Console.ReadKey();

Exercise 10:
Write a C# program to read time in hours, minutes, seconds (hh:mm:ss) make sure input
value should be correct, if input values are wrong read them recursively. Calculate how
many seconds in your time.
int h, m, s;
do
{
Console.WriteLine("Input hour: ");
h = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Input minute: ");
m = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Input second: ");
s = Convert.ToInt32(Console.ReadLine());
if (h < 24 && m < 60 && s < 60)
{
Console.WriteLine("Total seconds in this time is: " + ((h * 60 * 60)
+ (m * 60) + s));
break;
}
else
{
Console.WriteLine("Invalid values, please try again.\n");
}
} while (true);
Console.ReadKey();

Exercise 11:
Write a C# program to display the following menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

- If the users enter 1, the program will ask users input 2 integer number a ,b and then
calculate a + b and finally display the result to screen and come back to the menu.
- Do the same for 2, 3, 4.
- If the users enter 5, close the application
int menu;
float a, b;
do
{
Console.WriteLine("----------MENU----------");
Console.WriteLine("| 1.ADDITION");
Console.WriteLine("| 2.SUBTRACTION");
Console.WriteLine("| 3.MULTIPLICATION");
Console.WriteLine("| 4.DIVISION");
Console.WriteLine("| 5.EXIT");
Console.WriteLine("------------------------");
menu = Convert.ToInt32(Console.ReadLine());
switch (menu)
{
case 1:
Console.WriteLine("Enter a and b for addition: ");
Console.Write("a: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("b: ");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Sum of {0} and {1} is: {2} ", a, b, (a + b));
break;
case 2:
Console.WriteLine("Enter a and b for division: ");
Console.Write("a: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("b: ");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Difference of {0} and {1} is: {2} ", a, b, (a
- b));
break;
case 3:
Console.WriteLine("Enter a and b for multiplication: ");
Console.Write("a: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("b: ");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Product of {0} and {1} is: {2} ", a, b, (a *
b));
break;
case 4:
Console.WriteLine("Enter a and b for division: ");
Console.Write("a: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("b: ");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Quotient of {0} and {1} is: {2} ", a, b, a /
b);
break;
case 5:
Console.WriteLine("Chosen EXIT, goodbye.");
break;
}
if(menu != 1 && menu !=2 && menu!= 3 && menu != 4 && menu != 5)
{
Console.WriteLine("\nERROR ! WRONG INPUT !\n");
}
}while (menu != 5);
Console.ReadKey();
Arrays

Exercise 1:
Write a C# program to input an array of float numbers and and fulfill the following
requirements:
- Output array to screen
- Find maximum and minimum values in array
- Count total number of negative elements in array
- Find sum of all elements of an array
- Count positive even and negative odd elements in array
float[] arr = new float[20];
int n, countN, countPE, countNO;
float max, min, sum;
Console.WriteLine("Enter number of value (limit 20): ");
n = Convert.ToInt32(Console.ReadLine());
//in
for(int i = 0; i < n; i++ )
{
Console.Write("Value #{0}: ", (i+1));
arr[i] = float.Parse(Console.ReadLine());
}
//out
Console.WriteLine("\nYour array: ");
for(int i = 0; i < n; i++)
{
Console.WriteLine("Value #{0}: {1}", (i + 1), arr[i]);
}
//max,min,negative counts,sum
max = min = arr[0];
countN = countPE = countNO = 0; sum = 0;
for(int i = 0; i < n; i++)
{
if (max < arr[i])
max = arr[i];
if (min > arr[i])
min = arr[i];
if (arr[i] < 0)
{
countN++;
if( arr[i] % 2 != 0)
{
countNO++;
}
}
if (arr[i] > 0 && arr[i] % 2 == 0)
{
countPE++;
}
sum = sum + arr[i];
}
Console.WriteLine("\nMax is {0}, min is {1}", max, min);
Console.WriteLine("There are {0} negative number.", countN);
Console.WriteLine("Sum off all elements: {0}.");
Console.WriteLine("There are {0} odd negative numbers and {1} even
positive numbers.", countNO, countPE);
Console.ReadKey();

Exercise 2:
Write C# program to print all unique element in an array
int n;
int[] arr = new int[20];
bool IsUnique;
Console.Write("Enter the ammount of numbers (limit 20): ");
n = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i < n; i++)
{
Console.Write("Enter number #{0}: ", (i + 1));
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("The unique numbers from your sequence are: ");
for(int i = 0; i < n; i++)
{
IsUnique = true;
for(int j = 0; j < n; j++ )
{
if (arr[i] == arr[j] && i != j)
{
IsUnique = false;
}
}
if (IsUnique == true)
{
Console.Write("{0} ", arr[i]);
}
}
Console.ReadKey();

Exercise 3:
Write C# program to sort an array in descending order
int n;
int[] arr = new int[20];
Console.Write("Enter the ammount of numbers (limit 20): ");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nEnter Your Array: ");
for(int i = 0; i < n; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if (arr[i] < arr[j] && i != j && i < j)
{
arr[n] = arr[i];
arr[i] = arr[j];
arr[j] = arr[n];
}
}
}
Console.WriteLine("Your array but sorted decending: ");
for(int i = 0; i < n; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.ReadKey();

Exercise 4:
Write C# program to delete all duplicate elements from an array
int n, i, j, k;
int[] arr = new int[20];

Console.Write("Enter the ammount of number: ");


n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your array: ");
for(i = 0; i < n; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 0; i <n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] == arr[j])
{
for (k = j; k < n - 1; k++)
{
arr[k] = arr[k + 1];
}
n--;
j--;
}
}
}
Console.Write("Your array but with no duplicates: ");
for(i = 0; i < n; i++)
{
Console.Write("{0} ", arr[i]);
}

Console.ReadKey();

Exercise 5:
Write C# program to insert (specific position) an element in array
int n, i, place;
int[] arr = new int[20];
Console.Write("Enter the ammount of number: ");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your array:");
for (i = 0; i < n; i++)
{
arr[i] = int.Parse(Console.ReadLine());
}
Console.Write("Here's your array: ");
for (i = 0; i < n; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.WriteLine("Where do you want to insert an element ?");
place = int.Parse(Console.ReadLine()) - 1;
Console.WriteLine("What do you want to add ?");
for(i = n; i >=place; i--)
{
arr[i + 1] = arr[i];
}
arr[place] = int.Parse(Console.ReadLine());
Console.Write("Your new array: ");
for(i = 0; i<= n;i++)
{
Console.Write("{0} ", arr[i]);
}
Console.ReadLine();

Exercise 6:
Write C# program to count total duplicate elements in an array
int n, i, j;
int count = 0;
int[] arr = new int[20];

Console.Write("Enter the ammount of number: ");


n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your array: ");
for (i = 0; i < n; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
break;
}
}
}
if(count == 0)
{
Console.WriteLine("There are no duplicates.");
}
else
{
Console.WriteLine("There are {0} duplicates.", count);
}
Console.ReadKey();

Exercise 7:
Write a program in C# language to find the second largest element in an array.
int n, i, max1, max2;
int[] arr = new int[20];
Console.Write("Enter the ammount of numbers: ");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your array:");
for(i = 0; i< n; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
max1 = max2 = arr[0];
for(i = 0; i<n; i++)
{
if (max1 < arr[i])
{
max2 = max1;
max1 = arr[i];
}
else if (max2 < arr[i] && arr[i] < max1)
max2 = arr[i];
}
Console.WriteLine("Your array's second largest number: {0}", max2);
Console.ReadKey();

Exercise 8:
Write a C# program to find the common elements between two arrays (tìm các phần tử
giống nhau giữa 2 mảng)
Example:

int n1, n2, i, j, count;


int[] arr1 = new int[20];
int[] arr2 = new int[20];
int[] arr3 = new int[20];
Console.Write("Your first array ammount of numbers: ");
n1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your first array: ");
for(i = 1; i<= n1; i++)
{
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Your second array ammount of numbers: ");
n2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your second array: ");
for (i = 1; i <= n2; i++)
{
arr2[i] = Convert.ToInt32(Console.ReadLine());
}

for (i = 0; i < n1; i++)


{
for (j = 0; j < n2; j++)
{
if (arr1[i] == (arr2[j]))
{
Console.WriteLine("Common elements: {0}", arr1[i]);
}
}
}
Console.ReadKey();

Exercise 9:
Write a C# program to find the duplicate values of an array of integer values
int n, i, j, k = 0;
int[] arr = new int[20];
int[] duplicates = new int[20];
Console.Write("Enter the ammount of number: ");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your array: ");
for (i = 0; i < n; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] == arr[j])
{
Console.WriteLine("Duplicated elements: {0}", arr[i]);
break;
}
}
}
Console.ReadKey();

Exercise 10:
Write a C# program to find second lowest number from the array
int n, i, min1, min2;
int[] arr = new int[20];
Console.Write("Enter the ammount of numbers: ");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your array:");
for (i = 0; i < n; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
min1 = min2 = arr[0];
for (i = 0; i < n; i++)
{
if (min1 > arr[i])
{
min2 = min1;
min1 = arr[i];
}
else if (min2 > arr[i] && arr[i] > min1)
min2 = arr[i];
}
Console.WriteLine("Your array's second lowest number: {0}", min2);
Console.ReadKey();

Exercise 11:
Write a C# program to print the largest and smallest values in an array of integers
int n, i, min, max;
int[] arr = new int[20];
Console.Write("Enter the ammount of numbers: ");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your array:");
for (i = 0; i < n; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
min = max = arr[0];
for (i = 0; i < n; i++)
{
if (max < arr[i])
max = arr[i];
if (min > arr[i])
min = arr[i];
}
Console.WriteLine("Min: {0} Max: {1}", min, max);
Console.ReadKey();

Exercise 12:
Write a C# program to find the most appeared element in an array
int n, i, j, number, count = 1, tempcount, tempnumber = 0;
int[] arr = new int[20];
number = arr[0];
Console.Write("Enter the ammount of numbers in your array: ");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter your array: ");
for(i = 0; i < n; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
for( i = 0; i < n; i++)
{
tempcount = 0;
tempnumber = arr[i];
for( j = i; j < n; j++)
{
if(tempnumber == arr[j])
{
tempcount++;
}
}
if(tempcount > count)
{
count = tempcount;
number = tempnumber;
}
}
Console.WriteLine("The most frequent number is {0}, repeated {1} times.",
number, count);
Console.ReadKey();

You might also like