C# Are there three (without converting to string)

A while loop does all the work of iterating every digit in n; first it asks if the digit on the right is 3, and if it is it stops looping. If it is not, and n is still greater than 0, it divides n by 10. If we hit 0 for n we have iterated the entire number, and the loop stops now if it didn't already. This means we just have to check if n is greater than 0 - if it is we stopped early because we found a 3.

Code
Diff
• ``````using System;

public class Kumite {
public static bool IsThree(int n) {
while(n%10!=3&&n>0)n/=10;
return n>0;
}
}``````
• using System;
• public class Kumite {
• public static bool IsThree(int number) {
• var digitToSearch = 3;
• var amountOfDigits = (int)Math.Log10(number);
• for (int i = 0; i <= amountOfDigits; i++)
• {
• var divisor = (int)Math.Pow(10, i);
• var truncate = number / divisor;
• var digit = truncate % 10;
• if (digit == digitToSearch)
• {
• return true;
• }
• }
• return false;
• public static bool IsThree(int n) {
• while(n%10!=3&&n>0)n/=10;
• return n>0;
• }
• }