Software Development Tricky

Friday 28 April 2017

Fibonacci FizzBuzz Algorithm


Ø  Find give question answer with help of first 20 fibonacci series. First 20 fibonacci series are 1, 1, 2, 3, 5, 8, 13

Question
Expect Output
         Concatenate each Fibonacci series number
            If number contain ‘3’ than add ‘Fizz’
Ex: 1123

       If number contain ‘5’ than add ‘Buzz’
ex: 11235

       If Number divide by 3
Add ‘Fizz’
       If Number divide by 5
Add ‘Buzz’
        None of them not match
Return same number

Note: using oops concept to develop this algorithm

Sample Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
namespace Logical_Programs
{
    class FizzBuzz_Algorithm
    {
        static void Main()
        {
            string fbstrings = "";
            int[] myArray = new int[] { 1,1,2,3,5,8,13 };
            Console.WriteLine("FizzBuzz Algorithm Implementation");
            Console.WriteLine("**********************************");
            Console.WriteLine("Results:");
            foreach(int i in myArray)
            {
                fbstrings += i;
                Console.WriteLine("{0}", FizzBuzz.GetFizzBuzz(fbstrings.Trim(), i));
            }
            Console.WriteLine("**********************************");
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }
    }
    public static class FizzBuzz
    {
        public static string GetFizzBuzz(string fbstrings, int fbValues)
        {
            string localFbString = "";
            if (fbstrings.Contains("3"))
            {
                localFbString += "Fizz";
            }
            if (fbstrings.Contains("5"))
            {
                localFbString += "Buzz";
            }
            if (Convert.ToInt32(fbstrings) % 3 == 0)
            {
                localFbString += "Fizz";
            }
            if (Convert.ToInt32(fbstrings) % 5 == 0)
            {
                localFbString += "Buzz";
            }
            if (localFbString == "")
            {
                localFbString = fbValues.ToString();
            }
            return localFbString;
        }
    }
}

Output:



If you like this post. Kindly share your feedback.

No comments:

Post a Comment

Followers