Share via

Simple string algoritm

diego gutierrez 161 Reputation points
2022-01-18T19:25:16.403+00:00

Hi everybody.

How can I create a loop with this problem:

Input n = 21,

Output Return: ['1','2','Big','4','Bang','Big','Theory','8','Big','Bang','11','Big','13','Theory','BigBang','16','17','Big','19','Bang','BigTheory']

If the number is multiple of 3 = 'Big', if it is multiple of 5 = 'Bang', if it is multiple of 7 = 'Theory', if it is multiple of 3 and 5 (15) = BigBang, if it is multiple of 3 and 7 (21) = BigTheory.

Thanks in advance.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


2 answers

Sort by: Most helpful
  1. diego gutierrez 161 Reputation points
    2022-01-18T19:53:40.7+00:00

    I have this. But it only works for number 21, if somebody puts a higher number like 500 it won't work anymore.

    public string ReturnNumbers(int number)
    {
    string strResulting = "[";
    int contador = 1;

            while (contador <= number)
            {
                if (contador % 3 == 0 && contador % 5 == 0)
                {
                    strResulting = strResulting + "'BigBang'" + ",";
                }
                else if (contador % 3 == 0 && contador % 7 == 0)
                {
                    strResulting = strResulting + "'BigTheory'" + ",";
                }
                else if (contador % 3 == 0)
                {
                    strResulting = strResulting + "'Big'" + ",";
                }
                else if (contador % 5 == 0)
                {
                    strResulting = strResulting + "'Bang'" + ",";
                }
                else if (contador % 7 == 0)
                {
                    strResulting = strResulting + "'Theory'" + ",";
                }
                else
                {
                    strResulting = strResulting + "'" + contador.ToString() + "'" + ",";
                }
    
                contador = contador + 1;
            }
    
            strResulting = strResulting.Remove(strResulting.LastIndexOf(','));
            strResulting = strResulting + "]";
    
            return strResulting;
        }
    

    Was this answer helpful?


  2. P a u l 10,766 Reputation points
    2022-01-18T19:32:51.26+00:00

    This sounds like homework but you could google "fizz buzz c#" and get an idea of how to solve this.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.