Home > Mobile >  How can i make a addition with the numbers from for loop in c#?
How can i make a addition with the numbers from for loop in c#?

Time:09-02

I am a newbie on c# and I am trying to make a calculator. In these codes, I am trying to take the values from input in a for loop and make an addition with them. But I couldn't do it. How can I do this?

using System;
using System.Text;

namespace cihantoker
{
    class Program1
    {
        public static void Main()
        {
            Console.WriteLine("Please enter your operation:");
            Console.WriteLine("[1]Addition");
            Console.WriteLine("[2]Subtraction");
            Console.WriteLine("[1]Multiplacition");
            Console.WriteLine("[1]Division");
            String operation = Console.ReadLine();
            //Addition Begins
            if (operation == "1")
            {

                Console.WriteLine("Please enter the count of the numbers that you want to make addition:");
                String NumberCount = Console.ReadLine();
                int Numbercount = int.Parse(NumberCount);
                for (int i = 0; i < Numbercount; i  )
                {
                    String NumberToMakeAddition = Console.ReadLine();
                    int NumberToMakeAddition2 = int.Parse(NumberToMakeAddition);

                }
            }
        }
    }
}

CodePudding user response:

You aren't adding any numbers in your for loop. You are overwriting the value of NumberToMakeAddition2 with each number you parse.

Define NumberToMakeAddition2 outside the for loop and initialize it to 0. Add each number that you parse to the sum.

You have no error handling so be careful if the text entered is not an integer.

CodePudding user response:

Cihan, First, you are assuming people would make inputs that are parseable to int. That may not be the case. Check and study int.TryParse() for that matter. Second, in your loop, you are redeclaring a NEW variable NumberToMakeAdition2 and assigning the parsed value to it. There is no addition there. What you should do is to initialize it before the loop, say:

int sum = 0;

and then in loop add to it:

sum  = int.Parse(...) // remember TryParse, just keeping it simple here

CodePudding user response:

I will suggest you use int.TryParse() instead of int.Parse since we cannot trust the input from the console. And the only thing you need to do here is to declare one variable outside the loop and keep the sum in that variable while looping. A sample code will be like the following:

if (operation == "1")
{
    Console.WriteLine("Please enter the count of the numbers that you want to make addition:");
    int.TryParse(Console.ReadLine(), out int numberCount);
    int sumOfNumbers = 0;
    for (int i = 0; i < numberCount; i  )
    {
        if (int.TryParse(Console.ReadLine(), out int numberInput))
        {
            sumOfNumbers  = numberInput;
        }
        else
        {
            Console.WriteLine("Wrong input from console, skipping the number");
        }
    }
    Console.WriteLine($"Sum of numbers :{sumOfNumbers}");
}

CodePudding user response:

Create a List like this: List<int> myList = new List<int>(), to push all your users numbers into. In your loop, when you parse the users entry, use myList.Add(NumberToMakeAddition). Then, after your loop, you can do MyList.Sum(), or you can loop through your list and add them one at a time

  •  Tags:  
  • c#
  • Related