Advertisement

Latest Post

Sunday, 1 January 2017

How to print the sum of all numbers up to a given number[C#].

Write a program in C# to print the sum of all numbers up to a given number.

Following is the simple C# code :

class Lab
    {
        static void Main(string[] args)
        {
            int number;
            double sumOfNumbers = 0;
            Console.Write("how many Number you want to enter: ");
            number = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < number; i++)
            {
                double number1 = Convert.ToDouble(Console.ReadLine());
                sumOfNumbers += number1;
            }
            Console.WriteLine("The Sum of All entered number is =" + sumOfNumbers);
            Console.ReadLine();

        }
    }

how many Number you want to enter: 

45
60
12
15
78

The Sum of All entered number is =210

Government Jobs