C# exercises - Part 1

Good coding starts with getting the basics right! These C# functions written for my Coder Foundry class perform some basic and not-so-basic numeric tasks. Click the "View Code" button to see exactly how each one is written. See C Sharp Part 2 for text analysis function examples, or C Sharp Web API Style for the AngularJS/Web API version.

Factorial view code

Task: Write a function that calculates the factorial of a number. The factorial of a non-negative interger n, denoted by n!, is the product of all positive integers less than or equal to n. (i.e. 5! = 5 x 4 x 3 x 2 x 1 = 120)

The factorial of 5 is : 120

Palindrome view code

Task: Define a function palindrome() that computes the reversal of a string, returning true if the string is a palindrome (the same forwards and backwards), and false otherwise.

Radar is a palindrome. Hello World is NOT a palindrome.

FizzBuzz view code

Task: Write a program that iterates the integers from 1 to 100, printing the numbers to the screen. For each number that is a multiple of 3, however, print "Fizz" instead of the number. For multiples of 5 print "Buzz" instead of the number. For numbers that are multiples of both print "FizzBuzz."

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz

Max of Three view code

Task: Define a function that takes 3 numbers as arguments and returns the largest of them.

The max of 10, 20, 30 is: 30

Sum view code

Task: Define a function sum() that sums all the all the numbers in an array of numbers.

The sum of 1,2,3,4 is : 10

Multiply view code

Task: Define a function that calculates the product of all the numbers in an array of numbers.

The product of 1,2,3,4 is : 24