int n = 1;
while (n < 6)
{
Console.WriteLine("Current value of n is {0}", n);
n++;
}
int x = 0;
do
{
Console.WriteLine(x);
x++;
} while (x < 5);
// loop N times
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
// nested for loops
for (i = 2; i < 100; i++)
{
for (j = 2; j <= (i / j); j++)
if ((i % j) == 0) break; // if factor found, not prime
if (j > (i / j))
Console.WriteLine("{0} is prime", i);
}
int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
foreach (int element in fibarray)
{
System.Console.WriteLine(element);
}
continue
and break
can control the loop executionProblem 1: Numbers from 1 to N
Write a program that reads from the console a positive integer N and prints all the numbers from 1 to N inclusive, on a single line, separated by a whitespace.
Input | Output |
---|---|
5 | 1 2 3 4 5 |
1 | 1 |
Problem 2: Not Divisible Numbers
Write a program that reads from the console a positive integer N and prints all the numbers from 1 to N not divisible by 3 or 7, on a single line, separated by a space.
Input | Output |
---|---|
10 | 1 2 4 5 8 10 |
3 | 1 2 |
Problem 3: MMSA of N Numbers
Write a program that reads from the console a sequence of N integer numbers and returns the minimal, the maximal number, the sum and the average of all numbers (displayed with 2 digits after the decimal point).
• The input starts by the number N (alone in a line) followed by N lines, each holding an integer number.
• The output is like in the examples given.
Problem 3: Examples
Input | Output |
---|---|
3 2 5 1 | min=1.00 max=5.00 sum=8.00 1 avg=2.67 |
3 2 -1 4 | min=-1.00 max=4.00 sum=5.00 avg=1.67 |
Problem 4: Calculate Factorials
Write a program that calculates N! / K! for given N and K. • Challenge: Use only one loop for both factorial calculations
Input | Output |
---|---|
8 3 | 6720 |
Problem 5: Matrix of Numbers
Write a program that reads from the console a positive integer number N and prints a matrix like in the examples below. Use two nested loops.
• Challenge: achieve the same effect without nested loops
Problem 5: Examples
Input | Output |
---|---|
2 |
1 2 2 3 |
4 |
1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7 |
Problem 6: Odd and Even Product
You are given N integers (given in a single line, separatedby a space).
• Write a program that checks whether the product of the odd elements is equal to the product of the even elements.
• Elements are counted from 1 to N. so the first element is odd, the second is even, etc.
Problem 6: Input / Output
Input
• On the first line you will receive the number N
• On the second line you will receive N numbers separated by a whitespace
Output
• If the two products are equal, output a string in the format "yes PRODUCT_VALUE", otherwise write on the console "no ODD PRODUCT_VALUE EVEN_PRODUCT_VALUE"
Problem 6: Examples
Input | Output |
---|---|
5 2 1 1 6 3 |
yes 6 |
5 4 3 2 5 2 |
no 16 15 |