-
Description 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 using System; public class Kumite { public static bool IsThree(int n) { while(n%10!=3&&n>0)n/=10; return n>0; } }
Test Cases using NUnit.Framework; namespace Tests { [TestFixture] class TestsClass { [Test] public void Tests() { Assert.IsFalse(Kumite.IsThree(525458)); Assert.IsFalse(Kumite.IsThree(2222)); Assert.IsTrue(Kumite.IsThree(354523)); Assert.IsTrue(Kumite.IsThree(748504231)); } } }
Output:
-
Code - 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;
- }
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}