1. Write a program that prints all the numbers from 1 to N.
using System; class PrintNumbersFromOneToN { static void Main() { Console.WriteLine("Enter one number"); int number = int.Parse(Console.ReadLine()); for (int i = 1; i <= number; i++) { Console.WriteLine(i); } } }
2. Write a program that prints all the numbers from 1 to N, that are not divisible by 3 and 7 at the same time.
using System; class PrintNumbersFromOneToNNotDivisibleByThreeAndSeven { static void Main() { Console.WriteLine("Enter a number: "); int number = int.Parse(Console.ReadLine()); for (int i = 1; i <= number; i++) { if (i % 3 != 0 && i % 7 != 0) { Console.WriteLine(i); } } } }
3. Write a program that reads from the console a sequence of N integer numbers and returns the minimal and maximal of them.
using System; class MinimalAndMaximalOfNIntigerNumbers { static void Main() { Console.WriteLine("Enter the count of numbers:"); int number = int.Parse(Console.ReadLine()); Console.WriteLine("Enter a number:"); int currentNumber = int.Parse(Console.ReadLine()); int minNumber; int maxNumber; minNumber = maxNumber = currentNumber; for (int i = 1; i < number; i++) { Console.WriteLine("Enter a number:"); currentNumber = int.Parse(Console.ReadLine()); if (currentNumber > maxNumber) { maxNumber = currentNumber; } if (currentNumber < minNumber) { minNumber = currentNumber; } } Console.WriteLine("The smallest number is: {0}" , minNumber); Console.WriteLine("The biggest number is: {0}" , maxNumber); } }
4. Write a program that calculates N!/K! for given N and K (1<K<N).
using System; class DivideFactorials { static void Main() { Console.WriteLine("Enter a value for N"); int n = int.Parse(Console.ReadLine()); Console.WriteLine("Enter a value for K (1 < K < N )"); int k = int.Parse(Console.ReadLine()); double nFactorial = 1; double kFaktorial = 1; for (int i = n; i > 0; i--) { nFactorial *= i; } for (int j = k; j > 0; j--) { kFaktorial *= j; } double result = nFactorial / kFaktorial; Console.WriteLine("N!/K! = {0}" ,result); } }
5. Write a program that calculates N!*K! / (K-N)! for given N and K (1<N<K).
using System; class MultiplyFactorials { static void Main() { Console.WriteLine("Enter a value for N:"); int n = int.Parse(Console.ReadLine()); Console.WriteLine("Enter a value for K (1 < N < K)"); int k = int.Parse(Console.ReadLine()); int differenceNK = n - k; double nFactorial = 1; double kFactorial = 1; double differeceNKFactorial = 1; for (int i = n; i > 0; i--) { nFactorial *= i; } for (int i = k; i > 0; i--) { kFactorial *= i; } for (int j = differenceNK; j > 0; j--) { differeceNKFactorial *= j; } double result = (nFactorial * kFactorial) / differeceNKFactorial; Console.WriteLine("N!*K!/(N-K)!= {0}" , result); } }
6. Write a program that, for a given two integer numbers N and X, calculates the S = 1 + 1!/X + 2!/X2 + … + N!/XN.
using System; class CalculateSumS { static void Main() { Console.WriteLine("Enter a Value for N:"); int n = int.Parse(Console.ReadLine()); Console.WriteLine("Enter a value for X:"); int x = int.Parse(Console.ReadLine()); double sum = 1; double factorial = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { factorial *= j; } sum += factorial / Math.Pow(x, i); factorial = 1; } Console.WriteLine("Sum S is equal to: {0}", sum); } }
7. Write a program that reads a number N and calculates the sum of the first N members of the sequence of Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, …
Each member of the Fibonacci sequence (except the first two) is a sum of the previous two members.
using System; class CalculateSumOfFibonacciSequence { static void Main() { Console.WriteLine("Enter a value for the Fibonacci members:"); int members = int.Parse(Console.ReadLine()); int firstMember = 0; int secondMember = 1; int sum = 0; for (int i = 1; i <= members; i++) { sum = firstMember + secondMember; firstMember = secondMember; secondMember = sum; } Console.WriteLine("The sum of first {0} members is equal to: {1}" , members , secondMember - 1); } }
8. Write a program that calculates the greatest common divisor (GCD) of given two numbers. Use the Euclidean algorithm (find it in Internet).
using System; class GreatestCommonDivisorOfTwoNumbers { static void Main() { Console.WriteLine("Enter a value for the first number:"); int firstNumber = int.Parse(Console.ReadLine()); Console.WriteLine("Enter a value for the second number:"); int secondNumber = int.Parse(Console.ReadLine()); int temp; if (firstNumber == secondNumber) { Console.WriteLine("First number is equal to second number"); } if (firstNumber > secondNumber) { while (firstNumber % secondNumber != 0) { temp = firstNumber % secondNumber; firstNumber = secondNumber; secondNumber = temp; } Console.WriteLine("The gratest common divisor is: {0}" , secondNumber); } if (secondNumber > firstNumber) { while (secondNumber % firstNumber != 0) { temp = secondNumber % firstNumber; secondNumber = firstNumber; firstNumber = temp; } Console.WriteLine("The gratest common divisor is: {0}" , firstNumber); } } }
10. In the combinatorial mathematics, the Catalan numbers are calculated by the following formula:
Write a program to calculate the Nth Catalan number by given N.
using System; using System.Numerics; class NthCatalanNumber { static void Main() { Console.WriteLine("Cn = (2n)!/(1 + n)!*n!"); Console.Write("Enter a value for n: "); int number = int.Parse(Console.ReadLine()); BigInteger firstFactorial = 1; BigInteger secondFactorial = 1; BigInteger thirdFactorial = 1; for (int i = number * 2; i > 0; i--) { firstFactorial *= i; } for (int j = number + 1; j > 0; j--) { secondFactorial *= j; } for (int k = number; k > 0; k--) { thirdFactorial *= k; } BigInteger catalanNumber = firstFactorial / (secondFactorial * thirdFactorial); Console.WriteLine("Cn = {0}" , catalanNumber); } }
11. Write a program that prints all possible cards from a standard deck of 52 cards (without jokers). The cards should be printed with their English names. Use nested for loops and switch-case.
using System; class PrintAllCards { static void Main() { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 13; j++) { switch (i) { case 1: Console.Write("Clubs "); break; case 2: Console.Write("Diamonds "); break; case 3: Console.Write("Hearts "); break; case 4: Console.Write("Spades "); break; default: break; } switch (j) { case 1: Console.WriteLine("deuce"); break; case 2: Console.WriteLine("three"); break; case 3: Console.WriteLine("four"); break; case 4: Console.WriteLine("five"); break; case 5: Console.WriteLine("six"); break; case 6: Console.WriteLine("seven"); break; case 7: Console.WriteLine("eight"); break; case 8: Console.WriteLine("nine"); break; case 9: Console.WriteLine("ten"); break; case 10: Console.WriteLine("jack"); break; case 11: Console.WriteLine("queen"); break; case 12: Console.WriteLine("king"); break; case 13: Console.WriteLine("ace"); break; default: break; } } } } }
12. Write a program that reads from the console a positive integer number N (N < 20) and outputs a matrix like the following:
N = 3 N = 4
using System; class MatrixOutput { static void Main() { Console.WriteLine("Enter one number between 1 and 20: "); int number = int.Parse(Console.ReadLine()); for (int i = 1; i <= number; i++) { Console.WriteLine(" "); for (int j = i; j <= number - 1 + i; j++) { Console.Write(" " + j + " "); } } } }
Няма коментари:
Публикуване на коментар