Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Student
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public DateTime Birthdate { get; set; }
- public string Town { get; set; }
- }
- class Program
- {
- static void Main()
- {
- //string student1Name = "Demetra";
- //string[] studentNames = new string[100];
- //string[] studentTowns = new string[100];
- Student demetra = new Student();
- demetra.Name = "Demtra";
- demetra.Age = 18;
- demetra.Birthdate = new DateTime(1990, 4, 29);
- demetra.Town = "Plovdiv";
- Student djulian = new Student();
- djulian.Name = "Djulian";
- djulian.Age = 20;
- djulian.Birthdate = new DateTime(1991, 6, 29);
- djulian.Town = "Karlovo";
- Student[] students = new Student[100];
- InputStudents(students);
- Console.WriteLine("My name is {0}. I am {1} years old!", demetra.Name, demetra.Age);
- Console.WriteLine("My name is {0}. I am {1} years old!", djulian.Name, djulian.Age);
- Console.Write("I will give you the sum and factorial of all number to n. Please, enter n: ");
- int n = int.Parse(Console.ReadLine());
- int sum = 0;
- checked
- {
- sum = GetSumIteration(n);
- }
- Console.WriteLine("The sum of all number to n = {0} is {1}", n, sum);
- // sum = Sum(n);
- // Console.WriteLine("The sum of all number to n = {0} is {1}", n, sum);
- int factorial = 0;
- factorial = Factorial(n);
- Console.WriteLine("{0}! = {1}", n, factorial);
- // 0, 1, 1, 2, 3, 5, 8, 13
- // Fibonacci(n) = Fibonacci(n - 2) + Fibonacci(n - 1)
- int fibonacci = 0;
- fibonacci = Fibonacci(n);
- Console.WriteLine("Fibonacci({0}) = {1}", n, fibonacci);
- }
- static int GetSumIteration(int n)
- {
- int sum = 0;
- for (int i = 0; i <= n; i++)
- {
- checked
- {
- sum += i;
- }
- }
- return sum;
- }
- // Sum(n) = Sum(n-1) + n Sum(0) = 0;
- static int Sum(int n)
- {
- // 1. Check for the end of the recursion
- if (n == 0) return 0;
- // 2. change the parameter
- // 3. call the same function
- return Sum(n - 1) + n;
- }
- // 2. 0! = 1, n! = (n - 1)! * n;
- static int Factorial(int n) // !
- {
- if (n == 0) return 1;
- return Factorial(n - 1) * n;
- }
- static int Fibonacci(int n)
- {
- if (n <= 0) return 0;
- if (n <= 2) return 1;
- return Fibonacci(n - 2) + Fibonacci(n - 1);
- }
- static void InputStudents(Student[] students)
- {
- for (int i = 0; i < students.Length; i++)
- {
- Student student = new Student();
- Console.WriteLine("Please, enter a name for student {0}: ", i);
- student.Name = Console.ReadLine();
- Console.WriteLine("Please, enter an age for student {0}: ", i);
- student.Age = int.Parse(Console.ReadLine());
- students[i] = student;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement