user2669196
user2669196

Reputation: 175

Writing out input numbers to console application,C#

I have a program that asks for a number (int x). then, the user should input x numbers to the console. And the console should add all the numbers together and write out the result of all the input numbers. So I've done this:

Console.WriteLine("Enter an number: ");
int x = int.Parse(Console.ReadLine());

for (int i = 0; i < x; i++ )
{
    Console.WriteLine("Ange tal {0}: ",i );
    double numbers= double.Parse(Console.ReadLine());
}

Console.WriteLine("Sum of the entered numbers are: {0} ",x);
Console.ReadLine();

But the the result only gives me the last entered number. What am I doing wrong?

Upvotes: 2

Views: 27609

Answers (7)

Dido Viktorov
Dido Viktorov

Reputation: 11

    Console.Write("Enter N number: ");
    double numberN = double.Parse(Console.ReadLine());
    double sum = 0;

    for (double i = 0; i < numberN; i++)
    {

        Console.Write("Enter number: ");
        double number = double.Parse(Console.ReadLine());
        sum += number;

    }

    Console.WriteLine("The sum is: {0}", sum); 

Upvotes: 1

Umer Mushtaq
Umer Mushtaq

Reputation: 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace add

{
    class Program
    {
        static void Main(string[] args)
        {
            int a,b,c;
            Console.WriteLine("Enter the first number");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter second number");
            b = Convert.ToInt32(Console.ReadLine());
            c = a + b;
            Console.WriteLine("The addition of two number is {0}", c);
            Console.ReadLine();
        }
    }
}

Upvotes: 0

Sergey Gavruk
Sergey Gavruk

Reputation: 3558

You need to make a variable where you will store the sum of the numbers (sum). Then after you read the next number, you should add it to your sum.

Console.WriteLine("Enter a number: ");
int x = int.Parse(Console.ReadLine());
double sum = 0;
for (int i = 0; i < x; i++ )
{
   Console.WriteLine("Ange tal {0}: ", i);
   double number = double.Parse(Console.ReadLine());
   sum += number;
}

Console.WriteLine("Sum of the entered numbers is: {0}", sum);
Console.ReadLine();

Upvotes: 3

xanatos
xanatos

Reputation: 111820

Here there is the code, with the Write and WriteLine correctly formatted.

Console.Write("Enter an number: ");
int x = int.Parse(Console.ReadLine());

double sum = 0;

for (int i = 0; i < x; i++)
{
    Console.Write("Ange tal {0}: ", i);
    double number = double.Parse(Console.ReadLine());
    sum = sum + number;
}

Console.WriteLine("Sum of the entered numbers are: {0:R} ", sum);
Console.Write("Press a key to exit");
Console.ReadKey();

But now we want to go a step forward: try inserting:

2
0.1
0.2

(or 0,1 and 0,2 if you use , as the decimal separator)

I always think that OMG Ponies!!! (Aka Humanity: Epic Fail) is the best reading possible...

Upvotes: 0

Ehsan
Ehsan

Reputation: 32661

You can do it like this

Console.WriteLine("Enter an number: ");
int x = int.Parse(Console.ReadLine());
List<double> allNumbers = new List<double>();
for (int i = 0; i < x; i++ )
{
      Console.WriteLine("Ange tal {0}: ",i );
      double temp;
      if(double.TryParse(Console.ReadLine(), out temp))
         allNumbers.Add(temp);
      else
          Console.WriteLine("Enter a valid number");  
}

Console.WriteLine("Sum of the entered numbers are: {0} ",allNumbers.Sum());
Console.ReadLine();

Upvotes: 0

Deeko
Deeko

Reputation: 1539

You never actually do any summation in your code.

double sum = 0;
for (int i = 0; i < x; i++ )
{
    Console.WriteLine("Ange tal {0}: ",i );
    double numbers= double.Parse(Console.ReadLine());
    sum += numbers;
}

    Console.WriteLine("Sum of the entered numbers are: {0} ",sum);

Upvotes: 0

CaveCoder
CaveCoder

Reputation: 791

This way the sum of the inputed number will be shown

Console.WriteLine("Enter an number: ");
        int x = int.Parse(Console.ReadLine());
        double sum = 0
        for (int i = 0; i < x; i++ )
        {
            Console.WriteLine("Ange tal {0}: ",i );
            sum = sum + double.Parse(Console.ReadLine());

        }

        Console.WriteLine("Sum of the entered numbers are: {0} ",sum);
        Console.ReadLine();

Upvotes: 0

Related Questions