Shawn's Programming Portfolio

20 Questions.
C# Programming Language.
List made by Adriann.
Let me know what you think!

Basics

1. Write a program that prints ‘Hello World’ to the screen.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
  
Hello World

2. Write a program that asks the user for their name and greets them with their name.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter your name: ");
            string yourName = Console.ReadLine();
            Console.WriteLine("Greetings " + yourName);
        }
    }
}

Please enter your name: Shawn
Greetings Shawn

3. Modify the previous program such that only the users Alice and Bob are greeted with their names.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter your name: ");
            string yourName = Console.ReadLine();
            if (yourName == "bob" | yourName == "alice") { 
                    Console.Write("Greetings " + yourName);
            }
        }
    }
}

Please enter your name: Alice
Greetings Alice
Please enter your name: Shawn

4. Write a program that asks the user for a number n and prints the sum of the numbers 1 to n.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n, sum = 0; 
            Console.Write("Please enter a number: ");
            n = Int32.Parse(Console.ReadLine());
            for (i = 1; i <= n; i++)
            {
                sum += i;
            }
            Console.WriteLine(sum);
        }
    }
}

Please enter a number: 5
15
Please enter a number: 6
21

5. Modify the previous program such that only multiples of three or five are considered in the sum, e.g. 3, 5, 6, 9, 10, 12, 15 for n=17

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n, sum = 0;
            Console.Write("Please enter a number: ");
            n = Int32.Parse(Console.ReadLine());
            for (i = 1; i <= n; i++)
            {
                if (i % 3 == 0 | i % 5 == 0)
                {
                    sum = sum + i;
                }
            }
            Console.WriteLine(sum);
        }
    }
}

Please enter a number: 17
60
Please enter a number: 10
33

6. Write a program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n, sum = 1;
            Console.Write("Please enter a number: ");
            n = Int32.Parse(Console.ReadLine());
            Console.Write("What would you like to compute? The sum (1) or the product (2): ");
            switch (Console.ReadLine())
            {
                case "1":
                    for (i = 1; i <= n; i++)
                    {
                        sum += i;
                    }
                    Console.WriteLine(sum - 1);
                    break;
                case "2":
                    for (i = 1; i <= n; i++)
                    {
                        sum = sum * i;
                    }
                    Console.WriteLine(sum);
                    break;
            }
        }
    }
}

Please enter a number: 6
What would you like to compute? The sum (1) or the product (2): 1
21
Please enter a number: 5
What would you like to compute? The sum (1) or the product (2): 2
120

7. Write a program that prints a multiplication table for numbers up to 12.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            for (n = 1; n <= 12; n++)
            {
                int i;
                for (i = 1; i <= 12; i++)
                {
                    Console.Write(n * i);
                    Console.Write(" "); 
                }
                Console.WriteLine();
            }
        }
    }
}

1 2 3 4 5 6 7 8 9 10 11 12 
2 4 6 8 10 12 14 16 18 20 22 24 
3 6 9 12 15 18 21 24 27 30 33 36 
4 8 12 16 20 24 28 32 36 40 44 48 
5 10 15 20 25 30 35 40 45 50 55 60 
6 12 18 24 30 36 42 48 54 60 66 72 
7 14 21 28 35 42 49 56 63 70 77 84 
8 16 24 32 40 48 56 64 72 80 88 96 
9 18 27 36 45 54 63 72 81 90 99 108 
10 20 30 40 50 60 70 80 90 100 110 120 
11 22 33 44 55 66 77 88 99 110 121 132 
12 24 36 48 60 72 84 96 108 120 132 144 

8. Write a program that prints all prime numbers.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            for (i = 0; i < 100; i++)
            {
                if (isPrime(i)){
                    Console.WriteLine(i);
                }
            }

            bool isPrime(int n)
            {
                int k = 2;
                while (k * k <= n)
                {
                    if ((n % k) == 0)
                        return false;
                    else k++;
                }
                return true;
            }
        }
    }
}

2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

9. Write a guessing game where the user has to guess a secret number. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int secretNumber = 69;
            int answer;
            int count = 0;
            do
            {
            Console.Write("Try to guess the secret number(below 100): ");
            answer = Int32.Parse(Console.ReadLine());
                if (answer < secretNumber)
                {
                    Console.WriteLine("↑ Go up ↑");
                    count++;
                }
                if (answer > secretNumber)
                {
                    Console.WriteLine("↓ Go down ↓");
                    count++;
                }
            }
            while (answer != secretNumber);
            Console.WriteLine("Congratulations, you've guessed the secret number " + secretNumber + " after " + count + " trie(s).");
        }
    }
}

Try to guess the secret number(below 100): 20
↑ Go up ↑
Try to guess the secret number(below 100): 50
↑ Go up ↑
Try to guess the secret number(below 100): 80
↓ Go down ↓
Try to guess the secret number(below 100): 60
↑ Go up ↑
Try to guess the secret number(below 100): 65
↑ Go up ↑
Try to guess the secret number(below 100): 70
↓ Go down ↓
Try to guess the secret number(below 100): 69
Congratulations, you've guessed the secret number 69 after 6 trie(s).

10. Write a program that prints the next 20 leap years.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int year = 2019;
            int count = 0;
            do
            {
                if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
                {
                    Console.WriteLine(year);
                    count++;
                }
                year++;
            }
            while (count < 20);
        }
    }
}

2020
2024
2028
2032
2036
2040
2044
2048
2052
2056
2060
2064
2068
2072
2076
2080
2084
2088
2092
2096

11. Write a program that prints an Easter Egg.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            bool yup = true;
            do
            {
                Console.Write("Please enter a special character(@#$%&): ");
                string input = Console.ReadLine();
                if (input == "@" | input == "#" | input == "$" | input == "%" | input == "&")
                {
                    Console.WriteLine("   __   ");
                    Console.WriteLine(@"  /" + input + input + @"\  ");
                    Console.WriteLine(@" /" + input + input + input + input + @"\  ");
                    Console.WriteLine(" |" + input + input + input + input + "| ");
                    Console.WriteLine(@" \____/");
                    yup = false;
                }
                else
                {
                    Console.WriteLine("Please enter one of the accepted characters");
                    Console.WriteLine();
                }
            } while (yup == true);
        }
    }
}

Please enter a special character(@#$%&): S
Please enter one of the accepted characters

Please enter a special character(@#$%&): %
   __   
  /%%\  
 /%%%%\  
 |%%%%| 
 \____/

Lists & Strings

12. Write a function that returns the largest element in a list.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            var l = new List<int>();
            for(i = 0; i < 100+1; i++)
            {
                l.Add(i);
            }
            Console.WriteLine(l.Max());
        }
    }
}

100

13. Write function that reverses a list, preferably in place.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            var l = new List<int>();
            int i = 0;
            for(i = 0; i < 100+1; i++)
            {
                l.Add(i);
            }
            Console.WriteLine("This is the normal list");
            foreach (int m in l)
            {
                Console.Write(m);
                Console.Write(" ");
            }
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("This is the reversed list");
            l.Reverse();
            foreach(int m in l)
            {
                Console.Write(m);
                Console.Write(" ");
            }
        }
    }
}

This is the normal list
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 

This is the reversed list
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 

14. Write a function that checks whether an element occurs in a list.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            var l = new List<int>();
            for(i = 0; i < 100+1; i++)
            {
                l.Add(i);
            }
            Console.Write("Please enter a number to check if it is in the list: ");
            int check = Int32.Parse(Console.ReadLine());
            bool isIn = false;
            foreach (int index in l)
            {
                if (index == check)
                {
                    Console.WriteLine("It is in the list.");
                    isIn = true;
                }
            }
            if (isIn == false)
            {
                Console.WriteLine("It is not in the list.");
            }
        }
    }
}

