Examples

Please refer to the original site for a more comprehehsive list of good examples to try when learning a new language: Adrian Neumann Site

This is a partial list of simple examples that can help you learn a language:

  1. Write a program that prints 'Hello World' to the screen.
  2. Write a program that asks the user for their name and greets them with their name.
  3. Modify the previous program such that only the users Alice and Bob are greeted with their names.
  4. Write a program that asks the user for a number n and prints the sum of the numbers 1 to n
  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
  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.
  7. Write a program that prints a multiplication table for numbers up to 12.
  8. Write a program that prints the first 30 prime numbers.
  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. It counts only as one try if they input the same number multiple times consecutively.
  10. Write a program that prints the next 30 leap years.
  11. Write a program that computes 4k=1106(1)k+12k1=4(11/3+1/51/7+1/91/11).4\cdot \sum_{k=1}^{10^6} \frac{(-1)^{k+1}}{2k-1} = 4\cdot(1-1/3+1/5-1/7+1/9-1/11\ldots).
  12. Write a function that returns the largest element in a list.
  13. Write function that reverses a list, preferably in place.
  14. Write a function that checks whether an element occurs in a list.
  15. Write a function that tests whether a string is a palindrome.
  16. Write a function map that applies a function to every element of a list. Test your function by passing it a list of numbers and returning a list of each number squared.
  17. Write a function that takes a number and returns a list of its digits. So for 2345 it should return [2,3,4,5].
  18. Write a program that outputs all possibilities to put + or - or nothing between the numbers 1,2,…,9 (in this order) such that the result is 100. For example 1 + 2 + 3 - 4 + 5 + 6 + 78 + 9 = 100.