FizzBuzz w/ C#


  • 0
    J
    public class Solution {
        public IList<string> FizzBuzz(int n) {
            List<string> result = new List<string>();
    
            for (int i = 1; i <= n; i++)
            {
                if (i % 3 == 0 & i % 5 != 0) //multiples of three
                {
                    result.Add("Fizz");
                }
                else if (i % 5 == 0 & i % 3 != 0) //multiples of five
                {
                    result.Add("Buzz");
                }
                else if (i % 15 == 0)
                {
                    result.Add("FizzBuzz");
                }
                else
                {
                    result.Add(Convert.ToString(i));
                }
    
            }
    
            return result;
            
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.