Please enter a number to check if it is in the list: 69
It is in the list.
Please enter a number to check if it is in the list: 101
It is not in the list.

15. Write a function that returns the elements on odd positions in a list.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            var l = new List<int>();
            for (i = 0; i < 100 + 1; i++)
            {
                l.Add(i);
            }
            foreach (int index in l)
            {
                if (index % 2 != 0)
                {
                    Console.Write(index);
                    Console.Write(" ");
                }
            }
        }
    }
}     

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 

16. Write a function that computes the running total of a list.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            var l = new List<int>();
            for (i = 0; i < 100 + 1; i++)
            {
                l.Add(i);
            }
            int count = 0;
            foreach (int index in l)
            {
                count++;
            }
            Console.WriteLine("There are " + count + " total elements in this list.");
        }
    }
}     

There are 101 total elements in this list.

17. Write a function that tests whether a string is a palindrome.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter a palindrome: ");
            string Input = Console.ReadLine();


            char[] reversedInput = Input.ToCharArray();
            Array.Reverse(reversedInput);
            string m = new string(reversedInput);

            string i = Input.ToLower();
            string x = m.ToLower();

            if (i == x)
            {
                Console.WriteLine("This is a Palindrome.");
            }
            else {
            Console.WriteLine("This is not a Palindrome.");
            }
        }
    }
}

Please enter a palindrome: Shawn
This is not a Palindrome.
Please enter a palindrome: Kayak
This is a Palindrome.

18. Write three functions that compute the sum of the numbers in a list: using a for-loop, a while-loop and recursion.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            var l = new List<int>();
            for (i = 0; i < 100 + 1; i++)
            {
                l.Add(i);
            }
            Console.WriteLine(sumForLoop(l));
            Console.WriteLine(sumWhileLoop(l));
            Console.WriteLine(sumRecursion(l, 0));
        }
        static int sumForLoop(List<int> Items)
        {
            int m = 0, i = 0;
            for(i = 0;m < Items.Count-1; i++)
            {
                i += m;
                m++;
            }
            return i;
        }
        static int sumWhileLoop(List<int> Items)
        {
            int i = 0, m = 0;
            while (m < Items.Count)
            {
                i += m;
                m++;
            }
            return i;
        }
        static int sumRecursion(List<int> Items, int value)
        {
            if (value >= Items.Count)
            {
                return 0;
            }
            return Items[value] + sumRecursion(Items, value + 1);
        }
    }
}

5050
5050
5050

19. Write a function on_all that applies a function to every element of a list. Use it to print the first twenty perfect squares. The perfect squares can be found by multiplying each natural number with itself.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = 0;
            int i;
            var l = new List<int>();
            for (i = 0; i < 1000; i++)
            {
                l.Add(i);
            }
            on_all(l, (int x) => { if (count < 20) { if (isPerfectSquare(x)) { count++; Console.WriteLine(x); } } return 0; });
        }

        private static bool isPerfectSquare(int x)
        {
            double m = Math.Floor(Math.Sqrt(x));
            return m * m %gt;= (double) x;
        }

        private static void on_all(List<int> l, Func<int, object> p)
        {
            foreach (int i in l)
            {
                p.Invoke(i);
            }
        }
    }
}

0
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361

20. Write a function that computes the list of the first 100 Fibonacci numbers.

using System;

namespace Programs
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine(fibonacci(i));
            }  

            long fibonacci(int amount)
            {
                if (amount == 0)
                {
                    return 0;
                }
                else if (amount == 1)
                {
                    return 1;
                }
                else
                {
                    return fibonacci(amount - 1) + fibonacci(amount - 2);
                }
            }
        }
    }
}

  
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
165580141
267914296
433494437
701408733
...and so on...




←Go back to shawnbeaton.com