Problem of the Day
A new programming or logic puzzle every Mon-Fri

Problem of the Day

You've got an easy one for today. Create a program that iterates over the numbers [1-100] (inclusive). If a number is divisible by 1 print "problem". If a number is divisible by 5 print "of". If a number is divisible by 25 print "the". If a number is divisible by 100 print "day". Thus when you reach 100 your final line of output should be "problemoftheday".

Permalink: http://problemotd.com/problem/problem-of-the-day/

Comments:

  • Daniel - 3 years, 4 months ago

    My very simple C# solution

        static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                Console.WriteLine(String.Format("problem{0}{1}{2}", (i % 5 == 0 ? "of" : ""), (i % 25 == 0 ? "the" : ""), (i % 100 == 0 ? "day" : "")));
            }
            Console.ReadKey();
        }
    

    I shall try to make it shorter if I have time later.

    reply permalink

  • Daniel - 3 years, 4 months ago

    A little shorter

        static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
                Console.WriteLine("problemoftheday".Substring(0, (i % 100 == 0 ? 15 : (i % 25 == 0 ? 12 : (i % 5 == 0 ? 9 : 7)))));
            Console.ReadKey();
        }
    

    reply permalink

  • Max Burstein - 3 years, 4 months ago

    You're going to love tomorrow's problem

    reply permalink

  • Anonymous - 3 years, 4 months ago

    Python

    for i in range(1,101):
        text = "problem"
        if i%5 == 0:
            text += "of"
        if i%25 == 0:
            text += "the"
        if i%100 == 0:
            text += "day"
        print text
    

    reply permalink

  • Sellyme - 3 years, 4 months ago

    I had broadly the same solution, but for efficiency, I nested the %25 and %100 statements. If i%5 isn't 0, you don't need to test i%25, and if i%25 is 0, you don't need to test i%100.

    reply permalink

  • Anonymous - 3 years, 4 months ago

    #python 2.7
    for x in range(100):
        y = x+1
        string = "problem"
        if y%5==0:
            string = string + "of"
            if y%25 == 0:
                string = string + "the"
                if y%100 == 0:
                    string = string + "day"
        print string
    

    reply permalink

Content curated by @MaxBurstein