Unlimited Sum - C# Programming Exercise

In this exercise, you need to create a program that allows the user to enter an unlimited amount of numbers. Besides entering numbers, the program should allow the user to enter the following commands:
"sum", to display the sum of all the numbers entered so far.
"view", to display all the numbers entered.
"end", to quit the program.

This exercise is an excellent opportunity to learn how to handle user input and how to interact with text strings and numbers in C#. The program should keep running, accepting new numbers or commands until the "end" command is entered.

Here is an example of the program's execution:
Number or command? 5
Number or command? 3
Number or command? view
Entered numbers:
5
3
Number or command? 6
Number or command? sum
Sum = 14
Number or command? -7
Number or command? end

This exercise allows you to practice using control structures like conditionals and loops in C#, as well as improve your skills in handling collections and displaying data to the user.

 Category

Dynamic Memory Management

 Exercise

Unlimited Sum

 Objective

Create a program to allow the user to enter an unlimited amount of numbers. Also, they can enter the following commands:

"sum", to display the sum of all the numbers entered so far.
"view", to display all the numbers entered.
"end", to quit the program.

This is an execution sample:
Number or command? 5
Number or command? 3
Number or command? view

Entered numbers:
5
3

Number or command? 6
Number or command? sum
Sum = 14
Number or command? -7
Number or command? end

 Write Your C# Exercise

// Importing necessary namespaces for basic functionality
using System;
using System.Collections.Generic; // For using List

class Program
{
    static void Main(string[] args)
    {
        // List to store the numbers entered by the user
        List numbers = new List();

        // Start an infinite loop to keep asking the user for input
        while (true)
        {
            // Prompt user to enter a number or command
            Console.Write("Number or command? ");
            string input = Console.ReadLine();

            // Check if the input is a command
            if (input == "sum")
            {
                // Calculate the sum of all entered numbers
                int sum = 0;
                foreach (int number in numbers)
                {
                    sum += number;
                }
                // Display the sum
                Console.WriteLine("Sum = " + sum);
            }
            else if (input == "view")
            {
                // Display all the numbers entered so far
                Console.WriteLine("Entered numbers:");
                foreach (int number in numbers)
                {
                    Console.WriteLine(number);
                }
            }
            else if (input == "end")
            {
                // Exit the program
                break;
            }
            else
            {
                // Try to convert the input to a number
                if (int.TryParse(input, out int number))
                {
                    // Add the number to the list if it is valid
                    numbers.Add(number);
                }
                else
                {
                    // Inform the user if the input is invalid
                    Console.WriteLine("Invalid input, please enter a number or a valid command.");
                }
            }
        }

        Console.WriteLine("Program has ended.");
    }
}

 Share this C# exercise

 More C# Programming Exercises of Dynamic Memory Management

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#.

  •  ArrayList - Text file reader

    In this exercise, you need to create a basic text file reader that displays 21 lines of text and allows the user to navigate using the up and down arrow keys, and exit using...

  •  Hast Table - Dictionary

    In this exercise, you need to create a dictionary using a hash table. The purpose of this exercise is to practice the implementation of an efficient data struc...

  •  Parenthesis

    In this exercise, you need to implement a function to check if a sequence of open and closed parentheses is balanced. In other words, the function should check if every open...

  •  Mix and sort files

    In this exercise, you need to create a program that reads the contents of two different files, merges them, and sorts them alphabetically. The program should be able to take...

  •  ArrayList of Points

    In this exercise, you need to create a structure named "Point3D" to represent a point in 3D space with X, Y, and Z coordinates. The structure should allow storing and manipu...

  •  Search in file

    In this exercise, you need to create a program that reads the contents of a text file, saves the content into an ArrayList, and prompts the user to enter sentences to...