This program is giving me a count up to 999. which is not what it is suppose to do. can someone tell me how to fix it. The question is as follow:

Write a program that allows the user to enter any number of integer values continuously (in any order) until the user enters 999. Display the sum of the values entered, not including 999.

static void Main()
    {
        int num= 1;

        Console.WriteLine("Please enter a number:");
        Console.ReadLine();


        while (num >1 || num < 999)
        {
            Console.WriteLine("The Sum of {0}", num);
            num = num + 1;
        }
    }
}

Recommended Answers

All 4 Replies

Member Avatar for saravind84

Hi,

You can use the code given below.

static void Main(string[] args)
        {
            int iNum = 0;
            int iSum = 0;
            
            while (iNum != 999)
            {
                Console.WriteLine("Please enter a number:");
                iNum = Convert.ToInt32(Console.ReadLine());
                if (iNum != 999)
                {
                    iSum = iSum + iNum;
                }
            }

            Console.WriteLine("The sum of the numbers is {0}", iSum);
            Console.ReadLine();
        }

Thank you for the help. But i am getting a error.

The name 'convert' does not exist in the current context

I got it to work. i typed the as convert instead of Convert.
You Rock. Thank you.

Member Avatar for saravind84

Mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.