This was one of the first exercises we received during my first year of programming. I was asked about it in an interview years ago and I also found it as a programming challenge in a website. For a given number, for example 723, find the sum of its digits. In our example is 7+2+3 which is 12. To my surprise, not everyone is able to find an elegant solution. I saw many trying to convert it to string and then loop though the characters etc. Here we will provide an elegant solution based on math and how integer division is working in most programming languages, including C#.
Check if number is Prime – C# Method
Prime number or Prime is a number greater than 1 that can be divided only by 1 and itself. In other words, it can’t be formed by multiplying two other numbers. The first ten prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. 2 is the only even number that is prime. There are several ways to check for prime numbers and in some cases a brute force algorithm might not be a problem. If the system has restrictions or you are trying to solve a programming challenge with time restrictions then you need an algorithm that can find the answer fast. The method provided is simple and fast and used in many programming challenges with time restrictions.
Least Common Multiple (LCM) – C# Method
The Least Common Multiple (LCM) or Lowest Common Multiple of two integers is the smallest integer that is divisible by both numbers. The method that is provided in this post uses the Greatest Common Divisor (GCD). This one line method calculates the LCM by leveraging the GCD, the code of the GCD method is provided in the GCD post.
Greatest Common Divisor (GCD) – C# Method
I’ve decided to create a post series with methods I found useful, especially when solving programming challenges. The first one is how to calculate the Greatest Common Divisor (GCD) of two numbers. GCD is the largest number that divides both numbers without a reminder. We follow the Euclidean algorithm which is very simple and fast.
WPF – Styles – Change Image Source
I don’t post in this section often because I don’t know the most common problems unless I receive an email from someone about a problem. You can understand the need with the number of results after searching about a specific problem. Many programmers have this issue, they find difficulties when they try to change the source of the image using datatriggers. This is because they try to put only the change in the trigger. In order for this to work you should also include the source property in the style as a setter for the default image source instead of initializing it in your image. If you initialize the source in the image tag like this <Image Source=”/Resources/OK.png” /> it will not work.
C# – Extension Methods – IsPalindrome
Extension Methods allow you to extend existing types by adding methods to them. The extension methods are special static methods and they are called the same way as type’s instance methods. In this example we will implement an extension method called IsPalindrome which extends strings and you can check them if they are palindrome. After that you can use the IsPalindrome() method just like any string method, for example Trim().