Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate how many ways you can add three numbers so that they equal 1000

Tags:

c#

I need to create a program that calculates how many ways you can add three numbers so that they equal 1000.

I think this code should work, but it doesn't write out anything. What am I doing wrong? Any tips or solution?

using System;

namespace ConsoleApp02
{
    class Program
    {
        public static void Main(string[] args)
        {
            for(int a = 0; a < 1000; a++)
            {
                for(int b = 0; b < 1000; b++)
                {
                    for(int c = 0; c < 1000; c++)
                    {
                        for(int puls = a + b + c; puls < 1000; puls++)
                        {
                            if(puls == 1000)
                            {
                                Console.WriteLine("{0} + {1} + {2} = 1000", a, b, c);
                            }
                        }
                    }
                }
            }
            Console.ReadKey(true);
        }
    }
}
like image 1000
Freakingout Avatar asked Feb 23 '09 18:02

Freakingout


1 Answers

Your innermost loop (iterating the puls variable) doesn't really make any sense, and because of the condition on it (puls < 1000) Console.WriteLine never runs.

Perhaps you should test whether A + B + C is 1000, instead.

Also, you'll find that you might be missing a couple particular combinations of numbers because of the bounds on your loops (depending on the problem statement.)

like image 170
mqp Avatar answered Nov 06 '22 01:11

mqp