Many Numbers And Sum - C# Programming Exercise

In this C# exercise, you are asked to write a program that asks the user for several numbers (until they enter the word "end") and then displays the sum of the entered numbers. When the execution ends, the program should display all the numbers entered and the final sum, as shown below:

Enter a number: 5
Sum = 5
Enter a number: 3
Sum = 8
Enter a number: end
The numbers are: 5 3
The sum is: 8

This exercise is excellent practice for handling data inputs in C#, using loops to process multiple user inputs and accumulating values in a variable to obtain the total sum. Additionally, it will help you work with strings and how to manage the exit condition with the keyword "end".

 Category

Arrays, Structures and Strings

 Exercise

Many Numbers And Sum

 Objective

Write a C# program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is going to end, it must display all the numbers entered, and the sum again, as follows:

Enter a number: 5
Sum = 5
Enter a number: 3
Sum = 8
Enter a number: end
The numbers are: 5 3
The sum is: 8

 Write Your C# Exercise

using System;  // Import the System namespace for basic functionality

class Program  // Define the main class
{
    static void Main()  // The entry point of the program
    {
        double sum = 0;  // Variable to store the sum of the numbers
        string input;  // Variable to store user input
        string numbersEntered = "";  // String to store the entered numbers
        
        // Prompt user for numbers until they enter "end"
        Console.WriteLine("Enter numbers (type 'end' to stop):");
        
        // Continue looping until user enters "end"
        while (true)
        {
            Console.Write("Enter a number: ");
            input = Console.ReadLine();  // Read user input
            
            if (input.ToLower() == "end")  // Check if input is "end"
                break;  // Exit the loop if "end" is entered
            
            // Try to convert the input to a double and add it to the sum
            if (double.TryParse(input, out double number))
            {
                sum += number;  // Add the number to the sum
                numbersEntered += number + " ";  // Append the number to the list of entered numbers
                Console.WriteLine($"Sum = {sum}");  // Display the current sum
            }
            else
            {
                Console.WriteLine("Invalid input. Please enter a valid number.");
            }
        }

        // Display the entered numbers and the total sum once "end" is entered
        Console.WriteLine($"The numbers are: {numbersEntered.Trim()}");
        Console.WriteLine($"The sum is: {sum}");
    }
}

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Two dimensional array

    In this C# exercise, you are asked to write a program that asks the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and then disp...

  •  Statistics V2

    In this C# exercise, you are asked to create a statistical program that allows the user to perform the following actions: - Add new data - See all data ...

  •  Struct

    In this C# exercise, you are asked to create a struct to store data of 2D points. The fields for each point will be: - x coordinate (short)

  •  Array of struct

    In this C# exercise, you are asked to expand the previous exercise that used a struct to store 2D points. Now, you need to store up to 1,000 points usin...

  •  Array of struct and menu

    In this C# exercise, you are asked to expand the previous exercise (array of points) so that the program displays an interactive menu. The menu should a...

  •  Books database

    In this C# exercise, you are asked to create a small database that will be used to store information about books. For each book, the following data should be kept: