PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 16, 2022

[FIXED] How to get Numbers from user store them until they type -1 and than add all the numbers up toghterr

 August 16, 2022     add, c#, input, loops, output     No comments   

Issue

Hey i am trying to figure out the best way to get numbers from a user and than once the user types -1 it will add up all the numbers


Solution

This does as your question's described, you can test it here

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        List<int> numbers = new List<int>();
        Console.WriteLine("Enter your numbers below, 1 per line");
        string input = "";
        int inputNumber = 0;
        while((input = Console.ReadLine()) != "-1"){
            if(int.TryParse(input, out inputNumber)){
               numbers.Add(inputNumber);
            }
        }
        int sum = numbers.Sum();
        Console.WriteLine("Total Sum: " + sum);
    }
    
}


Answered By - Dean Van Greunen
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing