0

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;
        }
    }
}

Edited by Dani: Formatting fixed

2
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by saravind84
0

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();
        }

Edited by saravind84: Add Comments

0

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

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.